1

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.

@RestResource(urlMapping='/MultiTicket/*')
global with sharing class MultiTicketRestController{
   global class RequestBody {
       global List<Ticket__c> tickets;
   }

    @HttpPost   
    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)];
      ticket.Activity__c=activity.get(0).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)];
        ticket.Payment__c=payment.get(0).ID;
      }
      else{ticket.Payment__c=null;}

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

      String pricetype_ID=ticket.PriceType__c;
      priceType=[SELECT id from PriceType__c 
               where External_PriceType_ID__c=:Integer.valueOf(pricetype_ID)];
      ticket.PriceType__c=priceType.get(0).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)];
       ticket.User__c=user.get(0).ID;}
      else{ticket.User__c=null;}

      String guide_ID=ticket.Guide__c;
      if(!String.isEmpty(guide_ID)) { 
        guide=[SELECT id from User where External_UserID__c=:Integer.valueOf(guide_ID)];
        ticket.Guide__c=guide.get(0).ID;}
      else{ticket.Guide__c=null;}
      System.Debug(String.valueOf(activity_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.