I insert object X, do a web call out, and then take the data I get back from the call out to update object X.
I found that you can't do DML before a webcallout so I broke this up into two methods and use action function to call the webservice method on the completion of the first method.
Where I am now stuck is on the update of the object's fields. I believe there to be another rule about DML after web call outs that I am unaware of. As you can see in the below code I update the object with the values I got back from the web service. But I am getting back System.NullPointerException: Attempt to de-reference a null object
as soon as I try and save a value into the object's field. I know the values are not NULL. It is getting stuck on: crs.Bankcard_Score__c = IDScore1
and all the following field updates.
I have tried placing the update in the same method as well as a separated out method with a third action function. Is there a good workaround to this or something that I am missing with updates and web services?
VFP:
<apex:actionFunction name="crsUpdate_APEX" action="{!pullIDAnalyticsReportAPEX}" status="spinnerStatus1" oncomplete="CallWebService_APEX();"/>
<apex:actionFunction name="CallWebService_APEX" action="{!CallWebService}" status="spinnerStatus1" rerender="IDoldPullreportbutton1, refreshOutPutPannel1"/>
....
<apex:commandLink value="Pull Report" onclick="crsUpdate_APEX(); removeButtonAndText(); return false;" style="text-decoration: none; text-align: center; position:relative; color: #333;"/>
....
APEX:
public PageReference pullIDAnalyticsReportAPEX() {
currentPgId = ApexPages.currentPage().getParameters().get('id');
guarantorId1 = [SELECT PG1__c FROM Credit_Review__c WHERE Id = :currentPgId].PG1__c;
crs = new Consumer_Risk_Summary__c(Position__c = '1');//creating a new object
insert crs;
crsid = crs.Id;
return null;
}
public PageReference CallWebService(){
if(getAccounts() != NULL) {
getAccounts(); //WEBSERVICE CALL OUT
errorMessage();
saveDateToString();
crs.Bankcard_Score__c = IDScore1; //THIS IS WHERE IT ERRORS OUT, UNABLE TO UPDATE CRS VALUES
crs.Comply360_Grade__c = Comply360Grade1;
crs.Credit_Review__c = thecurrentPageId;
crs.Transaction_Date__c = newdate;
crs.Guarantor__c = guarantorId1;
crs.Error_Message__c = ErrorMessage1;
crs.Error__c = ApiErrorCode;
List<Credit_Review__c> creditreview = [SELECT PG1__c, PG1_Cons_Risk_Summary__c FROM Credit_Review__c WHERE Id = :currentPgId];
for (Credit_Review__c credRev : creditreview) {
credRev.PG1_Cons_Risk_Summary__c = crs.Id;
system.debug('got into the current credit review loop');
update credRev;
}
if (crs.Error__c == 'null' || (crs.Bankcard_Score__c != 'null' && crs.Bankcard_Score__c != NULL)) {
crs.Status__c = 'Completed';
}
update crs;
}
else {
genericError = 'There has been an error in your organization. Please contact your system administrator for assistance.';
}
return null;
}
public string getAccounts() {
Integer i = 0;
system.debug('got into getAccounts: ' + (i = i + 1));
HttpRequest req = new HttpRequest();
endpoint = IDAnalyticsEndPoint__c.getInstance();
url1 = endpoint.IDAnalyticsEndpointUrl__c;
req.setEndpoint(url1);
Http h = new Http();
req.setMethod('POST');
req.setBody(generateJSON());
req.setHeader('Content-Type', 'application/json');
system.debug('this is the setHeader');
req.setTimeout(120000);
system.debug('this is the setTimeout');
string d = getAuth();
string e = 'token ';
string f = e + d;
system.debug('this is the f: ' + f);
req.setheader('Authorization', f);
try {
res = h.send(req);
jsonStr = res.getBody();
errorMessage();
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'IDScore')) {
parser.nextToken();
IDScore1 = parser.getText();
}
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Comply360Grade')) {
parser.nextToken();
Comply360Grade1 = parser.getText();
}
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'ApiErrorCode')) {
parser.nextToken();
ApiErrorCode = parser.getText();
}
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'ErrorMessage')) {
parser.nextToken();
ErrorMessage1 = parser.getText();
}
}
return jsonStr;
}
catch (System.CalloutException z) {
errorMessage();
return null;
}
}
I found most of my data from here: WebService and DML
crs
is a static variable. When I removestatic
modifier, I am able to update thecrs
object. This is great but does not entirely help because I need that variable to be static to persist the data on the VFP. So I am still stuck. – Olivia May 4 '17 at 0:13