• Neil Smith 10
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hi,

I have a custom object (inspection) which has a lookup to the account object. The account object also has a formula field called SPAN. In an Apex class I'd like to create an inspection with a lookup to an account. It's a little weird how the data comes to the class but here it is:

@RestResource(urlMapping='/inspection/*')
global with sharing class InspectionRestController{
   global class RequestBody {
       global Inspection__c inspection;
   }

    @HttpPost
    global static Inspection__c create(InspectionRestController.RequestBody req) {
        Inspection__c newInspection = req.inspection;
        
        if (req.inspection.Account__r != null) {
            String span = req.inspection.Account__r.SPAN__c;
            
            Account account = [select Id from Account where SPAN__c = :span];
            
            newInspection.Account__c.Id = account.Id;
        }
    
        insert newInspection;
        return newInspection;
    }
}

I've tried to attach the account to the inspection in every way possible (new Account(Id = account.Id), etc...). I know I'm getting the correct account by the query because I've logged it out during debugging. 

Any idea what I'm doing wrong?

Thanks
I have a custom object called Questions. This object has a lookup to itself because a question can have additional questions which depend on their parent question. We need to insert 200+ at a time so I wrote an apex class that simply inserts the collection given to it:

global with sharing class QuestionRestController{
   global class RequestBody {
       global List<Question__c> questions;
   }

    @HttpPost   
    global static List<Question__c> createBulk(QuestionRestController.RequestBody req) {
        insert req.questions;
        return req.questions;
    }
}

I get this error: System.DmlException: Insert failed. First exception on row 94; first error: INVALID_FIELD, Foreign key external ID: 966.0 not found for field MyExtId__c in entity Question__c no matter what I try.

I figured I could sort the questions before sending the request so that the parent questions are always inserted first but I still get the errors. I've added debug statements so I could verify the order of the questions prior to inserting and they are ordered correctly. I've also tried to insert each question by itself:

for (Question__c question: req.questions) {
    insert question;
}

But then I get a System.LimitException because I'm sending 200+ questions.

So what is the proper way to insert a list of custom objects which have a lookup to itself when the parent and child are both being inserted with the same request?

Thanks
I have a custom object called Questions. This object has a lookup to itself because a question can have additional questions which depend on their parent question. We need to insert 200+ at a time so I wrote an apex class that simply inserts the collection given to it:

global with sharing class QuestionRestController{
   global class RequestBody {
       global List<Question__c> questions;
   }

    @HttpPost   
    global static List<Question__c> createBulk(QuestionRestController.RequestBody req) {
        insert req.questions;
        return req.questions;
    }
}

I get this error: System.DmlException: Insert failed. First exception on row 94; first error: INVALID_FIELD, Foreign key external ID: 966.0 not found for field MyExtId__c in entity Question__c no matter what I try.

I figured I could sort the questions before sending the request so that the parent questions are always inserted first but I still get the errors. I've added debug statements so I could verify the order of the questions prior to inserting and they are ordered correctly. I've also tried to insert each question by itself:

for (Question__c question: req.questions) {
    insert question;
}

But then I get a System.LimitException because I'm sending 200+ questions.

So what is the proper way to insert a list of custom objects which have a lookup to itself when the parent and child are both being inserted with the same request?

Thanks