function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Sharma 527Abhishek Sharma 527 

to get id in the place of index number in for loop

Hello Expert, 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..). Please help
Best Answer chosen by Abhishek Sharma 527
SwethaSwetha (Salesforce Developers) 
HI Abhishek,
Can you try replacing OwnerId with OwnerId.Id in the following lines so that the ID of the owner is used instead of the index number.:
 
request.setEndpoint('https://test/Appointments.json');
request.setMethod('PATCH');
for(id ownerId : keySet)
{
    body=body+'"'+Ownerid.Id+'" :'+JSON.serialize(UserEvent2.get(ownerId))+',';
}

If this information helps, please mark the answer as best. Thank you

All Answers

Abhishek Sharma 527Abhishek Sharma 527
I'm getting this as result in firebase User-added image
SwethaSwetha (Salesforce Developers) 
HI Abhishek,
Can you try replacing OwnerId with OwnerId.Id in the following lines so that the ID of the owner is used instead of the index number.:
 
request.setEndpoint('https://test/Appointments.json');
request.setMethod('PATCH');
for(id ownerId : keySet)
{
    body=body+'"'+Ownerid.Id+'" :'+JSON.serialize(UserEvent2.get(ownerId))+',';
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer