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
sfdcianpsfdcianp 

After Insert trigger on different objects

I am on contract object and i have a lookup for the account object , when selecting the particular account and giving a name to the contract  then if i click on save    then on that particular account  a  text field need to be updated with the name of the contract..

 

please help me in finding a solution for this trigger

 

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Hi, Try this trigger

 

trigger UpdateAccount on Contract (after insert) {
list<Account> accList = new list<Account>();
for(Contract contract : trigger.new) {
if(Contract.AccountId != null) {
Account account = new Account(Id =Contract.AccountId);
if(contract.name != NULL){
account.YourTextField__c = contract.name;
accList.add(account);
}
}
}
if(accList.size()>0){
update accList;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

souvik9086souvik9086

Hi, Try this trigger

 

trigger UpdateAccount on Contract (after insert) {
list<Account> accList = new list<Account>();
for(Contract contract : trigger.new) {
if(Contract.AccountId != null) {
Account account = new Account(Id =Contract.AccountId);
if(contract.name != NULL){
account.YourTextField__c = contract.name;
accList.add(account);
}
}
}
if(accList.size()>0){
update accList;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
sfdcianpsfdcianp
Thanks for ur help, need some more changes for this if the selected account is the parent account for the another two accounts and the child accounts also need to be updates with this contract name..