I have a method on which I'm running loop for showing all records of particular user(account). I'm getting index number in result. I want to show id of user instead of index number.
public static void methodEventData2(){
try {
Map<id,list<event>> UserEvent2 = new Map<id,list<event>>();
List<event> tempEventList = null;
List<Event> newEvents = [Select Id, Subject, WhatId, WhoId, Who.Name, EndDate, EndDateTime,Start_Date_Time__c, OwnerId,
ActivityDate, ActivityDateTime, Facilities__c,Facilities__r.Name, Services__c, Appointment_Status__c, Description,
DC_Session_End_Time__c,DC_Session_Last_Start_Time__c,DC_Session_Run_Time__c,DC_Session_Start_Time__c,
DC_Session_Status__c, End_Time__c, IsRecurrence, Sup_Provider__c, DOSSession__c, What.Name,Owner.Name From Event WHERE
whoid != null order by OwnerId limit 200];
for(event et : newEvents){
if(!UserEvent2.isEmpty() || UserEvent2 !=null){
if(UserEvent2.get(et.OwnerId) != null){
tempEventList.add(et);
UserEvent2.put(et.OwnerId, tempEventList);
}
else{
tempEventList = new List<event>();
tempEventList.add(et);
UserEvent2.put(et.OwnerId, tempEventList);
}
}
}
Set<Id> keySet = UserEvent2.keySet();
String body='{';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://test/Appointments.json');
request.setMethod('PATCH');
for(id ownerId : keySet)
{
body=body+'"'+Ownerid+'" :'+JSON.serialize(UserEvent2.get(ownerId))+',';
}
body=body.removeEnd(',');
body=body+'}';
request.setBody(body);
HttpResponse response = http.send(request);
when I'm writing tempEventList.add(et.OwnerId), it's showing error. Can anyone please guide as to how I can print ownerId in result instead of index number (0,1,2..) This issue was fixed by changing my code to -
for(Id ownerId: UserEvent2.keySet()){
body=body+'"'+ownerId+'" : {';
for(Event e: UserEvent2.get(ownerId)){
body=body+'"'+e.Id+'" :'+JSON.serialize(e)+',';
}
body=body.removeEnd(',');
body=body+'},';
}
List<Event>
toList<Id>
? Note that it is always best to edit the question to add more detail rather than provide that detail in comments.