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

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) 
sandeep@Salesforcesandeep@Salesforce

Here is code you can use 

trigger contract on Contract (after insert ,after Update)

{
   List<Account> ListToUpdate = new list<Account>();

   Map<id , Account> MapOfAccounts = new Map<id , Account>([select id, Name from Account ]);
   Account a ;

   for(Contract contract : trigger.new)

    {
         if(Contract.AccountId != null)

        {             

            a = new Account();    

            a = MapOfAccounts.get(contract.AccountId) ;

            a.Name = contract.Name ;

            ListToUpdate.add(a);
        }
   }
   update ListToUpdate;
}

All Answers

AshlekhAshlekh

Hi ,

 

If you want to update a field on account after inserting or updating the contract object record then you can achive this by trigger on contract object which will execute on after/before inserting or updating the contract object record.

 

If this info help you then Please mark it as a solution and give me kudo's by click on star icon

 

Thanks 

Ashlekh Gera

Devender MDevender M
Hi,

You can before/after insert for this

U can try this, in this is have update account name u can update your text field.
trigger contract on Contract (after insert) {
list<Account> listOfAccounts = new list<Account>();
for(Contract contract : trigger.new) {
if(Contract.AccountId != null) {
Account account = new account(Id =Contract.AccountId, Name = contract.Name);
listOfAccounts.add(account);
}
}
update listOfAccounts;
}
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

sandeep@Salesforcesandeep@Salesforce

Here is code you can use 

trigger contract on Contract (after insert ,after Update)

{
   List<Account> ListToUpdate = new list<Account>();

   Map<id , Account> MapOfAccounts = new Map<id , Account>([select id, Name from Account ]);
   Account a ;

   for(Contract contract : trigger.new)

    {
         if(Contract.AccountId != null)

        {             

            a = new Account();    

            a = MapOfAccounts.get(contract.AccountId) ;

            a.Name = contract.Name ;

            ListToUpdate.add(a);
        }
   }
   update ListToUpdate;
}

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..
AshlekhAshlekh

Hi,

 

Here is the best code with all securtiy 

trigger contract on Contract (after insert ,after Update)
{
          //Map hold the Parent Account which are parent of contract
         Map<id , Account> MapOfAccounts = new Map<id , Account>();

        //Iterating the contract record and updating the related account name of account reocord by contract name
       for(Contract contract : trigger.new)
      {
             if(Contract.AccountId != null)
             {
                  Account a = new Account(id = contract.AccountId,a.Name = contract.Name );
                 MapOfAccounts.put(contract.AccountId,a);
             }
       }

     //This will hold the child account record of parent account ( which are derictly related to contract) 
       Map<id, Account> ChildAccountMap = new Map<Id,Account>();
       for(Account acc :[select id ,name ,ParentId form account where ParentId :MapOfAccounts.keySet()])
       {
                if(MapOfAccounts.containsKey(acc.Parent))
               {
                      //Updating the name of name of account by the name of parent account name (Parent account name already update by contract name)
                      ChildAccountMap.put(acc.id,new Account(id=acc.id,name = MapOfAccounts.get(acc.ParentId).name) );
               }
       }


     //Now Upated all account record in single dml opertating

        if(ChildAccountMap!=null && ChildAccountMap.size()>0) 
                  MapOfAccounts.putAll(ChildAccountMap);

        if(MapOfAccounts!= null && MapOfAccounts.size()>0 )
               update MapOfAccounts.values();

 }

 

Dont forget to give me kudo's . 

 

Have nice day.

 

Thanks

Ashlekh