Create a trigger on object specified in Milestone Template creation
Since the Squivr Timeline can be utilized across standard and custom objects of your choice, a simple three line trigger is needed on any object where the Timeline component is utilized and where Milestones 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.
Included Milestone Triggers
- Account Trigger
- Opportunity Trigger
Out of the box, Squivr offers triggers for both the Account and Opportunity objects. To enable these triggers, navigate to Setup > Custom Settings > Manage Squivr Trigger Settings and enable the appropriate trigger (Account.Trigger or Opportunity.Trigger). Account 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_Opportunity_Trigger
) - Specify sObject (ie,
Opportunity
) - Click Submit
- Copy the below and replace the current trigger in the Developer Console window
- File > Save
trigger squivr_Opportunity_Trigger on Opportunity (after insert, after update) {
squivr.MilestoneHelpers.triggerMilestones(Trigger.new, Trigger.old);
}
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) {
squivr.MilestoneHelpers.triggerMilestones(Trigger.new, Trigger.old);
}
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 adding a Milestone Template for, leverage that! Simply add the below line to the after insert
trigger context.
Opportunity example...
squivr.MilestoneHelpers.triggerMilestones(Trigger.new, Trigger.old);
Generic example...
squivr.MilestoneHelpers.triggerMilestones(Trigger.new, Trigger.old);
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 single line of code, so if a test class already exists and fires after insert
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/Opportunity 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 a = new Account();
a.Name = 'testAccount';
insert a;
//next, insert a new Opportunity
Opportunity o = new Opportunity();
o.Name = 'testOpportunity';
o.AccountId = a.Id;
o.StageName = 'Closed Won';
o.CloseDate = system.today();
insert o;
}
}
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.