You need to sign in to do that
Don't have an account?

help with first trigger
Hey Folks,
I am writing my first non-tutorial trigger and could use some help. What is supposed to happen is that when an RFI record of record type A is created and child Submittal record should also be created. I need to set a bunch of field values but I am comfortable with that, just trying to get the skeleton to work. However, when I create an RFI with record type A no Submittal record is created. This is my trigger. Issue_Tracker__c is the lookup on the Submittal to the RFI
trigger NewSubmittal on Request_for_Information_RFI__c (after insert) {
for ( Request_for_Information_RFI__c ss : trigger.new) {
if( ss.RecordTypeId == '012800000007gsp' )
continue;
Submittal_1__c s = new Submittal_1__c();
s.Issue_Tracker__c = ss.Id;
insert s;
}
}
I am writing my first non-tutorial trigger and could use some help. What is supposed to happen is that when an RFI record of record type A is created and child Submittal record should also be created. I need to set a bunch of field values but I am comfortable with that, just trying to get the skeleton to work. However, when I create an RFI with record type A no Submittal record is created. This is my trigger. Issue_Tracker__c is the lookup on the Submittal to the RFI
trigger NewSubmittal on Request_for_Information_RFI__c (after insert) {
for ( Request_for_Information_RFI__c ss : trigger.new) {
if( ss.RecordTypeId == '012800000007gsp' )
continue;
Submittal_1__c s = new Submittal_1__c();
s.Issue_Tracker__c = ss.Id;
insert s;
}
}
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html
Best practices are IMPORTANT to follow.
As per your code, let's figure out the flaws:
1. We should never hardcode IDs:
if( ss.RecordTypeId == '012800000007gsp' ) // here you have hardcoded
2. DML inside a loop is big NO. You have to efficiently use collections (List, Map, Set to as best practice)
insert s; // this DML is inside for loop
3. Usually, we create handler class to keep the logic and in trigger, we don't keep any logic. But, let me correct the code in the way you have written:
All Answers
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html
Best practices are IMPORTANT to follow.
As per your code, let's figure out the flaws:
1. We should never hardcode IDs:
if( ss.RecordTypeId == '012800000007gsp' ) // here you have hardcoded
2. DML inside a loop is big NO. You have to efficiently use collections (List, Map, Set to as best practice)
insert s; // this DML is inside for loop
3. Usually, we create handler class to keep the logic and in trigger, we don't keep any logic. But, let me correct the code in the way you have written: