+ Start a Discussion
Bayarea 101Bayarea 101 

Opportunity update trigger

What i am trying to acehive here are 2 things.
1. When an Opportunity is updated this trigger should look for a custom object. If there is a custom object is available than fire other wise not.
2. Once this trigger is fired certain fields on the opportunity should be updated. 
trigger Oppupdate on Opportunity (After update)
{
Opportunity Opp = [Select ID broker_firm__c, broker_firm_office__c, primary_contact_role__c from Opportunity WHERE ID In:Trigger.newMap.keySet()];
Underwriting__c U=[Select ID Opportunity__c from Underwriting__c where Opportunity_Id__c =:Opp.Id];

For (Opportunity O:Trigger.new){        
                 U.broker_firm__c = o.broker_firm__c;
                 U.broker_firm_office__c = o.broker_firm_office__c;               
                 U.broker_name__c = o.primary_contact_role__c;
                 }
}
KaranrajKaranraj
Try the below code, just cross verify the api name of the fields in the below code
trigger Oppupdate on Opportunity (before update)
{

List<Underwriting__c> U=[Select ID,Opportunity__c,broker_firm__c,broker_firm_office__c,broker_name__c from Underwriting__c where Opportunity__c In:Trigger.newMap.keySet()];

for (Opportunity Oppty:Trigger.new){   
		for(Underwriting__c uw: u){
			Oppty.broker_firm__c = o.broker_firm__c;
        	Oppty.broker_firm_office__c = o.broker_firm_office__c;               
        	Oppty.broker_name__c = o.primary_contact_role__c;
		}     
    }
}
To understand more about apex code, check this trailhead modules https://developer.salesforce.com/trailhead/trail/force_com_programmatic_beginner
 
PragPrag
For satisfying 1st condition, you need to check whether the realtionship (Lookup / masterdetail) fileds are having values or not. This needs to be the first condition in Opportunity trigger.

For second condition, the code(multi group reference data model - List/set/Map) given by Karanraj will work for your requirement.

All the best! 
Bayarea 101Bayarea 101
This code is not working ti fails on compiling