You need to sign in to do that
Don't have an account?
domdick
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!!!
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.
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!
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);
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...
Thank you for all insights on this post. Let me know for further explaination.