1

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+'},';
            }

result image from firebase integration

4
  • Please edit the question to add the exact error message. I am guessing you simply need to define the list to be of the correct type for what you are trying. Also, please highlight exactly where the error happens.
    – Phil W
    Apr 17 at 7:25
  • Thanks @PhilW, basically it was Data type error - Method does not exist or incorrect signature: void add(Id) from the type List<Event> Apr 17 at 10:23
  • OK, so I assume you know how to resolve this for yourself, changing the list's type from List<Event> to List<Id>? Note that it is always best to edit the question to add more detail rather than provide that detail in comments.
    – Phil W
    Apr 17 at 10:39
  • yes @PhilW I understand, I have updated my code. Thanks for suggesting. Apr 17 at 16:13

1 Answer 1

3

Since your data structure is holding event records, your final JSON looks something like:

{
  "005...": [
   { "Subject": "Event 1", ... }
   { "Subject": "Event 2", ... }
  ],
  "005...": [
    { ... }
  ]
]

Your tool is showing the index, because each element is an event record; if you use the expand arrow next to an index, you'll see all the fields and their data.

The Id of the user is the key, shown as 0051D00000AMkwjQAD in your screenshot. Each entry below is an event owned by that user. In other words, the index shown is just the tool's way of saying that you have an array of objects, and for each object, there are more data values. You can expand one of those indexes to see the data there.


By the way, you'd get the same JSON output without the strange for loop by just serializing the entire map.

String body='{';
...
for(id ownerId : keySet)
{
    body=body+'"'+Ownerid+'" :'+JSON.serialize(UserEvent2.get(ownerId))+',';
}
body=body.removeEnd(',');
body=body+'}';

Can be written as:

body = JSON.serialize(UserEvent2);

List<event> tempEventList = null;

Variables are null by default, so you can just write:

List<event> tempEventList;
1
  • Thanks for your answer @sfdcfox. It was helpful. The issue is resolved. Apr 17 at 16:14

You must log in to answer this question.

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