Apex call out has an xml response.How can I map the fields in Salesforce to the response I receieved. I could map the fields like clean etc. But how could I map fields like make,model, year relatively.
Assume the fields are : Make__c ,Model__c etc.... Considering the XML response.
XML response is:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<vin>xxxxxxxxxxx</vin>
<id>xxxxxxxxxxx</id>
<date>2018-02-08 05:02:15 PST</date>
<specs>
<VIN />
*<Year>2011</Year>
<Make>Chevrolet</Make>
<Model>Suburban</Model>*
<Trim>LS 1500</Trim>
</specs>
<clean>0</clean>
<success>1</success>
<error />
</result>
public PageReference doVinExtract()
{
HttpRequest httpRequest = new HttpRequest();
httpRequest.setEndpoint('https:/VinProviderxxxx.php');
String body = 'id=01123999999198&key=xxxxxxx&vin=1GNSKHE34BRxxxxxxx&mode=test&user=vxxxxxx&pass=slll4250&format=xml';
httpRequest.setBody(body);
httpRequest.setMethod('GET');
Http htt = new hTTP();
HttpResponse httpRe = htt.send(httpRequest);
system.debug(httpRe.geTbody());
String re = httpRe.geTbody();
Dom.Document doc = httpRe.getBodyDocument();
Dom.XMLNode address = doc.getRootElement();
Dom.XMLNode specs = address.getChildElement('specs', null);
//String clean= address.getChildElement('clean', null).getText();
String vinDate= address.getChildElement('date', null).getText();
String Year = specs.getChildElement('Year', null).getText();
String Make = specs.getChildElement('Make', null).getText();
String Model = specs.getChildElement('Model', null).getText();
String FuelType = specs.getChildElement('FuelType', null).getText();
// print out specific elements
for(Dom.XMLNode child : address.getChildElements()) {
if(child.getName() == 'specs' ){
System.debug('******'+child.getText());
}
System.debug('address: ' + address);
System.debug('specs : ' + specs );
system.debug('result'+ re);
}
Vehicle__c ve =[select Name, Id, Year__c, VinDate__c, Fuel_Type__c from Vehicle__c where id =: vehicleId];
//ve.Title_Status__c=clean;
ve.VinDate__c=vinDate;
ve.Make__c= Make;
ve.Model__c= Model;
ve.Year__c=Year;
ve.Fuel_Type__c =FuelType;
update ve;
PageReference nextPage = new PageReference('xxxxxxxxx'+ vehicleId);
return nextPage;
}
}
The error is :
System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!doDirectPayment}' in component in page vinauditpage: Class.VinAuditCntlr.doDirectPayment: line 44, column 1 Class.cxxxCntlr.doVinExtract: line 44, column 1
Sometimes:It shows line 47 Column 1
Is this because of the response might not contain the fields required from salesforce?
I would also like to paste the debug log to have a comprehensive understanding.
10:02:34.0 (9170761)|VARIABLE_SCOPE_BEGIN|[28]|httpRequest|System.HttpRequest|true|false 10:02:34.0 (9259169)|VARIABLE_ASSIGNMENT|[28]|httpRequest|"System.HttpRequest[Endpoint=null, Method=null]"|0x3366c69d 10:02:34.0 (9269999)|STATEMENT_EXECUTE|[29] 10:02:34.0 (9274562)|HEAP_ALLOCATE|[29]|Bytes:43 10:02:34.0 (9345602)|STATEMENT_EXECUTE|[30] 10:02:34.0 (9350259)|HEAP_ALLOCATE|[30]|Bytes:37 10:02:34.0 (9468188)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"vehicle":"0x73268d7c","vehicleId":"a1Z350000001SO0EAM","vinNumber":"1FTCF15N5HLA06223"}|0x354564a4 10:02:34.0 (9491272)|HEAP_ALLOCATE|[30]|Bytes:66 10:02:34.0 (9506206)|HEAP_ALLOCATE|[30]|Bytes:120 10:02:34.0 (9513814)|VARIABLE_SCOPE_BEGIN|[30]|body|String|false|false 10:02:34.0 (9527933)|VARIABLE_ASSIGNMENT|[30]|body|"id=0112399992&key=KY (100 more) ..." 10:02:34.0 (9533681)|STATEMENT_EXECUTE|[31] 10:02:34.0 (9571678)|STATEMENT_EXECUTE|[32] 10:02:34.0 (9576000)|HEAP_ALLOCATE|[32]|Bytes:3 10:02:34.0 (9608165)|STATEMENT_EXECUTE|[33] 10:02:34.0 (9625502)|VARIABLE_SCOPE_BEGIN|[33]|htt|System.Http|true|false 10:02:34.0 (9703491)|VARIABLE_ASSIGNMENT|[33]|htt|"System.Http[]"|0x2753a4d2 10:02:34.0 (9714121)|STATEMENT_EXECUTE|[34] 10:02:34.0 (9841060)|CALLOUT_REQUEST|[34]|System.HttpRequest[Endpoint=https://api-ssl.vinaudit.com/pullreport.php, Method=GET] 10:02:34.0 (269995528)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:548 10:02:34.0 (270179937)|CALLOUT_RESPONSE|[34]|System.HttpResponse[Status=OK, StatusCode=200] 10:02:34.0 (270215805)|HEAP_ALLOCATE|[34]|Bytes:137 10:02:34.0 (270257158)|VARIABLE_SCOPE_BEGIN|[34]|httpRe|System.HttpResponse|true|false 10:02:34.0 (270358223)|VARIABLE_ASSIGNMENT|[34]|httpRe|"System.HttpResponse[Status=OK, StatusCode=200]"|0x282fcad7 10:02:34.0 (270371795)|STATEMENT_EXECUTE|[35] 10:02:34.0 (270381135)|HEAP_ALLOCATE|[35]|Bytes:14 10:02:34.0 (270481694)|HEAP_ALLOCATE|[35]|Bytes:131 10:02:34.0 (270510107)|HEAP_ALLOCATE|[35]|Bytes:145 10:02:34.0 (270538879)|USER_DEBUG|[35]|DEBUG|++++++response 1FTCF15N5HLA062230vin_mismatch 10:02:34.0 (270551659)|STATEMENT_EXECUTE|[36] 10:02:34.0 (270581910)|HEAP_ALLOCATE|[36]|Bytes:131 10:02:34.0 (270596218)|VARIABLE_SCOPE_BEGIN|[36]|re|String|false|false 10:02:34.0 (270615631)|VARIABLE_ASSIGNMENT|[36]|re|"\u003C?xml version=\"1.0\" (111 more) ..." 10:02:34.0 (270622063)|STATEMENT_EXECUTE|[38] 10:02:34.0 (270721993)|VARIABLE_SCOPE_BEGIN|[38]|doc|dom.Document|true|false 10:02:34.0 (270793869)|VARIABLE_ASSIGNMENT|[38]|doc|"Document[]"|0x27167566 10:02:34.0 (270804484)|STATEMENT_EXECUTE|[39] 10:02:34.0 (270846500)|VARIABLE_SCOPE_BEGIN|[39]|address|dom.XmlNode|true|false 10:02:34.0 (270927026)|VARIABLE_ASSIGNMENT|[39]|address|"XMLNode[ELEMENT,result,null,null,null,[XMLNode[ELEMENT,vin,null,null,null,[XMLNode[TEXT,null,null,null,null,null,1FTCF15N5HLA06223,]],null,], XMLNode[ELEMENT,success,null,null,null,[XMLNode[TEXT,null,null,null,null,null,0,]],null,], XMLNode[ELEMENT,error,null,null,null,[XMLNode[TEXT,null,null,null,null,null,vin_mismatch,]],null,]],null,]"|0x644ac164 10:02:34.0 (270942061)|STATEMENT_EXECUTE|[40] 10:02:34.0 (270947475)|HEAP_ALLOCATE|[40]|Bytes:5 10:02:34.0 (271005113)|VARIABLE_SCOPE_BEGIN|[40]|specs|dom.XmlNode|true|false 10:02:34.0 (271020975)|VARIABLE_ASSIGNMENT|[40]|specs|null| 10:02:34.0 (271026661)|STATEMENT_EXECUTE|[44] 10:02:34.0 (271031109)|HEAP_ALLOCATE|[44]|Bytes:4 10:02:34.0 (271152871)|HEAP_ALLOCATE|[44]|Bytes:41 10:02:34.0 (271261662)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object Class.xxx.doVinExtract: line 44, column 1
How to avoid these errors and proceed dynamically with the code that returns "null" and points out the response about the field that is not with Salesforce.