function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Erik LarameeErik Laramee 

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;
        
    }

}
Best Answer chosen by Erik Laramee
Nayana KNayana K
http://www.sfdc99.com/2013/05/12/example-how-to-write-a-simple-apex-trigger-2/
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:
trigger NewSubmittal on Request_for_Information_RFI__c (after insert) 
{
	// get intended record type Id for comparison
	Id intendedRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('YOUR_RECORDTYPE_NAME_HERE').getRecordTypeId();
	
	// create List to store child records for insertion
	List<Submittal_1__c> lstSubmittalToInsert = new List<Submittal_1__c>();
	
	// child instance
	Submittal_1__c objSumittal;
	
	// iterate over parent records
    for(Request_for_Information_RFI__c ss : Trigger.new) 
	{
		// if record type is different than intendedRecordTypeId
        if(ss.RecordTypeId != intendedRecordTypeId)
		{
			objSumittal = new Submittal_1__c();
			objSumittal.Issue_Tracker__c = ss.Id;
			/*
			objSumittal.FIELD_API_NAME_1 = SOMEVALUE1;
			objSumittal.FIELD_API_NAME_2 = SOMEVALUE2;
            ..........................................
			*/
			
			// add it to the list of insertion
			lstSubmittalToInsert.add(objSumittal);
		} 
	}
	// if list isn't empty, then insert
	if(!lstSubmittalToInsert.isEmpty())
	{
		insert lstSubmittalToInsert;
	}
}

 

All Answers

Nayana KNayana K
http://www.sfdc99.com/2013/05/12/example-how-to-write-a-simple-apex-trigger-2/
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:
trigger NewSubmittal on Request_for_Information_RFI__c (after insert) 
{
	// get intended record type Id for comparison
	Id intendedRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('YOUR_RECORDTYPE_NAME_HERE').getRecordTypeId();
	
	// create List to store child records for insertion
	List<Submittal_1__c> lstSubmittalToInsert = new List<Submittal_1__c>();
	
	// child instance
	Submittal_1__c objSumittal;
	
	// iterate over parent records
    for(Request_for_Information_RFI__c ss : Trigger.new) 
	{
		// if record type is different than intendedRecordTypeId
        if(ss.RecordTypeId != intendedRecordTypeId)
		{
			objSumittal = new Submittal_1__c();
			objSumittal.Issue_Tracker__c = ss.Id;
			/*
			objSumittal.FIELD_API_NAME_1 = SOMEVALUE1;
			objSumittal.FIELD_API_NAME_2 = SOMEVALUE2;
            ..........................................
			*/
			
			// add it to the list of insertion
			lstSubmittalToInsert.add(objSumittal);
		} 
	}
	// if list isn't empty, then insert
	if(!lstSubmittalToInsert.isEmpty())
	{
		insert lstSubmittalToInsert;
	}
}

 
This was selected as the best answer
Erik LarameeErik Laramee
Wow, thank you for taking the time to write such a detailed response! It is much appreciated. I will check out the recources you've linked.