3

The following is my Apex class to upsert the below input into salesforce.

INPUT

 {"req" : {
    "tickets": [
     {"External_TicketID__c" :"66",
     "Date_Time__c": "2015-09-13T22:30:00.000Z",
     "Activity__c":9,
     "Contact__c":0,
     "Quantity__c":3,
     "Payment__r":{"External_Pay__c":"2234","Amount__c":200.00,"TransactionDate__c":"2015-09-13T22:30:00.000Z"}},
     {"External_TicketID__c" :"77",
     "Date_Time__c": "2015-09-13T22:30:00.000Z",
     "Activity__c":9,
     "Quantity__c":3,
     "Contact__c":0,
     "Payment__r": {"External_Pay__c":"1134","Amount__c":200.00,"TransactionDate__c":"2015-09-13T22:30:00.000Z"}}]}}

APEX CLASS

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


    @HttpPost      
    global static List<Ticket__c> createBulk(MultiTicketPaymentRestController.RequestBody req) {
        for (Ticket__c ticket : req.tickets) {
        Ticket__c tick=new Ticket__c(External_TicketID__c=ticket.External_TicketID__c,Date_Time__c=ticket.Date_Time__c,Activity__c=ticket.Activity__c,Contact__c=ticket.Contact__c,Quantity__c=ticket.Quantity__c);
        System.debug(ticket.Payment__r.Amount__c); 
        System.debug(ticket.Payment__r.TransactionDate__c);
        System.debug(ticket.Payment__r.External_Pay__c);   
       Payment__c p = New Payment__c(External_Pay__c= ticket.Payment__r.External_Pay__c);  
        ticket.Payment__r=p;
         Payment__c p2 = New Payment__c(Amount__c=ticket.Payment__r.Amount__c,TransactionDate__c=ticket.Payment__r.TransactionDate__c,External_Pay__c= ticket.Payment__r.External_Pay__c);        
          Database.UpsertResult[] results = Database.upsert(new SObject[] {p2, tick});

         System.debug(ticket.Payment__r);
    }

        return req.tickets;

}
}

I am getting the following error
message: System.TypeException: DML on generic List only allowed for insert, update or delete Class.MultiTicketPaymentRestController.createBulk: line 18, column 1

Can i not implement an upsert for a coarse grained request like this?

1 Answer 1

3

For reasons I don't understand upsert does not support this pattern - as the error message explains insert, update and delete do.

Your present design will break when there are many tickets because you are doing an upsert per ticket and will run into the DML governor limit of a maximum of 150 DML operations per request/transaction. A way around that is to build a list of Ticket__c objects or Payment__c objects and upsert one full list then upsert the other full list. That means a maximum of 2 DML operations per request/transaction and allows you to use lists of the correct type and so avoid the upsert error you are seeing.

If the two objects are related to each other, this two step approach also allows you to get hold of the parent object ID values (after the parent upsert) and set them in the child objects foreign keys fields before you upsert the child objects and so relate the pairs of objects.

0

You must log in to answer this question.

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