+ Start a Discussion
Itayb34Itayb34 

Opportunity Stage is changed - Update Related Service Contract Field

Hello

 

I have a lookup field from opportunity (on record type "Renewal") to Service Contracts, and I'm trying to update a service contract field , based on changes on the opportunity stage. For example: if an opportunity is changed to won, custom field on SC will be "won", if it's changed to lost, field will be "Lost". if the stage is changed to an open stage, field is "Open".

I can't use workflows...but I managed to use the code below. The problem is it updates only when the Opportunity is closed (I want that the Service contract will be updated on any stage change)

 

trigger UpdateSCFromOpp on Opportunity (after update) {


  Set<Id> oppIds = new Set<Id>();
    
    RecordType debtRT = [Select Id From RecordType Where Name = 'Enterprise - Renewal'];
    for(Opportunity opps : Trigger.new){
        // Check for Renewal Deal Record Type and change from open to close
       if(opps.RecordTypeId == debtRT.Id && opps.isclosed == true && trigger.OldMap.get(opps.Id).isclosed == false && opps.Expired_Service_Contract__c != null)
        {
        
            oppids.add(opps.Expired_Service_Contract__c);
        }
        }
                   
        for (ServiceContract[] sc:[select id from ServiceContract where id in :oppids]) {
        
          for(ServiceContract c:sc) {
          
        
          c.Renewal_Opportunity_Status_Field__c = 'Closed Won';
 
       }
            update sc;
  }
}

 

 

 

Thanks!

 

Itay

 

 

Prafull G.Prafull G.

Hi Italy,

 

As your trigger is checking the Closed opty only i.e. additng Ids to set only when an Opportunity is updated to closed.

 

if(opps.RecordTypeId == debtRT.Id && opps.isclosed == true && trigger.OldMap.get(opps.Id).isclosed == false && opps.Expired_Service_Contract__c != null)

 

You should remove this (red highlighted) clause and add few more if statements inside this check to create set and then use the final set to update status based on the Opportuntiy.

 

Hope it helps!

Prafull

Itayb34Itayb34

Hi crmtech21, I changed my code a bit, see below:

 

trigger UpdateSCFromOpp on Opportunity (after insert, after update) {


  Set<Id> oppIds = new Set<Id>();
    
    RecordType debtRT = [Select Id From RecordType Where Name = 'Enterprise - Renewal'];
    for(Opportunity opps : Trigger.new){
        // Check for Renewal Deal Record Type
      // if(opps.RecordTypeId == debtRT.Id && opps.isclosed == true && trigger.OldMap.get(opps.Id).isclosed == false && opps.Expired_Service_Contract__c != null)
       if(Trigger.isInsert || (opps.RecordTypeId == debtRT.Id && opps.StageName !=  trigger.OldMap.get(opps.Id).StageName))
        {
        
            oppids.add(opps.Expired_Service_Contract__c);
        }
                          
        for (ServiceContract[] sc:[select id from ServiceContract where id in :oppids]) {
        
          for(ServiceContract c:sc) {
          
        
          c.Renewal_Opportunity_Status_Field__c = opps.StageName;
 
       }
            update sc;
  }
}
}

 I added "after insert" and "Trigger.isInsert" to the code statement.

I'm also displaying the stage in the service contract  (c.Renewal_Opportunity_Status_Field__c = opps.StageName)

 

Should be ok, right?

 

Thanks!

 

Itay