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
domdickdomdick 

Need Trigger to update lookup field

We have a lookup field (related_contract_number__c) on our Account object. The lookup field is related to Contract object (field called: ContractNumber)

Now I would like to write a trigger to update the lookup field on Account once Contract status change to 'Accepted'.

I am completely lost on how to do this. So how can i write a trigger on Contract (after update) to reference in the related_contract_number__c once the status changed?

 

I would appreciate any help you can give me.  Thanks so much!!! 

igoldsteinigoldstein

Triggers can only modify records of the same type as what the trigger is defined on, so you can't use a trigger on Contract  to update an Account field. Check out workflow rules instead.

SidharthSidharth
trigger test on Contract (after update) {
	
	List<Contract> modifiedContract = new List<Contract>();
	Set<Id> modifiedContractIds = new Set<Id>();
	List<Account> modifiedAccount = new List<Account>();
	
	if(trigger.isUpdate){		
		for(Contract a: trigger.new){
			if(a.contractStatus != Trigger.oldMap.get(a.Id).contractStatus && a.contractStatus = 'Accepted'){
				modifiedContract.add(a);
				modifiedContractIds.add(a.Id);
			}
		}		
	}
	
	modifiedAccount = [Select id, related_contact_number__c from Account where id IN: modifiedContractIds]; 
	
	for(Account a: modifiedAccount){
		a.related_contact_number__c = ('update this field');
	}
	
	update modifiedAccount;

}

 

domdickdomdick

Thanks Sidharth for valuable replied on this. But it doesn't update lookup field 'Related_Contract_Number__c' with actual ContractNumber (get from ContracNumber field on Contract object).

Here is the code and comment

 

trigger test on Contract (after update) {

    List<Contract> modifiedContract = new List<Contract>();
    Set<Id> modifiedContractIds = new Set<Id>();
    List<Account> modifiedAccount = new List<Account>();
   
    if(trigger.isUpdate)
    {
        for(Contract c: trigger.new)
        {
            if(c.Status != trigger.oldMap.get(c.Id).Status && c.Status == 'Activated')
            {
                modifiedContract.add(c);
                modifiedContractIds.add(c.Id);
            }
        }
    }
    modifiedAccount = [Select Id, Related_Contract_Number__c from Account where Id IN :modifiedContractIds];
    for(Account a: modifiedAccount)
    {
        a.Related_Contract_Number__c = trigger.newMap.get(a.Id).ContractNumber; //(Value should update with related ContractNumber from Contract object)
         
    }
    update modifiedAccount;
}

 

Am i missing something?

 

Thanks in advanced!

SidharthSidharth

 

trigger test on Contract (after update) {

    List<Contract> modifiedContract = new List<Contract>();
    Set<Id> modifiedContractIds = new Set<Id>();
    List<Account> modifiedAccount = new List<Account>();
   
    if(trigger.isUpdate)
    {
        for(Contract c: trigger.new)
        {
            if(c.Status != trigger.oldMap.get(c.Id).Status && c.Status == 'Activated')
            {
                modifiedContract.add(c);
                modifiedContractIds.add(c.Id);
            }
        }
    }


    List<Account> finalAccountstoUpdate = new List<Account>();


    modifiedAccount = [Select Id, Related_Contract_Number__c from Account where Id IN :modifiedContractIds];


    for(Account a: modifiedAccount){

for(Contract c:modifiedContract ){

if(a.id == c.Client__c.id){// i am assuming, Client__c is the lookup field in Contract objhect for related Account

a.Related_Contract_Number__c = c.ContractNumber;

finalAccountstoUpdate .add(a);

        }
         }
    }
    update finalAccountstoUpdate ;

}

 

If you still face problem, use debug statements, to see where you getting the wrong values/error.

ex debug statement: System.debug(finalAccountstoUpdate);

domdickdomdick

Still it doesn't add any value from Contract object (field - ContractNumber) on Related_Contract_Number__c on Account object and not even display any error message.

Something is not working and i can't figure out where the code is breaking. I have update the section with following code...

 

 for(Account a: modifiedAccount)
    {
        for(Contract c:modifiedContract)
        {
            if(a.id == c.AccountId)
            {
                a.Related_Contract_Number__c = c.ContractNumber;
                finalAccounttoUpdate.add(a);
            }
        }        
    }
    if(!finalAccounttoUpdate.isEmpty() || finalAccounttoUpdate.isEmpty())
update (finalAccounttoUpdate);
    System.debug(finalAccounttoUpdate);



Thank you for all insights on this post. Let me know for further explaination.