I'm trying to run this test and it keeps failing. The error I'm getting is "System.QueryException: unexpected token: 'null'" so it's related to my lstContacts SOQL query, but I can't figure out what's going. Works fine when I'm running the callout in the execute anonymous window and elsewhere.
Callout:
public class FilastCallout implements Schedulable {
public void execute(SchedulableContext ctx) {
makePostCallout();
}
@future(callout=true) public static void makePostCallout() {
// Get custom settings
Filast_Settings__c fu = Filast_Settings__c.getOrgDefaults();
List<Contact> lstContacts = Database.query('SELECT '+fu.Contact_First_Name__c+','+fu.Contact_Last_Name__c+','+fu.Contact_Email__c+',AccountId,Account.'+fu.Account_Name__c+' FROM Contact WHERE '+fu.Contact_Email__c+' !=null AND '+fu.Contact_First_Name__c+' !=null');
// Transform list ot json and add advanced settings
String jsonS = JSON.serialize(new Map<String, Object> {
'min_contacts' => fu.Minimum_Contacts__c,
'pct_syntax' => fu.Minimum_Pct_Same_Syntax__c,
'acct_fuzzy' => fu.Account_Fuzzy_Match__c,
'contacts' => lstContacts
});
// Create api request and send jsonified data
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://j9cf5hum55.execute-api.us-east-1.amazonaws.com/default/Filast');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
// Set the body as a JSON object
request.setBody(jsonS);
HttpResponse response = http.send(request);
// Convert json response back to list of accounts
JSONParser parser = JSON.createParser(response.getBody());
List<Account> lstAccounts = new List<Account>();
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
Account a = (Account)parser.readValueAs(Account.class);
lstAccounts.add(a);
}
}
//Update accounts
update lstAccounts;
}
}
Mock:
@isTest
public class CalloutMock implements HttpCalloutMock {
public HttpResponse respond(HttpRequest request) {
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('[{"Id": "0015f000004htPSAAY", "Filast_Email_Syntax__c": "[email protected]"}, {"Id": "0015f000004htPTAAY", "Filast_Email_Syntax__c": "[email protected]"}]');
response.setStatusCode(200);
return response;
}
}
Test Class:
@isTest
public class FilastCalloutTest {
@isTest
public static void makePostCalloutTest() {
Test.setMock(HttpCalloutMock.class, new CalloutMock());
Test.startTest();
FilastCallout.makePostCallout();
Test.stopTest();
}
}