Create a trigger on the object specified in the Object field on the Object Rule Configuration
Since Squivr AI can be utilized across standard and custom objects of your choice, a simple six line trigger is needed on any object where the Object Rules should be evaluated. This is to be utilized for Field Updates, Record Level Validation, Field Level Validation.
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.
Included Group Triggers
- Contact Trigger
Out of the box, Squivr offers triggers for the Contact object. To enable these triggers, navigate to Setup > Custom Settings > Manage Squivr Trigger Settings and enable the appropriate trigger (Contact.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) {
String triggerType = Trigger.isInsert
? 'Create'
: 'Create and Edit';
ObjectRuleHelper.evaluateObjectRules(Trigger.new, triggerType);
}
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) {
String triggerType = Trigger.isInsert
? 'Create'
: 'Create and Edit';
ObjectRuleHelper.evaluateObjectRules(Trigger.new, triggerType);
}
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
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
Generic example...
String triggerType = Trigger.isInsert
? 'Create'
: 'Create and Edit';
ObjectRuleHelper.evaluateObjectRules(Trigger.new, triggerType);
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
you'll already achieve full coverage from a percentage standpoint.
Contact test class example...
@isTest
private class squivr_Contact_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.