You need to sign in to do that
Don't have an account?
Rajat 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.
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.
All Answers
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.
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
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.
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;
Really appreciate your efforts, thanks a lot again.