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
Glenn BalmonteGlenn Balmonte 

Apex Trigger assigning Record Type for Child Record

Hello Experts,

Need your assistance. We have a trigger that wil automatically create child records every time that the Parent Record Type is selected (e.g. If User selected the Parent Record Type "AIM"). I just need your inputs on how I can set all of these Child Records to one record type which is also AIM. Here is the code:


//trigger that creates a new production task for every new AIM Project with SEO products
 
trigger AutoCreateProductionTask on SFDC_Project__c (after insert) {

   List<Production_Task__c> newRecord = new List<Production_Task__c>(); 

   List<String> namePT = new List<String>();
   namePT.add('Website Audit Report and Recommendations');
   namePT.add('Keyword Research');
   namePT.add('Competitor Research');
   namePT.add('Run page speed loading test and make improvements');
   namePT.add('Run site on validator and fix broken links and html errors');
   namePT.add('Run crawl test and redirect any obsolete urls');
   namePT.add('Keyword mapping, meta data creation and implementation');
   namePT.add('Text content creation');
   namePT.add('Upload optimized text content, insert and highlight ketwords');
   namePT.add('Optimize keywords in footer links');
   namePT.add('Optimize alt tags of images');
   namePT.add('Create or improve HTML sitemap page');
   namePT.add('Add breadcrumbs navigation on inner pages');
   namePT.add('Add Google+ Authorship code on inner pages');
   namePT.add('Create Google Places listing');
   namePT.add('Submit site to web directories');
   namePT.add('Create Google Webmaster Tools profile');
   namePT.add('Create Google Analytics profile');
   namePT.add('Create and submit xml sitemap to Google');
   namePT.add('Create and publish 1 Press Release');
   
    for (SFDC_Project__c aim : Trigger.new) {
        
        if (aim.RecordTypeID == '012300000009FLk')
        if (aim.Product_Service__c == 'SEO - Premium'|| aim.Product_Service__c == 'SEO - Pro'|| aim.Product_Service__c == 'SEO - Starter')
        
                {

          for(Integer i = 0; i < namePT.size(); i++){
            
            Production_Task__c aimTask = new Production_Task__c();
            aimTask.Name = namePT [i];
            aimTask.Due_Date__c = Date.today().addDays(30);
            aimTask.Project__c = aim.Id;
            aimTask.Instructions__c = 'test';
            
                newRecord.add(aimTask);   
            
           }
           }
            insert newRecord;
   }
   }

Any assistance is greatly appreciated. Thank you. 
Best Answer chosen by Glenn Balmonte
Naresh YadavNaresh Yadav
Hi Glenn Balmonte,

Take a look on the below code where i can bold the fonts.
One more thing don't use hard coded ids in the code it will fail if you are doing so.
 
trigger AutoCreateProductionTask on SFDC_Project__c (after insert) {

   List<Production_Task__c> newRecord = new List<Production_Task__c>(); 

   List<String> namePT = new List<String>();
   namePT.add('Website Audit Report and Recommendations');
   namePT.add('Keyword Research');
   namePT.add('Competitor Research');
   namePT.add('Run page speed loading test and make improvements');
   namePT.add('Run site on validator and fix broken links and html errors');
   namePT.add('Run crawl test and redirect any obsolete urls');
   namePT.add('Keyword mapping, meta data creation and implementation');
   namePT.add('Text content creation');
   namePT.add('Upload optimized text content, insert and highlight ketwords');
   namePT.add('Optimize keywords in footer links');
   namePT.add('Optimize alt tags of images');
   namePT.add('Create or improve HTML sitemap page');
   namePT.add('Add breadcrumbs navigation on inner pages');
   namePT.add('Add Google+ Authorship code on inner pages');
   namePT.add('Create Google Places listing');
   namePT.add('Submit site to web directories');
   namePT.add('Create Google Webmaster Tools profile');
   namePT.add('Create Google Analytics profile');
   namePT.add('Create and submit xml sitemap to Google');
   namePT.add('Create and publish 1 Press Release');
   
    for (SFDC_Project__c aim : Trigger.new) {
        
        if (aim.RecordTypeID == Schema.SObjectType.SFDC_Project__c.getRecordTypeInfosByName().get('AIM').getRecordTypeId())
        if (aim.Product_Service__c == 'SEO - Premium'|| aim.Product_Service__c == 'SEO - Pro'|| aim.Product_Service__c == 'SEO - Starter')
        
                {

          for(Integer i = 0; i < namePT.size(); i++){
            
            Production_Task__c aimTask = new Production_Task__c();
            aimTask.recordTypeId = Schema.SObjectType.Production_Task__c.getRecordTypeInfosByName().get('AIM').getRecordTypeId();
            aimTask.Name = namePT [i];
            aimTask.Due_Date__c = Date.today().addDays(30);
            aimTask.Project__c = aim.Id;
            aimTask.Instructions__c = 'test';
            
                newRecord.add(aimTask);   
            
           }
           }
            insert newRecord;
   }
   }

Let me if it is helpful.
Peace.

All Answers

Naresh YadavNaresh Yadav
Hi Glenn Balmonte,

Take a look on the below code where i can bold the fonts.
One more thing don't use hard coded ids in the code it will fail if you are doing so.
 
trigger AutoCreateProductionTask on SFDC_Project__c (after insert) {

   List<Production_Task__c> newRecord = new List<Production_Task__c>(); 

   List<String> namePT = new List<String>();
   namePT.add('Website Audit Report and Recommendations');
   namePT.add('Keyword Research');
   namePT.add('Competitor Research');
   namePT.add('Run page speed loading test and make improvements');
   namePT.add('Run site on validator and fix broken links and html errors');
   namePT.add('Run crawl test and redirect any obsolete urls');
   namePT.add('Keyword mapping, meta data creation and implementation');
   namePT.add('Text content creation');
   namePT.add('Upload optimized text content, insert and highlight ketwords');
   namePT.add('Optimize keywords in footer links');
   namePT.add('Optimize alt tags of images');
   namePT.add('Create or improve HTML sitemap page');
   namePT.add('Add breadcrumbs navigation on inner pages');
   namePT.add('Add Google+ Authorship code on inner pages');
   namePT.add('Create Google Places listing');
   namePT.add('Submit site to web directories');
   namePT.add('Create Google Webmaster Tools profile');
   namePT.add('Create Google Analytics profile');
   namePT.add('Create and submit xml sitemap to Google');
   namePT.add('Create and publish 1 Press Release');
   
    for (SFDC_Project__c aim : Trigger.new) {
        
        if (aim.RecordTypeID == Schema.SObjectType.SFDC_Project__c.getRecordTypeInfosByName().get('AIM').getRecordTypeId())
        if (aim.Product_Service__c == 'SEO - Premium'|| aim.Product_Service__c == 'SEO - Pro'|| aim.Product_Service__c == 'SEO - Starter')
        
                {

          for(Integer i = 0; i < namePT.size(); i++){
            
            Production_Task__c aimTask = new Production_Task__c();
            aimTask.recordTypeId = Schema.SObjectType.Production_Task__c.getRecordTypeInfosByName().get('AIM').getRecordTypeId();
            aimTask.Name = namePT [i];
            aimTask.Due_Date__c = Date.today().addDays(30);
            aimTask.Project__c = aim.Id;
            aimTask.Instructions__c = 'test';
            
                newRecord.add(aimTask);   
            
           }
           }
            insert newRecord;
   }
   }

Let me if it is helpful.
Peace.
This was selected as the best answer
Glenn BalmonteGlenn Balmonte
I guess I am missing something here. I was able to save it but received an error when I tried to do test record. 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AutoCreateProductionTask caused an unexpected exception, contact your administrator: AutoCreateProductionTask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AutoCreateProductionTask: line 29, column 1

 
Naresh YadavNaresh Yadav
Hi Glenn Balmonte

Make sure that you have recordtype on SFDC_Project__c named as 'AIM'.
Glenn BalmonteGlenn Balmonte
I already figured out. Record Type is not only AIM, it was AIM Project. I modified the code you provided and it is already working. Thank you so much Naresh :) Appreciate your help here. 
sandeep@Salesforcesandeep@Salesforce
 Hi Naresh, 

Can you please take out describe call from loop. 

Thanks
Sandeep Singhal