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?