I have a Master-Detail object in my org, Ticket__c(parent/master) and Ticket_Task__c (detail/child). I created a Web Service whose PUT method is successfully creating new tasks for a given ticket number. The JSON response for the put is in this format:
[{
"ticketName": "XYZ123",
"taskNumber": "1265659",
"assignedTo": "bLA BLA!"
},
{
"ticketName": "ZZZ456",
"taskNumber": "546",
"assignedTo": "GSDSD"
},
{
"ticketName": "SDSF67",
"taskNumber": "767",
"assignedTo": "SDSFF"
}
]
The problem comes when I want to create multiple tasks for the same ticketName. It only creates the last one. In other words if I want to create a task for the following JSON, then only the last one gets created:
[{
"ticketName": "XYZ123",
"taskNumber": "1265659",
"assignedTo": "bLA BLA!"
},
{
"ticketName": "XYZ123",
"taskNumber": "546",
"assignedTo": "GSDSD"
}
]
Can someone explain to me why both tasks (2 details) for the Ticket (master) is not being created? My Apex code is below:
@HttpPost
global static List<Ticket_Task__c> createTicketTask(){
RestRequest request = RestContext.request;
String jsonString = request.requestBody.toString();
Map<String, Tasks> MapTicket = new Map<String, Tasks>();
List<Tasks> tasklist = (List<Tasks>)JSON.deserialize(jsonString, List<Tasks>.class);
List<Ticket_Task__c> taskstoCreate = new List<Ticket_Task__c>();
for(Tasks task : tasklist){
MapTicket.put(task.ticketName, task);
}
List<Ticket__c> sticket = [SELECT Id, Name FROM Ticket__c WHERE Name IN: MapTicket.keySet()];
for(Ticket__c st : sticket){
Ticket_Task__c createTask = new Ticket_Task__c();
createTask.Ticket__c = st.Id;
createTask.Name = MapTicket.get(st.Name).taskNumber;
createTask.Assigned_To__c = MapTicket.get(st.Name).assignedTo;
taskstoCreate.add(createTask);
}
try{
insert taskstoCreate;
}
catch(DMLException e) {
system.debug('***************************************** ' + e.getMessage());
system.debug('***************************************** ' + e.getStackTraceString());
}
return taskstoCreate;
}
global class Tasks {
public String ticketName;
public String taskNumber;
public String assignedTo;
}
Edited version:
@HttpPost
global static List<Ticket_Task__c> createTicketTask(){
RestRequest request = RestContext.request;
String jsonString = request.requestBody.toString();
//Map<String, Tasks> MapTicket = new Map<String, Tasks>();
Map<String, List<Tasks>> ticketNameWithTasks = new Map<String, List<Tasks>>();
List<Tasks> tasklist = (List<Tasks>)JSON.deserialize(jsonString, List<Tasks>.class);
List<Ticket_Task__c> taskstoCreate = new List<Ticket_Task__c>();
for(Tasks task : tasklist){
if(ticketNameWithTasks.containsKey(task.ticketName)) {
ticketNameWithTasks.get(task.ticketName).add(task);
} else {
ticketNameWithTasks.put(task.ticketName, new List<Tasks>{task});
}
//MapTicket.put(task.ticketName, task);
}
List<Ticket__c> sticket = [SELECT Id, Name FROM Ticket__c WHERE Name IN: ticketNameWithTasks.keySet()];
for(Ticket__c st : sticket){
Ticket_Task__c createTask = new Ticket_Task__c();
createTask.Ticket__c = st.Id;
createTask.Name = ticketNameWithTasks.get(st.Name).taskNumber;
createTask.Assigned_To__c = ticketNameWithTasks.get(st.Name).assignedTo;
taskstoCreate.add(createTask);
}
try{
insert taskstoCreate;
}
catch(DMLException e) {
system.debug('***************************************** ' + e.getMessage());
system.debug('***************************************** ' + e.getStackTraceString());
}
return taskstoCreate;
}