Create a trigger on object specified in child Group Configuration
Since the Squivr ArcGroups Grouped Records can be utilized across standard and custom objects of your choice, a simple three line trigger is needed on any object where the ArcGroups component is utilized and where Group Configurations and Rules are defined.
Note: Salesforce triggers can not be created directly in a production environment. You will need to first create in a sandbox, ensure code coverage requirements are met (see below) and then deploy to production. For any objects that are NOT Contact or OpportunityContactRole, you will need to add a lookup field on the Grouped Record Object that points at your desired Object.
Included Group Triggers
- Contact Trigger
- OpportunityContactRole Trigger
Out of the box, Squivr offers triggers for both the Contact and OpportunityContactRole objects. To enable these triggers, navigate to Setup > Custom Settings > Manage Squivr Trigger Settings and enable the appropriate trigger (Contact.Trigger or OpportunityContactRole.Trigger). Contact Example:
New Opportunity Trigger Example
Follow these steps for creating a trigger on the Opportunity object.
- Open the Developer Console
- File > New > Apex Trigger
- Specify name of your choosing (ie,
Squivr_Contact_Trigger
) - Specify sObject (ie,
Contact
) - Click Submit
- Copy the below and replace the current trigger in the Developer Console window
- File > Save
trigger squivr_Contact_Trigger on Contact (after insert, after update, before delete) {
Boolean contextDelete = Trigger.isDelete;
List<Contact> records = contextDelete
? Trigger.old
: Trigger.new;
squivr.GroupedRecordHelper.evaluateRecords(records, !Trigger.isInsert);
}
New Generic Trigger Template
Use the below example, replacing items in [brackets] and using the Opportunity Trigger Example as guide.
trigger [your_trigger_name] on [sObject] (after insert, after update, before delete) {
Boolean contextDelete = Trigger.isDelete;
List<[sObject]> records = contextDelete
? Trigger.old
: Trigger.new;
squivr.GroupedRecordHelper.evaluateRecords(records, !Trigger.isInsert);
}
Utilize Existing Trigger Framework
Salesforce best practice is to limit the number of triggers on a given object - ideally only have one (One Trigger to rule them all)
If a trigger already exists for the object you are evaluating to be added to the Groups based on Rules, leverage that! Simply add the below lines to the after insert, after update, before delete
trigger context. With multiple trigger contexts being evaluated, actual placement of this logic may differ in your use case and based on existing trigger logic
Contact example...
Boolean contextDelete = Trigger.isDelete;
List<Contact> records = contextDelete
? Trigger.old
: Trigger.new;
squivr.GroupedRecordHelper.evaluateRecords(records, !Trigger.isInsert);
Generic example...
Boolean contextDelete = Trigger.isDelete;
List<[sObject]> records = contextDelete
? Trigger.old
: Trigger.new;
squivr.GroupedRecordHelper.evaluateRecords(records, !Trigger.isInsert);
Create a Test Class
If this is the first trigger implemented on a given object, you will need to write a quick test class in order to deploy to production. The new trigger added counts as a few lines of code, so if a test class already exists and fires after insert, after update, or before delete
you'll already achieve full coverage from a percentage standpoint.
Opportunity test class example...
@isTest
private class squivr_Opportunity_Trigger_Test {
//Note: this example shows the Account/Contact fields that are required out of box. If your org has other system required fields, validation rules, etc. you may need to adjust which fields are set
static testMethod void testOpportunityInsert(){
//first, insert a new Account
Account acc = new Account();
acc.Name = 'testAccount';
insert acc;
//next, insert a new Contact
Contact con = new Contact();
con.FirstName = 'Test';
con.LastName = 'Contact'
con.AccountId = acc.Id;
insert con;
}
}
Generic test class example...
...since this really depends on what object the trigger lives on, we can't provide a useful example here. Feel free to reach out if you need support!
Comments
0 comments
Please sign in to leave a comment.