As a follow up to my previous post: REST API calls to 3rd Party (Pardot), multiple @future callouts
There are two responses I expect back when I attempt this Web Service callout to Pardot:
1) Success
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<api_key>6d800bfaab63274b1b908dacd8544936</api_key>
</rsp>
2) Failure:
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="fail" version="1.0">
<err code="15">Login failed</err>
</rsp>
The problem is if I attempt to parse the success and it was a failed attempt then I get a debug status error of "Attempt to de-reference a null object" So I'm wondering if there is a way to do a try() or if() statement that will look for fail or success and then return the other, something like this in theory:
/* Try to send the request */
try {
response = http.send(request);
System.debug('::RESPONSE::'+response.getBody());
/* Setup Document for parsing */
Dom.Document doc = response.getBodyDocument();
Dom.XMLNode results = doc.getRootElement();
//=======================================
NOTE: Currently if this next line fails
then everything stops so the if
statement never happens.
===========================================//
String api_key = results.getChildElement('api_key', null).getText();
if( api_key != '' ){
return api_key;
} else {
System.debug('::API REQUEST ERROR:: '+results.getChildElement('err', null).getText());
return '';
}
} catch(System.CalloutException e) {
System.debug('ERROR: '+ e);
return '';
}