I need help troubleshooting the below apex. I have this class being called in a custom JSON Builder on a custom object. I'm logging successful runs in the log in the developer console, but in the record, the canvas is giving me an error:


Error: Campaign_Design_Profile__c.Related_Opportunity_ID__c An unexpected error has occurred. Your development organization has been notified.


//Products are Gathered from a related list of opportunities. 

    global class TestJsonBuilder extends pandadoc.JsonBuilder{
    global TestJsonBuilder()
        super(Campaign_Design_Profile__c.getSobjectType(), null);

    global override List<pandadoc.Recipient> getRecipients(sObject record) {
        List<pandadoc.Recipient> recipients = new List<pandadoc.Recipient>();
        return null;

    global override List<pandaDoc.Item> getItems(sObject record) {
        List<pandadoc.Item> items = new List<pandadoc.Item>();
        Campaign_Design_Profile__c cdp = (Campaign_Design_Profile__c) record; // create a local copy of our record to use.

        //select each opportunity in the record filter on Campaign_Design_Profile__c id
        List<Opportunity> Opp = [SELECT Id,Name,AccountId
                                    FROM Opportunity where Id = :cdp.Related_Opportunity_ID__c
        //loop though all my related Opportunities
        for(Opportunity Opportunity :Opp)
                //we need to get each product for the Opportunities now fitler on opportunityid
               List <OpportunityLineItem> products = [SELECT OpportunityId, Name, Description,ProductCode,TotalPrice,Product2Id
                                                FROM OpportunityLineItem where OpportunityId = :Opportunity.Id ];
                    //loop though each product now like we did for opportunity
                for(OpportunityLineItem product :products)
                    //create pandadoc items and add to our output list.
                 pandadoc.Item pi = new pandadoc.Item(
                        product.ProductCode,     // String   Optional    Item SKU or null
                        product.Name,            // String   Required    Item Name
                        product.TotalPrice,       // Decimal  Required    Item Price
                        product.Quantity,        // Decimal  Required    Item Quantity
                        product.Description     // String   Optional    Item Description or null

                    //Possible Overloads for Pandadoc.Items.Item
                    // pandadoc.Item.Item(sku, name, price, qty, description)
                    // pandadoc.item.Item(sku, name, price, qty, description, discount)
                    // pandadoc.item.Item(sku, name, price, qty, description, discount, customFields)

        return items;

  • 3
    The error suggests that you are using this field in your code without having that retrieved in the SOQL. How are you constructing the record and passing the record in this method getItems(sObject record)?
    – Jayant Das
    Oct 18, 2018 at 16:32
  • I'm not sure, I'm rather new to apex, honestly. Our business creates Campaign Design Profiles from Opportunities. Obviously, products live on the opp, not my custom object. The objective of this apex is to pull the products onto the custom object from the related opportunity. That is why I need OppID = Campaign_Design_Profile__c.Related_Opportunity_ID__c to ensure that I get the products that are on the related opp. Do you have any suggestions on how to make that happen?
    – Laura Babb
    Oct 18, 2018 at 16:40
  • 3
    Without having that detail, it will be difficult to say anything. The only way is to look for instances in all apex classes/triggers, where this method is being called and how you are retrieving the values and that if that SOQL consists of Related_Opportunity_ID__c field or not. If not, that's the place where the fix needs to be.
    – Jayant Das
    Oct 18, 2018 at 16:42

2 Answers 2


So this is a relation field Campaign_Design_Profile__c.Related_Opportunity_ID__c when it is relation it must have to be Campaign_Design_Profile__r.Related_Opportunity_ID__c

global override List<pandaDoc.Item> getItems(sObject record) {
    List<pandadoc.Item> items = new List<pandadoc.Item>();
    Campaign_Design_Profile__c cdp = (Campaign_Design_Profile__c) record;

The argument sObject record should be prepared, it means prior passing it to this method all the fields, particularly Related_Opportunity_ID__c, must be included to the SOQL query. For example:

Campaign_Design_Profile__c campaignDesignProfile = [SELECT Id, Related_Opportunity_ID__c FROM Campaign_Design_Profile__c];
List<pandaDoc.Item> items = new TestJsonBuilder.getItems(campaignDesignProfile);

You must log in to answer this question.

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