+ Start a Discussion
Rakesh ChattyRakesh Chatty 

How to get elements values from Http response

Hi All,

 

string templateId = '00XN0000002LbV8MAK';
string domainUrl = URL.getSalesforceBaseUrl().toExternalForm();
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(domainUrl+'/services/data/v51.0/tooling/query/?q=select+id,SenderType,templateId+from+WorkflowAlert+where+templateId=\''+templateId+'\'');
req.setMethod('GET'); 
Http h = new Http();
HttpResponse res = h.send(req);
system.debug(res.getBody());
Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
Object data1 = deserialized.get('records');
System.debug(deserialized.get('records'));
System.debug(deserialized.get('size'));

 

res.getBody(); -> {"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"WorkflowAlert","records":[{"attributes":{"type":"WorkflowAlert","url":"/services/data/v51.0/tooling/sobjects/WorkflowAlert/01WN00000005esGMAQ"},"Id":"01WN00000005esGMAQ","SenderType":"CurrentUser","TemplateId":"00XN0000002LbV8MAK"}]}

 

Im trying to get ,"SenderType":"CurrentUser"  value but values is not returning: 

System.debug(deserialized.get('records')); --> [{"TemplateId":"00XN0000002LbV8MAK","SenderType":"CurrentUser","Id":"01WN00000005esGMAQ","attributes":{"url":"/services/data/v51.0/tooling/sobjects/WorkflowAlert/01WN00000005esGMAQ","type":"WorkflowAlert"}}]

 

How to get SenderType Value?

 

Thanks in Advance,

Best Answer chosen by Rakesh Chatty
Abdul KhatriAbdul Khatri
Hi Rakesh

Please use the below code
Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> m2 = (List<Object>) deserialized.get('records');
Map<String, Object> m3 = (Map<String, Object>) m2[0];
system.debug(m3.get('SenderType'));

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take references from the below code.

Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Map<String, Object> m2 = (Map<String, Object>) m.get('records');
 System.debug('SenderType:::'+m2.get('SenderType'));
for(String s:m2.keyset()) {
     System.debug(m2.get(s));
}

Please let me know it is working or not?

Please mark it as the Best Answer so that other people would take references from it.

Thank You

Rakesh ChattyRakesh Chatty

Hi Tirupathi, 

Thank you!

I tried your approach but it is thowing an error.

Line: 12, Column: 1
System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

 

Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Map<String, Object> m2 = (Map<String, Object>) m.get('records'); // error
 System.debug('SenderType:::'+m2.get('SenderType'));
for(String s:m2.keyset()) {
     System.debug(m2.get(s));
}
Abdul KhatriAbdul Khatri
Hi Rakesh

Please use the below code
Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> m2 = (List<Object>) deserialized.get('records');
Map<String, Object> m3 = (Map<String, Object>) m2[0];
system.debug(m3.get('SenderType'));
This was selected as the best answer
Rakesh ChattyRakesh Chatty

Thank You Abdul, It made simple, before i was using this code to get values.

 

while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
                    String fieldName = parser.getText();
                    parser.nextToken();
                    String fieldValue=parser.getText();
                    
                    if(fieldName == 'Sendertype') {
                        workflowEmailAlertSendertype.add(fieldValue);
                        system.debug('f value :'+fieldvalue);
                        sendertype = fieldValue;
                    }
                }
            }