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
Elsa CordonnierElsa Cordonnier 

Custom Object <> Opportunity Trigger

Hello, 
I need help and advise to build a smal trigger on a Custom Object to push some information on the Opportunity, the mentionned object don't have any Master relationship with the Opportunity. 

My Object is called: Consumer_Order__c
I need to push the information only if the order_type__c (Picklist) is "Compensation Credit"

My aim, is to push in the Opportunity 2 information when the Object is created.
- First the Net_Price__c (Currency)
- Second the Compensation_credit_reasons__c (Picklist)

The two fields I have to update in the Opportunity are: 
- Consumer_Compensation_credit__c (Currency)
- Consumer_Compensation_credit_reasons__c (Text)

Can you help me building this simple Trigger ? 
Thank you in advance
RaidanRaidan
Hi Elsa,

I assume the Consumer_Order__c has a Lookup field to the Opportunity (say the field name is Opportunity__c). You can easily use the Process Builder to do this.
  • Create a new Process Builder
  • Select Consumer_Order__c as the main object
  • For the criteria, you can add the Order_Type__c field = Compensation Credit
  • For the action, select Update Records, and select the Opportunity
However, if you still want to use a trigger, here is a quick code:
trigger ConsumerOrderTrigger on (after create) {
	List<Opportunity> optiesToUpdate = new List<Opportunity>();
	for (Consumer_Order__c co : Trigger.new) {
		if (co.Order_Type__c == 'Compensation Credit') {
			optiesToUpdate.add(new Opportunity(
				Id = co.Opportunity__c
				, Consumer_Compensation_Credit__c = co.Net_Price__c
				, Consumer_Compensation_Credit_Reasons__c = co.Compensation_Credit_Reasons__c
			));
		}
	}
	update optiesToUpdate;
}