I am having trouble testing an apex class which inserts multiple records into a custom object having references to other objects. The following is my class.

global with sharing class MultiTicketRestController{
   global class RequestBody {
       global List<Ticket__c> tickets;

    global static List<Ticket__c> createBulk(MultiTicketRestController.RequestBody req) {

    for (Ticket__c ticket : req.tickets) {
      List<Activity__c> activity;
      List<Contact> contact;
      List<Payment__c> payment;
      List<User> user;
      List<User> guide;
      List<PriceType__c> priceType;  
      String activity_ID=ticket.Activity__c;

      activity=[SELECT id from Activity__c where ExternalAID__c=:Integer.valueOf(activity_ID)];
      String payment_ID=ticket.Payment__c;
      if(!String.isEmpty(payment_ID)) {
        payment=[SELECT id from Payment__c where External_Pay__c=:Integer.valueOf(payment_ID)];

      String contact_ID=ticket.Contact__c;
      contact=[SELECT id from  Contact where ExternalCID__c=:Integer.valueOf(contact_ID)];

      String pricetype_ID=ticket.PriceType__c;
      priceType=[SELECT id from PriceType__c 
               where External_PriceType_ID__c=:Integer.valueOf(pricetype_ID)];
      String user_ID=ticket.User__c;
      if(!String.isEmpty(user_ID)) {
       user=[SELECT id from User where External_UserID__c=:Integer.valueOf(user_ID)];

      String guide_ID=ticket.Guide__c;
      if(!String.isEmpty(guide_ID)) { 
        guide=[SELECT id from User where External_UserID__c=:Integer.valueOf(guide_ID)];

 insert req.tickets; 
 return req.tickets;


My input for this service is a JSON or an xml format - but I am having trouble writing a test class because for an referenced object a new object needs to be created invariably,the data type of which is not compatible with the controller input.

A detailed explaination is in the following thread: Test class for REST api to insert multiple records This thread is a background of the problem.

  • 1
    "the data type of which is not compatible with the controller input." ?? when you create a test class for a REST service, you mock records using DML and then invoke the REST class with suitable JSON. There is no controller involved – cropredy Feb 21 '16 at 0:03
  • @crop1645 Yes I am trying to do exactly that- but the problem comes in when my object has a reference to another object and I have to set a value for that reference. The link at the end of my question explains it. – goSF Feb 22 '16 at 2:47
  • 1
    First, make this problem simpler for us to understand - Ticket__c has various lookup fields that are populated by considering the JSON input and using that to locate the Sobjects by their externalId. So (1) Edit your post to just one of the relationships and remove all the rest; (2) I suspect you first need to mock the Actvities, Contacts, PriceType__c,User with external Ids , then construct the JSON as Keith C suggested to use those externalIds as part of the json payload. (3) I still don't understand the use of the word 'controller' - excise or explain further. – cropredy Feb 22 '16 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.