1

I have a custom button that creates a "Renewal" record type opportunity from a Service Contract. I would like to display the opportunity stage on the related service contract ("Expired service contract" lookup field), so whenever the stage is updated, the field on SC is updated also. I'd like to know if: 1. my trigger below is written in a correct way (i.e. won't fail if I'll upload opportunties in mass import). 2. how can I "convert" it into a apex class?

 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 && ( Trigger.isInsert || 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;
  }
}
}
1

Change it to

list<ServiceContract> sc = [select id,Renewal_Opportunity_Status_Field__c from ServiceContract where id in :oppids];
for(ServiceContract c:sc) {
      c.Renewal_Opportunity_Status_Field__c = opps.StageName;
}
update sc;

and it looks fine.

  • thanks! any help on writing this trigger on apex class? (as I have several trigger on opportunities..) – Itay B Mar 19 '13 at 14:36
  • Create a static function with this code, and sent from trigger to function the list of the Opportunity . – Shimshon Korits Mar 19 '13 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.