+ Start a Discussion
Arundhati DebArundhati Deb 

write a trigger on opportunity to update opportunity fields based on the contract licence type

Hi

I have to write a trigger to update the different opportunity fields based on the contract licence type. Contract id is a lookup field on opportunity and we have to see the contract licence type and update opportunity fields
SAKTHIVEL MSAKTHIVEL M
Hi Arundhati,

Please use the below sample trigger to achieve your requirement
 
trigger updateOpportunityOnContract 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;
  }
}


if you have the relationship, then you can achieve from process builder as well.

Thanks & Regards,
Sakthivel Madesh