You need to sign in to do that
Don't have an account?
Steve Kucklinca
Is it possible to limit trigger execution to exact value of a specified field
this code is firing in the sandbox and has 100% code coverage there and I have validated successfully (not yet deployed) in my production org. However I want to know if it can be edited (and how) to fire only when a specified field Completed__c on Merchant_Application__c object is updated with the value 'Completed'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){
Set<Id> accountIds = new Set<Id>();
Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
for(Merchant_Application__c ma : Trigger.new){
accountIds.add(ma.Account_Name__c);
}
for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
accountOpportunityMap.put(opp.AccountId,opp);
}
List<MerchOpps__c> mOps = new List<MerchOpps__c>();
for(Merchant_Application__c ma : Trigger.new){
if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
mOps.add(mo);
}
}
insert mOps;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){
Set<Id> accountIds = new Set<Id>();
Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
for(Merchant_Application__c ma : Trigger.new){
accountIds.add(ma.Account_Name__c);
}
for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
accountOpportunityMap.put(opp.AccountId,opp);
}
List<MerchOpps__c> mOps = new List<MerchOpps__c>();
for(Merchant_Application__c ma : Trigger.new){
if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
mOps.add(mo);
}
}
insert mOps;
}
Inside every trigger is the "old" version of the record and the "new" version. You basically want to compare the old and new version to see if it changed to a specific value.
Here's a good example:
http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MerchOppsRecords caused an unexpected exception, contact your administrator: MerchOppsRecords: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.MerchOppsRecords: line 18, column 1
Do I have the comparison in the wrong place? Do I use 'Completed' to often? Does it matter that trigger comes 'After Update'?
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Set<Id> accountIds = new Set<Id>();
Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
for(Merchant_Application__c ma : Trigger.new){
accountIds.add(ma.Account_Name__c);
}
for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
accountOpportunityMap.put(opp.AccountId,opp);
}
List<MerchOpps__c> mOps = new List<MerchOpps__c>();
for(Merchant_Application__c ma : Trigger.new){
Merchant_Application__c oldma = Trigger.oldMap.get(ma.Id);
Boolean oldmaiscompleted = oldma.Completed__c.equals ('Completed');
Boolean newmaiscompleted = ma.Completed__c.equals ('Completed');
if (!oldmaiscompleted && newmaiscompleted) {
ma.Completed__c = 'Completed';
}
if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
mo.Name = ma.Name + '_Notification';
mOps.add(mo);
}
}
insert mOps;
}
Merchant_Application__c oldma = Trigger.oldMap.get(ma.Id);
if (ma.Completed__c !=oldma.Completed__c) {
if (ma.Completed__c == 'Completed')