0

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

My guess is because you are using the same ticket name for the key in the map.

"ticketName": "XYZ123"

A map needs to have a unique key for each key-value pair.

Trying using this logic and data struct:

@HttpPost
global static List<Ticket_Task__c> createTicketTask(){

   RestRequest request = RestContext.request; 
   String jsonString = request.requestBody.toString();
   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});
      }
   }
    
   for(Ticket__c st : [SELECT Id, Name 
                       FROM Ticket__c 
                       WHERE Name IN: ticketNameWithTasks.keySet()]){

      if(ticketNameWithTasks.containsKey(st.Name)) {
         for(Tasks t : ticketNameWithTasks.get(st.Name)) {

            Ticket_Task__c createTask = new Ticket_Task__c();
            createTask.Ticket__c = st.Id;
            createTask.Name = t.taskNumber;
            createTask.Assigned_To__c = t.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;
    }

}
| improve this answer | |
  • are you suggesting something like what I have pasted for code titled "Edited version"? When I do that I am getting errors "Variable does not exist taskNumber/assignedTo. I am not really sure what your code is doing and where t put it. – user3688231 Aug 27 at 2:53
  • @user3688231 look at my edits – Tyler Zika Aug 27 at 3:04
  • Thank you so much!!! – user3688231 Aug 27 at 3:51

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.