Plugin code to calculate sum of fields across records:
I have the below requirement to be implemented in plugin code.
I need to add the values of the fields and update it in a new field.
eg: I need to add the values of field called "A" and display the sum of total in the field called " Total".
Just need guidance on how the logic should be written. Any help would be appreciated.
Hi @Gayu ,
Reference to start with Plugin: Link
This is simple,
1. Decide if you want to trigger the plugin on create or update
2. Based on your Target entity, check if the values exists that you want.
3. If you get, calculate Total and save it as new Entity then use service.update to save the record. Else throw error or add Tracelog
4. Sign, Build and register your plugin
5. Add steps (Create and or Update based on your requirement)
6. Test it
Hope this helps
You may also need to consider Delete/ Deactivation options if they are valid scenarios.
If you are new to Plug-ins, below are code snippets to start;
Create/ Update: https://sumedha8.blogspot.com/2012/10/sample-plug-in-code-create.html
Status Change: http://sumedha8.blogspot.com/2012/10/sample-plug-in-state-change.html
Delete: http://sumedha8.blogspot.com/2012/11/sample-plug-in-delete_1.html