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
GRStevenBrookesGRStevenBrookes 

Create Record and update Parent

Hi,

 

I have the following trigger which creates a New SI record on the SA object.

 

However, If i wanted to update a field on the SA (parent) record, where would i insert the code:

 

///Trigger now includes Creating Service Implementation and also Accounts Approval Case///
trigger AutoCreateSI on Service_Agreement__c (after insert)
{
    if(!StaticClass.flag)
    {
        List<Service_Implementation__c> listSA = new List<Service_Implementation__c>();
        List<Case> listC = new List<Case>();
        Id rtId = [select Id,name from RecordType where name='Client Management' and SObjectType='Case' limit 1].Id;
       
        for(Service_Agreement__c s : trigger.new)
        if(Trigger.isInsert)
            {
                if(s.Name != '' )
                {
                  listSA.add(new Service_Implementation__c(
                  name = s.Name,
                  Service_Agreements__c = s.id,
                  Scriptor__c='00520000001RyDK',
                  Lead_Trainer__c='00520000001xKyY'            
                  ));
                  if(s.Name != '' )
                  s.COUNT_of_AccountsSetupCases__c = 1;
               listC.add(new Case(
                  Service_Agreement__c = s.id,
                  Requested_By__c=userinfo.getUserId(),
                  RecordTypeId=rtId,
                  AccountID=s.Related_Account__c,
                  Origin='Automated Process',
                  Case_Type__c='Accounts',
                  Case_Reason__c='Approve Set-up',
                  Subject='Account Set-up - Initial Approval',
                  Description='Please update the Status of this case to indicate that you have all the required financial information for this client and where appropriate, you have been successful in collecting any deposits or advance payments.'
                  ));
               		
               	
               }
               
               
               
            }
    
        if(listSA.size() > 0)
        {
            insert listSA;
        }
        if(listC.size() > 0)
        {
            insert listC;
        }
        StaticClass.flag = true ;
    }
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
shruthishruthi

Hi

 

Assuming the field which you want to update is FieldToBeUpdated__c and the Parent is Parent__c on Service_Agreement__c

 

///Trigger now includes Creating Service Implementation and also Accounts Approval Case///
trigger AutoCreateSI on Service_Agreement__c (after insert)
{
    if(!StaticClass.flag)
    {
        List<Service_Implementation__c> listSA = new List<Service_Implementation__c>();
        List<Case> listC = new List<Case>();
        Set<Id> SAIds = new Set<Id>();
        Id rtId = [select Id,name from RecordType where name='Client Management' and SObjectType='Case' limit 1].Id;
      
        for(Service_Agreement__c s : trigger.new)
        if(Trigger.isInsert)
            {
                if(s.Parent__c != null)
                    SAIds.add(s.Parent__c);
                if(s.Name != '' )
                {
                  listSA.add(new Service_Implementation__c(
                  name = s.Name,
                  Service_Agreements__c = s.id,
                  Scriptor__c='00520000001RyDK',
                  Lead_Trainer__c='00520000001xKyY'           
                  ));
                  if(s.Name != '' )
                  s.COUNT_of_AccountsSetupCases__c = 1;
               listC.add(new Case(
                  Service_Agreement__c = s.id,
                  Requested_By__c=userinfo.getUserId(),
                  RecordTypeId=rtId,
                  AccountID=s.Related_Account__c,
                  Origin='Automated Process',
                  Case_Type__c='Accounts',
                  Case_Reason__c='Approve Set-up',
                  Subject='Account Set-up - Initial Approval',
                  Description='Please update the Status of this case to indicate that you have all the required financial information for this client and where appropriate, you have been successful in collecting any deposits or advance payments.'
                  ));
                      
                  
               }
              
              
              
            }
       
        List<Service_Agreement__c> parents = [select id, FieldToBeUpdated__c from Service_Agreement__c where id in :SAIds];
        for(Service_Agreement__c s:parents) {
            s.FieldToBeUpdated__c = 'UPDATED';
        }
        /* If you want your update to vary based on the child you can use the following logic
        for(Service_Agreement__c s:parents) {
            for(Service_Agreement__c st : trigger.new) {
                if(st.Parent__c == s.Id) {
                    s.FieldToBeUpdated__c = 'UPDATED BASED ON CHILD'+st.Id;
                }   
            }
       
        }
        */
        if(listSA.size() > 0)
        {
            insert listSA;
        }
        if(listC.size() > 0)
        {
            insert listC;
        }
        if(parents.size() > 0) {
            update parents;
        }
        StaticClass.flag = true ;
    }
    }



Hope this helps!

All Answers

Starz26Starz26

If you know what the field should be on insert, then add a BEFORE INSERT clause and section.

 

In the before insert secion, set the field to the value you want and that is it. When the record is inserted the field will be as you set it.

 

You would have to modify the trigger.isInsert to

 

trigger.isInsert && Trigger.isAfter

 

so it does not run during the isBefore trigger.

shruthishruthi

Hi

 

Assuming the field which you want to update is FieldToBeUpdated__c and the Parent is Parent__c on Service_Agreement__c

 

///Trigger now includes Creating Service Implementation and also Accounts Approval Case///
trigger AutoCreateSI on Service_Agreement__c (after insert)
{
    if(!StaticClass.flag)
    {
        List<Service_Implementation__c> listSA = new List<Service_Implementation__c>();
        List<Case> listC = new List<Case>();
        Set<Id> SAIds = new Set<Id>();
        Id rtId = [select Id,name from RecordType where name='Client Management' and SObjectType='Case' limit 1].Id;
      
        for(Service_Agreement__c s : trigger.new)
        if(Trigger.isInsert)
            {
                if(s.Parent__c != null)
                    SAIds.add(s.Parent__c);
                if(s.Name != '' )
                {
                  listSA.add(new Service_Implementation__c(
                  name = s.Name,
                  Service_Agreements__c = s.id,
                  Scriptor__c='00520000001RyDK',
                  Lead_Trainer__c='00520000001xKyY'           
                  ));
                  if(s.Name != '' )
                  s.COUNT_of_AccountsSetupCases__c = 1;
               listC.add(new Case(
                  Service_Agreement__c = s.id,
                  Requested_By__c=userinfo.getUserId(),
                  RecordTypeId=rtId,
                  AccountID=s.Related_Account__c,
                  Origin='Automated Process',
                  Case_Type__c='Accounts',
                  Case_Reason__c='Approve Set-up',
                  Subject='Account Set-up - Initial Approval',
                  Description='Please update the Status of this case to indicate that you have all the required financial information for this client and where appropriate, you have been successful in collecting any deposits or advance payments.'
                  ));
                      
                  
               }
              
              
              
            }
       
        List<Service_Agreement__c> parents = [select id, FieldToBeUpdated__c from Service_Agreement__c where id in :SAIds];
        for(Service_Agreement__c s:parents) {
            s.FieldToBeUpdated__c = 'UPDATED';
        }
        /* If you want your update to vary based on the child you can use the following logic
        for(Service_Agreement__c s:parents) {
            for(Service_Agreement__c st : trigger.new) {
                if(st.Parent__c == s.Id) {
                    s.FieldToBeUpdated__c = 'UPDATED BASED ON CHILD'+st.Id;
                }   
            }
       
        }
        */
        if(listSA.size() > 0)
        {
            insert listSA;
        }
        if(listC.size() > 0)
        {
            insert listC;
        }
        if(parents.size() > 0) {
            update parents;
        }
        StaticClass.flag = true ;
    }
    }



Hope this helps!

This was selected as the best answer
GRStevenBrookesGRStevenBrookes

Perfect - You a brilliant! Thank you