1

I have an apex class(REST endpoint) for inserting multiple records into a salesforce object

@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;
    }

}

I was trying to write a test class for this. My request is in the form of an XML or a JSON. So I am not able create ticket objects to test my class because the controller parses the raw json/xml input and puts it into ticket objects. How can I pass in a request to test this class. I am stuck!!

Sample request for endpoint:

{"req" : {"tickets": [
{
"External_TicketID__c" :"666",
"Date_Time__c: "2015-09-13T22:30:00.000Z",
"Payment__c":,
"Contact__c" :"3",
"Activity__c" :333,
"PriceType__c": 3,
"User__c":3,
"Guide__c":4,
"Price__c" :"12.50",
"Fees__c" :340,
"Status__c": "sale",
"Quantity__c": "2",
"Last_Transaction_No__c":123
},
{
"External_TicketID__c" :"777",
"Date_Time__c: "2015-09-13T22:30:00.000Z",
"Payment__c":,
"Contact__c" :"3",
"Activity__c" :333,
"PriceType__c": 3,
"User__c":3,
"Guide__c":,
"Price__c" :"12.50",
"Fees__c" :340,
"Status__c": "sale",
"Quantity__c": "2",
"Last_Transaction_No__c":123
}
]}}
2

Note that you can just call the @HttpPostmethod with the appropriate object structure (with the objects new'ed in the test class) as the aim is to test your code not the platform's JSON parsing code.

But if you really want to start from JSON for some reason (say stored in static resources or hard coded as Apex strings in your test class), use JSON.deserialize to turn the JSON into objects and then call the @HttpPost method in your test code:

MultiTicketRestController.RequestBody req = (MultiTicketRestController.RequestBody) JSON.deserialize(jsonString, MultiTicketRestController.RequestBody.class);
List<Ticket__c> tickets = MultiTicketRestController.createBulk(req);
  • The problem is that i have reference to other objects in my ticket object and the values for those references come in as the external IDs. In my controller class I convert those externalIDs into the actual referenced object IDs and insert the ticket object. If i create an appropriate object structure like you said they result in data type compatability issues . For eg- "Activity__c" :333 when it reaches the controller class from the test class as the object ID say "01p3B0000008tne" gives an error because the controller tries to pull the ID for the already converted object ID – goSF Feb 18 '16 at 15:14
  • when i try to create a ticket object in the test class Ticket__c ticket1=new Ticket__c(); ticket1.Contact__c=3(from the input above); -----> this is not possible. – goSF Feb 18 '16 at 15:16
  • @goSF Tests are normally isolated from any data already in the org so your test will have to insert any objects that createBulk references and create the matching MultiTicketRestController.RequestBody, – Keith C Feb 18 '16 at 15:22
  • Yes, I was trying to do that, but as I explained above- If i create the object in the test class, when it goes to the controller (in my question), it tries to convert into the referenced object IDs(but they are already converted when i create an object in my test class) – goSF Feb 18 '16 at 15:26
  • @goSF So it does sound like may need to go the JSON route then. Have you got the class to work outside a test i.e. posting JSON with the external ID values in it? As values like '3' are not valid as ID values I'm a bit surprised the deserialization doesn't blow up. – Keith C Feb 18 '16 at 15:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.