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
Sean C.ax1056Sean C.ax1056 

Auto update the Opportunity Stage field when a Contract's Status field changes to Activated

Hi all

 

I thought I could achieve this with a workflow and field update but no :-(  From research it looks like the only choice maybe a trigger? Anyones advice would be appreciated.

 

This is what I'm trying to do:

 

An approval process has been setup for Contracts and we only allow one contract to be created per Opportunity. When a Contract is approved in the Approval Process and and the Contract 'Status' pick list field is changed to 'Activated' then I want the 'Stage' pick-list field in that Opportunity to be auto updated/changed to 'Closed Won'.

 

Almost forgot, the Opportunity Stage value will be 'Closed Won Pending Approval' when the Contract is approved, just in case yuo may be wondering why the Oppotunuty Stage isn't already set at 'Closed Won'

 

Any ideas? Is a trigger the only way?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

I'd imagine a trigger would look like this:

 

trigger updateOppOnContractActivation on Contract (after update) {
  Set<Id> oppIds = new Set<Id>();
  for(Contract c:Trigger.new) {
    if(c.Status=='Activated' && Trigger.oldMap.get(c.id).Status!='Activated') {
      oppIds.add(c.Opportunity__c);
    }
  }
  for(Opportunity[] oppList:[select id from opportunity where id in :oppIds]) {
    for(Opportunity opp:oppList) {
      opp.StageName = 'Closed Won';
    }
    update oppList;
  }
}

Does this look like what you're looking for?

All Answers

sfdcfoxsfdcfox

Correct. Workflow rules only work on the "current" object, so you can't go off and update some other object with workflow. The trigger isn't so bad; it should consist of less than 10 lines of code. I don't think you'll find any other way to go about this.

Sean C.ax1056Sean C.ax1056

Thanks sfdcfox

 

I thought that might be the case.

 

Does anyone have an example of a trigger written for this purpose?

 

It sounds like it might be a fairly common request (auto updating a field from Contract to Opportunity?)

 

The trigger would need to update the standard Stage pick-list field in the Opportunity object to 'Closed Won', regadless of it''s current value, when the Contract created on that Opportunity is approved and the stadard 'Status' pick-list field on that Contract is changed to 'Activated'.

 

Any help would be appreciated.

 

Thanks

sfdcfoxsfdcfox

I'd imagine a trigger would look like this:

 

trigger updateOppOnContractActivation on Contract (after update) {
  Set<Id> oppIds = new Set<Id>();
  for(Contract c:Trigger.new) {
    if(c.Status=='Activated' && Trigger.oldMap.get(c.id).Status!='Activated') {
      oppIds.add(c.Opportunity__c);
    }
  }
  for(Opportunity[] oppList:[select id from opportunity where id in :oppIds]) {
    for(Opportunity opp:oppList) {
      opp.StageName = 'Closed Won';
    }
    update oppList;
  }
}

Does this look like what you're looking for?

This was selected as the best answer
Sean C.ax1056Sean C.ax1056

Thanks sfdcfox. Much appreciated. Will try this in the sandbox. Cheers

jjulianjjulian

Why the nested for loops on the opportunity ids?