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
Rajat Bhatt 4Rajat Bhatt 4 

Update Record using Trigger

Hi, I am new to salesforce i am finding difficulty in writing trigger.
I have 2 fields named 'Claim' on Account and Contract Object. Account is the parent record(Lookup Relationship).
How to automatically update the claim field of Account object with the Contract claim field using trigger.
Thanks in advance.
Best Answer chosen by Rajat Bhatt 4
SabrentSabrent
Here's the trigger code for your use case. Make sure you use the correcr field name for Claim
 
trigger UpdateClaim on Contract (after insert, after update) { 
 
 // Collect all Parent Account Id's  
 List<Id> listAccIds = new List<Id>();
 
 // Map of AccountID and Contract
 Map<ID, Contract> parentAccs = new Map<ID, Contract>();
 
 // Whenever a new contract record is inserted or updated
 // add the parent account id to the list above
 // also add to the map, the parent accountid and the contract record
 // this way we have the child associated with the parent handy    
 for (Contract childObj : Trigger.new) {
    listAccIds.add(childObj.AccountId);
     parentAccs.put(childObj.AccountId,childObj);
     
 }

   // Create a list of all Accounts that are parents of the contract records being inserted or updated 
  List<Account> parentAccList = [SELECT id,Name,Claim_c FROM Account WHERE ID IN :listAccIds]; 

  // Loop through each of the account record in the list and 
  // assign to the parent record the same value that's in the child record on the Claim__c field   
  for (Account acc: parentAccList){
    acc.Claim__c = parentAccs.get(acc.Id).Claim__c;
  }
  // update the list outside the for loop  
  update parentAccList;
  	
}

 

All Answers

SabrentSabrent
Here's the trigger code for your use case. Make sure you use the correcr field name for Claim
 
trigger UpdateClaim on Contract (after insert, after update) { 
 
 // Collect all Parent Account Id's  
 List<Id> listAccIds = new List<Id>();
 
 // Map of AccountID and Contract
 Map<ID, Contract> parentAccs = new Map<ID, Contract>();
 
 // Whenever a new contract record is inserted or updated
 // add the parent account id to the list above
 // also add to the map, the parent accountid and the contract record
 // this way we have the child associated with the parent handy    
 for (Contract childObj : Trigger.new) {
    listAccIds.add(childObj.AccountId);
     parentAccs.put(childObj.AccountId,childObj);
     
 }

   // Create a list of all Accounts that are parents of the contract records being inserted or updated 
  List<Account> parentAccList = [SELECT id,Name,Claim_c FROM Account WHERE ID IN :listAccIds]; 

  // Loop through each of the account record in the list and 
  // assign to the parent record the same value that's in the child record on the Claim__c field   
  for (Account acc: parentAccList){
    acc.Claim__c = parentAccs.get(acc.Id).Claim__c;
  }
  // update the list outside the for loop  
  update parentAccList;
  	
}

 
This was selected as the best answer
Rajat Bhatt 4Rajat Bhatt 4
Hi Rov,
Thanks for your efforts, i forgot to mention the data type of claim field in the contract object.
In the contract object the datatype of the claim field is lookup but in the account object the datatype of the claim field is Text , so the problem
is instead of getting the name of claim field its showing the id.
Please help in resoving the issue.
Thanks.
Ajay K DubediAjay K Dubedi
Hi Rajat,

The below trigger is as per your requirement. Hope this will work for you.

trigger ContarctTrigg on Contract(after insert, after update) {
    
    UpdateAccount.usingClaim(trigger.New);
}


public class UpdateAccount{


public static void usingClaim(List<Contract > contractList){
try{
    Set<Id> accIdSet = new Set<Id>();
    
    Map<ID, Contract> accIdVsContractMap = new Map<ID, Contract>();
    
    for(Contract con : contractList){
        
        accIdSet.add(con.AccountId);
        
        if(!accIdVsContractMap.containsKey(con.AccountId)){
             accIdVsContractMap.put(con.AccountId,con);
        }
        
    }
    
    List<Account> accToUpdateList = new List<Account>();
    
    accToUpdateList = [SELECT id,Name,Claim_c FROM Account WHERE ID IN :accIdSet]
    
    for (Account acc: accToUpdateList){
        acc.Claim__c = accIdVsContractMap.get(acc.Id).Claim__c;
    }
    
    Update accToUpdateList;
    
}
Catch(Exception exp){
    system.debug('The following exception has occurred: ' + exp.getMessage() + ' at line no: ' + exp.getLineNumber());
}
}

}

Thank you
Ajay Dubedi
Rajat Bhatt 4Rajat Bhatt 4
Hi Ajay,
Thanks for your efforts, i tried to implement your code but it is also showing the id instead of name.
Could u please resolve this issue.
Thanks.
SabrentSabrent
What does the Claim__c field lookup to? 

Try this ...

String lookupfieldname = Contract__r.WhateverItLooksUpto__r.Name;
   
Then in the for loop, replace  acc.Claim__c = parentAccs.get(acc.Id).Claim__c;  with 
 
acc.Claim__c = lookupfieldname;
Rajat Bhatt 4Rajat Bhatt 4
Thanks Rov,
Really appreciate your efforts, thanks a lot again.