0

Trying to retrieve the results from query to map inside JSONgenerator but not sure how to link my results of query to the map inside for loop. Please look into my code below and suggest where I am getting this wrong. Thanks

Map<Id, Case> cMap = new Map<Id, Case>([Select Id, accountId From Case Where ID IN :caseIds]);

list<Id> assetAccounts = new list<Id>();

JSONGenerator gen = JSON.createGenerator(true);

for(Id c: CaseIds) {
   gen.writeStartObject();
   if(caseMap.get(c).accountId != null) {
      assetAccounts.add(caseMap.get(caseId).accountId);
      Map<Id, string> phoneNos = new Map<Id, string>();
   }
   gen.writeEndObject();
}
system.debug(gen.getAsString());
for (Asset asset : [SELECT Id, AccountId FROM Asset WHERE AccountId IN :assetAccounts]) {
        if(asset.Description == 'TN'){
            phoneNos.add(asset.id, asset.SerialNumber);
        }
    }
1

As @Goki mentioned, the variable c is a case ID and not a case object. If you want to put the case ID in the JSON string you can do this:

for(Id c: CaseIds) 
{  
   gen.writeStartObject();
   gen.writeStringField('caseId', c);  
   gen.writeEndObject();
}
| improve this answer | |
  • Bachovski, I forgot to edit if condition while pasting here. Corrected it now. Can you suggest how to get the values of asset query (phoneNos) inside JSON as I am getting error "variable doesnot exist: phoneNos". Thanks – sf_user Aug 6 '15 at 4:00
  • 1
    Move this line Map<Id, string> phoneNos = new Map<Id, string>(); before the for loop. – Boris Bachovski Aug 6 '15 at 4:11
  • @pmvsdt Guess you should compile your code and see the errors first. You have declared phoneNos in first for loop and accessing it in second for loop. This will throw error variable doesn't exist. – Goki Aug 6 '15 at 4:31
  • Goki, As Bachovski suggested I made a mistake by writing map<....> = new map<...> inside my for loop. Moving that outside for loop resolved my issue. Thanks – sf_user Aug 6 '15 at 4:37
2

You don't really explain what you are trying to get into the JSON. But I suggest that JSONGenerator is best reserved for when you want very detailed control of the conversion to Apex. Generally it is much easier to put the data in maps and lists either with or without simple Apex classes whose fields represent the types and names. Then when something changes, you typically won't have to change the serialisation code.

See JSON.serialize and JSON2apex.

| improve this answer | |
  • 1
    Hi Keith, I am using JSON to post these values to a web service. Your reference links and suggestions are definitely helpful for me. Thanks for posting them. – sf_user Aug 6 '15 at 4:03
2

I guess the the variable c in for loop is just an Id, but you are referencing it to get an accountId. That is wrong.

| improve this answer | |
  • Hi Goki, corrected the code now. I incorrectly pasted the if condition initially – sf_user Aug 6 '15 at 4:06

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.