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
kailash ambekarkailash ambekar 

trigger on Opportunity to update custom object field

i'm new to Trigger. i want to write trigger on Opportnuity . 
Opportunity Standard field :CloseDate
Custom Field :Status(Status_c) (picklist)

Custom Object :Customer(Customer__c)
custom field : Contract End Date(Contract_End_Date)

when Opportunity status field is updated to Accepted, then customer  contract end date field should be updated to close date . there is master detail relationship. i have tried to code.
trigger ContractDate on Opportunity ( after update) {
		List <ID> opps=New List<ID>();
    		
    for(Opportunity o:Trigger.new){
        if(o.Status__c=='Approved'){
            opps.add(o.Id);
           
        }
    }
    
    Customer__c customerList=[SELECT Id,Contract_End_Date__c FROM Customer__c WHERE id in:opps];
	
    
    for(Customer__c cst:customerList){
       customerList.Contract_End_Date__c=Opps.CloseDate;
         update cst;
    }
   

}
Jayanth ThathapudiJayanth Thathapudi
Hi Kailash,

Please post the error you are getting .

Regards
Jay
Dayakar.DDayakar.D
Hi Kailash,

You can simply use process builder to accumplish you requirement, anyway below trigger will help you.
 
trigger ContractDate on Opportunity (after update) {
    List <ID> opps=New List<ID>();
    Map<Id,Opportunity> OpportunityMap= New Map<Id,Opportunity>();
    for(Opportunity o:Trigger.new){
        system.debug('');
        if(o.Status__c=='Accepted'){
            opps.add(o.Id);
            OpportunityMap.put(o.Id, o);
        }
    }
    
    list<Customer__c> customerList=[SELECT Id,Contract_End_Date__c,Opportunity__c FROM Customer__c WHERE Opportunity__c in:opps];
    List<Customer__c> listOfCustomersToUpdate=new List<Customer__c>();
    for(Customer__c cst:customerList){
        Opportunity OpportunityVar=OpportunityMap.get(cst.Opportunity__c);
        cst.Contract_End_Date__c=OpportunityVar.CloseDate;
        listOfCustomersToUpdate.add(cst);
    }
    update listOfCustomersToUpdate;
}

 Please let me know, if it helps you.

BestRegards,
Dayakar.D