0

Hello I wrote a web service that updates that status of each Master record, called Ticket__c. The service updates perfectly. However, I just realized that each master record to update may have several detail records, TicketTasks__c that will also need updating. So if I am updating 2 master records, each master record might have 2 tasks associated with them. My code does the first part but not the second part of updating the child records. My initial code is below:

@HttpPut
    global static List<Ticket__c> updateTicket(){
        RestRequest request = RestContext.request; 
        String jsonString = request.requestBody.toString(); 
        
        List<Status> ticketStatus = (List<Status>)JSON.deserialize(jsonString, List<Status>.class); 

        Map<String, Status> MapticketNumber = new Map<String, Status>();
        
        List<Ticket__c> ticketstoUpdate = new List<Ticket__c>();
        
        for(Status ss : ticketStatus){
            MapticketNumber.put(ss.ticketName, ss);
        }
        
        List<Ticket__c> st = [SELECT Name, Status__c, Work_Notes__c FROM Service_Ticket__c WHERE Name IN: MapticketNumber.keySet()];

        for(Ticket__c s : st){
            s.Status__c = MapticketNumber.get(s.Name).Status;
            s.Work_Notes__c = MapticketNumber.get(s.Name).workNotes;
            ticketstoUpdate.add(s);
        }
        
        try{
            update ticketstoUpdate;  
        }
        catch(DMLException e) {
            system.debug('***************************************** ' + e.getMessage()); 
            system.debug('***************************************** ' + e.getStackTraceString());
        } 
 
        return ticketstoUpdate;
        
    }
  global class Status {
        public String ticketName;
        public String Status;
        public String workNotes;
    }  
}

So the detail record has a field called StatusState__c and an AgentNotes__c that will need to be updated. I am not sure how to modify my code to include the update to the child records. I modified my constructor as the following:

global class Status {
            public String ticketName;
            public String Status;
            public String workNotes;
            public String TaskStatus //for the child record just added
            public String TaskworkNotes //for the child record just added
} 

Then the query in the code I modified it to:

List<Ticket__c> st = [SELECT Name, Status__c, Work_Notes__c, (SELECT Id, Name, Assigned_To__c, StatusState__c, AgentNotes__c FROM Ticket_Tasks__r) FROM Ticket__c WHERE Name IN: MapticketNumber.keySet()];

I also changed the for loop where I am adding key/value to my Map from

for(Status ss : ticketStatus){
            MapticketNumber.put(ss.ticketName, ss);
}

to the following:

for(Status ss : ticketStatus){
            if(MapticketNumber.containsKey(ss.ticketName)) {
                MapticketNumber.get(ss.ticketName).add(ss); 
            } else {
                MapticketNumber.put(ss.ticketName, new List<Status>{ss});
            }
        }

I am not sure if the above changes are correct, but assuming they are, I am not sure what I should do next to update each child/detail record as well in my web service. Any help would be greatly appreciated. Thank you

1 Answer 1

0

If the new query is working fine then you can loop over the detail records and set their value in the existing loop.

e.g;

list<TicketTasks__c> ticketTaskToUpdate = new list<TicketTasks__c>();
...
for(Ticket__c s : st){
    s.Status__c = MapticketNumber.get(s.Name).Status;
    s.Work_Notes__c = MapticketNumber.get(s.Name).workNotes;
    ticketstoUpdate.add(s);
    
    for(TicketTasks__c  ticketTask : s.Ticket_Tasks__r){
        ticketTask.StatusState__c  = MapticketNumber.get(s.Name).TaskStatus;
        ticketTask.AgentNotes__c = MapticketNumber.get(s.Name).TaskworkNotes;
        ticketTaskToUpdate.adD(ticketTask);
    }
}

...

update ticketTaskToUpdate;
2
  • Thank you so much! Out of curioisty, lets say that a parent record has 4 detail records but I only wanted to updated 2 of them. How would I modify the code to do that? The detail record has a name as well. Sep 1, 2020 at 3:19
  • If possible, write filter in the query after the Ticket_Tasks__r, for example: where StatusStat__c = 'XXXX', if not possible inside query you can write the filter logic in the loop before setting the ticketTask fields
    – Liron C
    Sep 1, 2020 at 8:43

You must log in to answer this question.

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