I run in an issue with calling APEX out of JavaScript, when i call my APEX class, the class is executed with API-version 34.0, but in the metadata of this class is the API-version set to 27.0. I need 27.0 because there is a change how fields with null values are represented in the JSONized Query, before 28.0 the fields where included afterwards not anymore. I've found a solution, but not a perfect one, i just put the logic in an @future method and everything is running fine. But this is not a solution for every case i try to solve. Here some code snippets to try out:
global class fetchJSONDataMaster{
webservice static void JSONFetchData(){
fetchJSONDataFuture.CalledMethod();
}
webservice static void JSONFetchDataNoFuture(){
fetchJSONData.CalledMethod();
}
}
public class fetchJSONDataFuture{
@future
public static void CalledMethod(){
String query_full = ('SELECT AccountId, AssistantName, AssistantPhone, Birthdate, Fax, Phone, Description, Id, CreatedById, CreatedDate, IsDeleted, Department, EmailBouncedDate, EmailBouncedReason, FirstName, Name, HomePhone, LastActivityDate, LastModifiedById, LastModifiedDate, LastName, LastCURequestDate, LastCUUpdateDate, LeadSource, MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, MasterRecordId, MobilePhone, OtherCity, OtherCountry, OtherPhone, OtherState, OtherStreet, OtherPostalCode, OwnerId, ReportsToId, Salutation, SystemModstamp, Title FROM Contact LIMIT 10');
SObject[] s = database.query(query_full);
System.debug(JSON.serialize(s));
}
}
public class fetchJSONData{
public static void CalledMethod(){
String query_full = ('SELECT AccountId, AssistantName, AssistantPhone, Birthdate, Fax, Phone, Description, Id, CreatedById, CreatedDate, IsDeleted, Department, EmailBouncedDate, EmailBouncedReason, FirstName, Name, HomePhone, LastActivityDate, LastModifiedById, LastModifiedDate, LastName, LastCURequestDate, LastCUUpdateDate, LeadSource, MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, MasterRecordId, MobilePhone, OtherCity, OtherCountry, OtherPhone, OtherState, OtherStreet, OtherPostalCode, OwnerId, ReportsToId, Salutation, SystemModstamp, Title FROM Contact LIMIT 10');
SObject[] s = database.query(query_full);
System.debug(JSON.serialize(s));
}
}
And the calling JavaScript:
{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/27.0/apex.js")}
sforce.apex.execute("fetchJSONDataMaster","JSONFetchData",{});
sforce.apex.execute("fetchJSONDataMaster","JSONFetchDataNoFuture",{});
I have no Idea if its a bug or a feature! Maybe someone of you! I hope someone can help me!
Small Edit: You need to set the API Version fetchJSONData and fetchJSONDataFuture to 27.0. You can do this via something like Sublime with Mavensmateor any other IDE which supports changes on the metadata.
Edit 2: The outcome of both calls is different! Without @future the null values arent included in the JSON.
Edit 3: The difference between the two calls is that the @future method runs in its own execution context while the method without @future runs in the execution context of the class called out of JS.
I've already tried to set the API-version of the class called out of JS to 27.0. It doesn't changes nothing