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
Steve ConnellySteve Connelly 

How do i pull an opportunity ID into an Apex trigger?

I am new to coding in general and to Apex in particular so any help I can get is greatly appreciated.

I am building a trigger that will create a new custom object record from an opportunity.

I plan to use process builder to fire the trigger when a particular opp type is created or when a particular field in that opp is updated.

I want the new record to be related to the Opp so i want to pull the Opp ID and some of the fields into the trigger to use in the new record.

How do i pull that specific data in?

Like I said, I am new at this but willing to work and learn.

Best regards,
Steve
 
Best Answer chosen by Steve Connelly
SFDC Apex DevSFDC Apex Dev
In addition, you need to pull the opportunity id related to your custom object that what I'm doing here.
This trigger is written on your custom object so there will be some logic on your opportunity trigger to populate the opp on custom object or to create custom object whenevr new opp created.

For basic of apex trigger go through the below urls:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers

Thanks!
Chirag

All Answers

SFDC Apex DevSFDC Apex Dev
Hi Steve,

Here is the sample code for you.
Create trigger on the custom object.
trigger opportunityTrigger on customObject__c(before insert, before update){    // add whatever context you wanna use
Set<Id> oppIdSet = new Set<Id>();  // Creating the set of Opportunity Id
for(customObject__c obj: Trigger.new){
        if(Trigger.IsInsert || (Trigger.IsUpdate && obj.Opportunity__c != Trigger.oldMap.get(obj.Id).Opportunity__c)){
            oppIdSet.add(obj.Opportunity__c); //Taking the opp Id from custom Object adding to the set
        }            
    }
    If(oppIdSet.size()>0){  //Checking if there are the opp records
        Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([Select Id, status from Opportunity where Id = :oppIdSet]); //Querying the OpportunityId present in the Set and adding to the map
        for(customObject__c cusObj: Trigger.new){
            if(cusObj.Opportunity__c != null && opportunityMap.containsKey(cusObj.Opportunity__c)){ // Checking if the map contains the same opportunity on your custom Object
                cusObj.status = opportunityMap.get(cusObj.Opportunity__c).status;  //adding the status field value from opportunity to the same field on your custom object
            }
        }
    }
}
Hope it will help you to write your trigger.
Please mark this as the best answer if it resolve your issue.

Thanks!
Chirag
 
Steve ConnellySteve Connelly
Thanks for both replies.

Question, if this is set to run on the custom object, how will it know to fire when a new Opp is created or updated?

Sc
Steve ConnellySteve Connelly
Another thing...I guess that I did not fully understand what a trigger is. Based upon what I am reading today, I don't have to fire this with a process. It just listens for the desired change (insert or update record).

Is that correct?
Sc
SFDC Apex DevSFDC Apex Dev
Hi Steve,

At line 1, we have used the context that means when the trigger should fire.
At line 4, we've given the condition either insert or update in both condition it will take the opportunity id from custom object and will add those in the set of id's.

Thanks!
Chirag

 
SFDC Apex DevSFDC Apex Dev
In addition, you need to pull the opportunity id related to your custom object that what I'm doing here.
This trigger is written on your custom object so there will be some logic on your opportunity trigger to populate the opp on custom object or to create custom object whenevr new opp created.

For basic of apex trigger go through the below urls:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers

Thanks!
Chirag
This was selected as the best answer
Steve ConnellySteve Connelly
Ok, I think I was unclear. I want to create the custom record after an aopportunity is created or updated. So the custom object record does not exist until inserted by the trigger. It certainly doews not have the Opp ID yet.

I had thought that the trigger would need to run on the Opp to pull in the ID and fields so i could use them in the creation of the custom record.
Sc