1

I have a custom object called 'BidManager_Bid__c'. On this object, I created an External Lookup field called 'Global_Opportunity__c' that reference to the external object called 'Opportunity__x'.

I want to write a trigger on the 'BidManager_Bid__c' object and fetch the field 'Amount__c' from the parent 'Opportunity__x' record. Following is my code:

Trigger:

trigger BidManager_Trigger on BidManager_Bid__c (after update, after insert) {

    if(Trigger.isInsert || Trigger.isUpdate) {
        BidManagerTriggerHandler.getGlobalOpportunityInfo(Trigger.New);
    }
}

Apex:

public class BidManagerTriggerHandler {
    
    public static void getGlobalOpportunityInfo(List<BidManager_Bid__c> getInfo) {
        
        Id globalOpp;
        
        for (BidManager_Bid__c bm : getInfo) {
            globalOpp =  bm.Global_Opportunity__c;
        }
        
        Opportunity__x globalOppAmount = [Select Id, Amount__c From Opportunity__x WHERE ExternalId =: globalOpp];  
        system.debug(globalOppAmount.Amount__c);
    }

}

I could save the trigger and the class but when I try to update a record, it gives me the following error:

enter image description here

BidManager_Trigger: execution of AfterUpdate caused by: System.UnexpectedException: Apex triggers can't reference external objects or call out to external data sources. Contact your administrator for help. Class.BidManagerTriggerHandler.getGlobalOpportunityInfo: line 11, column 1 Trigger.BidManager_Trigger: line 4, column 1

0

1 Answer 1

3

Accessing External Objects in a trigger requires a callout. Callouts are not permitted in triggers. You will need to move your code to be asynchronous, using the Queueable interface, for example.

There are plenty of good resources already on this site for this. For example, this answer from Phil W: Best approach to make callouts from trigger

1
  • Nice to be cited...
    – Phil W
    Commented Nov 11 at 16:54

Not the answer you're looking for? Browse other questions tagged .