You need to sign in to do that
Don't have an account?
William Roach-Barrette
Trying to encode JSON while using SOQL to pull Contacts based on AccountID, keep getting FATAL ERROR "Can not write a field name, expecting a value"
I'm trying to re-design some apex code that sends customer data to our companies support site using JSON. Because my employees have started using the mass in-line editing features provided in lightning I need to make sure I can always pass an array of contacts and acconts to my support site. Im running into trouble generating that array and now I keep getting this fatal error:
Additionally. Here the code snippit I believe is responsible for it:
At this point I dont know if its my SOQ or the way Im encoding my arrays in JSON, can someone more expirenced please try and find the error in my ways?
3:43:37.253 (287905094)|METHOD_ENTRY|[72]||System.JSONGenerator.writeFieldName(String) 13:43:37.253 (288134049)|METHOD_EXIT|[72]||System.JSONGenerator.writeFieldName(String) 13:43:37.253 (288142611)|METHOD_EXIT|[58]|01p13000001q7nX|Account_Only_JSONpush_Service.createJSONString() 13:43:37.253 (288148793)|METHOD_EXIT|[47]|01p13000001q7nX|Account_Only_JSONpush_Service.getActInfo(List<Account>) 13:43:37.253 (288156928)|METHOD_EXIT|[15]|01p13000001q7nX|Account_Only_JSONpush_Service.processTrigger(Boolean, Boolean, Boolean, Boolean, List<Account>, Map<Id,Account>, Map<Id,Account>) 13:43:37.253 (288246123)|FATAL_ERROR|System.JSONException: Can not write a field name, expecting a value Class.System.JSONGenerator.writeFieldName: line 58, column 1 Class.Account_Only_JSONpush_Service.createJSONString: line 72, column 1 Class.Account_Only_JSONpush_Service.getActInfo: line 58, column 1 Class.Account_Only_JSONpush_Service.processTrigger: line 47, column 1 Trigger.Account_Only_JSONpush_Service_Handler: line 15, column 1 13:43:37.253 (288263078)|FATAL_ERROR|System.JSONException: Can not write a field name, expecting a value Class.System.JSONGenerator.writeFieldName: line 58, column 1 Class.Account_Only_JSONpush_Service.createJSONString: line 72, column 1 Class.Account_Only_JSONpush_Service.getActInfo: line 58, column 1 Class.Account_Only_JSONpush_Service.processTrigger: line 47, column 1 Trigger.Account_Only_JSONpush_Service_Handler: line 15, column 1 13:43:37.288 (288304061)|CUMULATIVE_LIMIT_USAGE 13:43:37.288 (288304061)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 6 out of 100 Number of query rows: 7 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10
Additionally. Here the code snippit I believe is responsible for it:
public String createJSONString(){ System.debug('MADE IT TO CREATE JSON STRING'); JSONGenerator gen = JSON.createGenerator(true); System.debug('CREATED JSON GENERATOR'); System.debug('STARTED ARRAY'); gen.writeFieldName('accounts'); gen.writeStartArray(); for(Account act1 : actList) { System.debug('ENTERED ACCOUNT FOR LOOP'); List<Account> dumblst; dumblst.add(act1); List<Contact> cntList = [Select Id, firstName, lastName, mailingStreet, mailingCity, mailingState, mailingPostalCode, mailingCountry, phone, MobilePhone, email, accountId, Support_Site_Access__c, LastModifiedBy.Name, LastModifiedDate, Support_Portal_Expiration_Date_New__c, CreatedDate from Contact where AccountId IN :dumblst AND Support_Site_Access__c = TRUE]; System.debug('CLEARED CONTACT SOQL'); gen.writeStartObject(); gen.writeFieldName('account'); gen.writeObjectField('accountID',act1.Id); gen.writeObjectField('parentID', act1.ParentId == null ? '' : act1.ParentId); gen.writeObjectField('partnerAccountID', act1.Reseller__c == null ? '': act1.Reseller__c); gen.writeObjectField('accountType', act1.Account_Type__c == null ? '' : act1.Account_Type__c); gen.writeObjectField('accountName', act1.Name == null ? '' : act1.Name); gen.writeObjectField('accountManager', act1.Owner.Name == null ? '' : act1.Owner.Name); gen.writeObjectField('accountManagerEmail', act1.Owner.Email == null ? '' : act1.Owner.Email); gen.writeObjectField('billingStreet', act1.billingStreet == null ? '' : act1.billingStreet); gen.writeObjectField('billingCity', act1.billingCity == null ? '' : act1.billingCity); gen.writeObjectField('billingState', act1.billingState == null ? '' : act1.billingState); gen.writeObjectField('billingZip', act1.billingPostalCode == null ? '' : act1.billingPostalCode); gen.writeObjectField('billingCountry', act1.billingCountry == null ? '' : act1.billingCountry); gen.writeObjectField('website', act1.Website == null ? '' : act1.Website); if(act1.Annual_Support_Expiration_Date__c!=null) { DateTime aSED = act1.Annual_Support_Expiration_Date__c; String aSEDout = aSED.format('yyyy-MM-dd'); annualSupportExpirationDate = aSEDout; gen.writeObjectField('annualSupportExpirationDate', aSEDout == null ? '' : aSEDout); } if(act1.Annual_Firmware_Expiration_Date__c!=null) { DateTime aSED1 = act1.Annual_Firmware_Expiration_Date__c; String aSED1out = aSED1.format('yyyy-MM-dd'); gen.writeObjectField('annualFirmwareExpirationDate', aSED1out == null ? '' : aSED1out); } if(act1.Reseller_Agrmt_Expiration_Date__c!=null) { DateTime aRAD = act1.Reseller_Agrmt_Expiration_Date__c; String aRADout = aRAD.format('yyyy-MM-dd'); gen.writeObjectField('resellerAgrmtExpirationDate', aRADout == null ? '' : aRADout); } if(act1.Distributor_Agrmt_Expiration_Date__c!=null) { DateTime aDAD = act1.Distributor_Agrmt_Expiration_Date__c; String aDADout = aDAD.format('yyyy-MM-dd'); gen.writeObjectField('distributorExpirationDate', aDADout == null ? '' : aDADout ); } gen.writeObjectField('sFUser',sFUser == null ? '' : sFUser); gen.writeEndObject(); // gen.writeFieldName('contacts'); gen.writeStartArray(); for(Contact cnt1 : cntList) { gen.writeFieldName('contact'); gen.writeStartObject(); gen.writeObjectField('contactID',cnt1.Id); gen.writeObjectField('firstName',cnt1.firstName == null ? '' : cnt1.firstName); gen.writeObjectField('lastName',cnt1.lastName == null ? '' : cnt1.lastName); gen.writeObjectField('mailingStreet',cnt1.mailingStreet == null ? '' : cnt1.mailingStreet); gen.writeObjectField('mailingCity',mailingCity == null ? '' : cnt1.mailingCity); gen.writeObjectField('mailingState',cnt1.mailingState == null ? '' : cnt1.mailingState); gen.writeObjectField('mailingZip',cnt1.mailingPostalCode == null ? '' : cnt1.mailingPostalCode); gen.writeObjectField('mailingCountry',cnt1.mailingCountry == null ? '' : cnt1.mailingCountry); gen.writeObjectField('phone',cnt1.phone == null ? '' : cnt1.phone); gen.writeObjectField('mobile',cnt1.MobilePhone == null ? '' : cnt1.MobilePhone); gen.writeObjectField('email',cnt1.Email == null ? '' : cnt1.Email); gen.writeObjectField('modifiedBy', cnt1.LastModifiedBy.Name == null ? '' : cnt1.LastModifiedBy.Name); if(cnt1.LastModifiedDate != null){ //if statements must be used here because datetime objects are incompatiable with ternary gen.writeObjectField('modifiedDate', cnt1.LastModifiedDate); } else{ gen.writeObjectField('modifiedDate', ''); } datetime cntExpDate = datetime.newInstance(cnt1.Support_Portal_Expiration_Date_New__c.year(),cnt1.Support_Portal_Expiration_Date_New__c.month(),cnt1.Support_Portal_Expiration_Date_New__c.day()); if (cntExpDate != null){ gen.writeObjectField('contactExpirationDate', cntExpDate); } else{ gen.writeObjectField('contactExpirationDate', ''); } datetime cntCretDate = datetime.newInstance(cnt1.CreatedDate.year(),cnt1.CreatedDate.month(),cnt1.CreatedDate.day()); if (cntExpDate != null){ gen.writeObjectField('contactCreationDate', cntCretDate); } else{ gen.writeObjectField('contactCreationDate', ''); } gen.writeEndObject(); gen.writeEndArray(); } gen.writeEndObject(); gen.writeEndArray(); } actjsonTosend = gen.getAsString(); System.debug('JSON String Account_Only --------------->>' + actjsonTosend); System.debug(actjsonTosend); return actjsonTosend; }
At this point I dont know if its my SOQ or the way Im encoding my arrays in JSON, can someone more expirenced please try and find the error in my ways?