- William Roach-Barrette
- NEWBIE
- 30 Points
- Member since 2018
-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
11Questions
-
14Replies
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?
- William Roach-Barrette
- January 29, 2019
- Like
- 0
cannot create permissions set for salesforce inbox
I am having some trouble setting up salesforce inbox and hope you can help. I am using Classic right now (in the process of upgrading to lightning) and I am following this tutorial: http://salesforce.vidyard.com/watch/MqnjffCiLSJZf5EkF47xSd I installed the Salesforce Inbox Control Pannel from the appexchange and am trying to create a permissions set, however when I select my license and try to save I get this error: "You may not turn off permission Read Opportunities for this License Type" I am including a photo for refrence, do you know what might be causing this
- William Roach-Barrette
- August 15, 2018
- Like
- 0
Can I send an email to a contact based on a custom date field using workflow rules?
Hello, I want to figure out the best way to send automated emails to customers letting them know their certification with my company is about to expire. I have created email templates for 6 months, three months, 1 month etc. And I created a formula field that caculates the expiration date of their certification. I cannot figure out a way to setup a workflow rule to check whether the expiration date is less than 6 months away, 3 months, etc. Am I using the wrong part of salesforce to do this? Or do I need to rethink how I am determining the certification expiration dates and how workflow rule should monitor them? I thought about setting up an apex trigger that checks on the expiration dates whenever someone creates or edits a contact. But the issue is I have existing dates that would never be checked if I did that.
- William Roach-Barrette
- July 05, 2018
- Like
- 0
Not using ID to insert? Insert failed: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please provide either an Email Address or Phone Number: []
I am having trouble understanding this error and how its being caused. I am trying to pull user data in the form of JSON from an external website then Im trying to store that data in the contacts record. I know that every JSON string has a name, email and contact ID present. So how is it I can get an error like this?
For testing purposes I had to create a mock response by implimenting the HTTPMockUp class, you can see my code below. Can I add some kind of logic check that would keep this from happening? im really close to passing all my orgs tests.
Wrapper for JSON:
Callout Class and updater:
My test class:
HttpCalloutMock implementation:
My trigger:
And here is a picture of my errors:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please provide either an Email Address or Phone Number: []
For testing purposes I had to create a mock response by implimenting the HTTPMockUp class, you can see my code below. Can I add some kind of logic check that would keep this from happening? im really close to passing all my orgs tests.
Wrapper for JSON:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
Callout Class and updater:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} public HttpResponse httpResponse {get;set;} @future(callout=true) public static void deserialize() { GDPRWrapper wrapper; HttpResponse httpResponse = new HttpResponse(); Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('https://private'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = new HttpResponse(); try{ response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ // insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
My test class:
@isTest private class JSONDeserializeTest { @isTest static void testCalloutWithStaticResource(){ Test.setMock(HttpCalloutMock.class, new Test_callApply1()); JSONDeserialize.deserialize(); System.assert(true); } }
HttpCalloutMock implementation:
@isTest global class Test_callApply1 implements HttpCalloutMock { // Implement this interface method global HTTPResponse respond(HttpRequest req) { // Optionally, only send a mock response for a specific endpoint // and method. //System.assertEquals('http://example.com/example/test', req.getEndpoint()); //System.assertEquals('GET', req.getMethod()); // Create a fake response HttpResponse res = new HttpResponse(); res.setHeader('Content-Type','application/json'); res.setBody('PRIVATE JSON DATA'); res.setStatusCode(200); return res; } }
My trigger:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false && !Test.isRunningTest()) { System.debug('i got triggered!!!'); JSONDeserialize.deserialize(); } }
And here is a picture of my errors:
- William Roach-Barrette
- June 08, 2018
- Like
- 0
Cant get salesforce to test using mock Web Service callout
No matter what I do I cant get my code to validate using the test methods I made. I want to test a web service callout and already created the test class and static resource that mock uses to make the response. In order to properly test I needed to return the httpresponse I recieved in my desearialize method. This ment I couldnt make it @future(callout=true) so I created another method with the future tag that gets called by my trigger, the test method can call deserialize directly and use it to compare the expected httpResponse to the actual response. But no matter what I seem to do the program wont validate using my test code. It just immeaditally fails saying it cant test webservice callouts. Can anyone offer me a solution that lets me test my respose while maintaining the future callout and in such a way that salesforce will accept it?
WRAPPER:
Web callout class:
Test Class:
Trigger:
Errors:
WRAPPER:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
Web callout class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} public HttpResponse httpResponse {get;set;} @Future(callout=true) Public static void firecallout(string str) { System.debug('@@@ ' + str); JSONDeserialize.deserialize(); } public static HttpResponse deserialize() { GDPRWrapper wrapper; HttpResponse httpResponse = new HttpResponse(); Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('PRIVATE'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = new HttpResponse(); try{ response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } return response; } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
Test Class:
@isTest private class JSONDeserializeTest { @isTest static void testCalloutEithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); HttpResponse res = JSONDeserialize.deserialize(); System.assertEquals('PRIVATE DATA', res.getBody()); System.assertEquals(200, res.getStatusCode()); System.assertEquals('application/json', res.getHeader('Content-Type')); } }
Trigger:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false) { JSONDeserialize.firecallout('callout fires'); } }
Errors:
- William Roach-Barrette
- June 08, 2018
- Like
- 0
Validating webService callout using mock and @future causing deployment failure
I am currently trying to deploy some code on my production org and can only atchieve 64% code coverage. I am trying to test a web callout and have created a static resource that houses some sample JSON I want to parse. I belive my issue has to do with needing an @future call while also needing to return the HTTP response to validate my code while testing. I am getting the following erros on the following tests right now:
Here is my wrapper:
My web callout class:
My test class:
And finally, here is my current trigger, I think I might have some issues with this as well but one thing at a time:
Can someone please help me figure out why my code is failing and what I need to do to midigate some of the errors
Here is my wrapper:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
My web callout class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} //@Future(callout=true) public static HttpResponse deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('PRIVATE'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = new HttpResponse(); try{ response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } return response; } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
My test class:
@isTest private class JSONDeserializeTest { @isTest static void testCalloutEithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); HttpResponse res = JSONDeserialize.deserialize(); System.assertEquals('PRIVATE DATA', res.getBody()); System.assertEquals(200, res.getStatusCode()); System.assertEquals('application/json', res.getHeader('Content-Type')); } }
And finally, here is my current trigger, I think I might have some issues with this as well but one thing at a time:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false){ JSONDeserialize.deserialize(); } }
Can someone please help me figure out why my code is failing and what I need to do to midigate some of the errors
- William Roach-Barrette
- June 07, 2018
- Like
- 0
How to make production org use a static resource for web service callout validation
I have been trying to use static resources to test a piece of code I wrote and am trying to validate and deploy on my production org. I created a static resource called GDPR that houses the JSON. Then I wrote a private test class that mocks up a response then calls my method. The problem is no matter what I do I cant seem to make salesforce use the static resource when its validating my code. I will include all my code below, what do I need to modify to make this work properly?
WRAPPER:
Web Callout Class:
Test Class:
When I run the basic validation now I get four errors, all of them the same
WRAPPER:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
Web Callout Class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('**********************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); try{ HttpResponse response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
Test Class:
@isTest private class desearializeTest{ @isTest static void testWebCalloutWithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); JSONDeserialize.deserialize(); } }
When I run the basic validation now I get four errors, all of them the same
Methods defined as TestMethod do not support Web service callouts Stack Trace: null
- William Roach-Barrette
- June 07, 2018
- Like
- 0
Deployment Failed: How to work around "Methods defined as TestMethod do not support Web service callouts Stack Trace: null"
I want to begin by saying I am very new to salesforce and deploying code on salesforce, so this question might be very rudimentary at its core. That being said, I need assistance creating a test class for some code I have developed. This code reachs out to an external server to pull a JSON file that contains some user prefrences I want to import into salesforce. I have all my code debugged and moved over to my production instance, but no matter what I do I keep getting this as an error when I try to validate:
And here is my code:
Wrapper:
JSON Parser:
My main question is this: How do I modify my desearialize method so that I can make use of a static resource for testing purposes?
Methods defined as TestMethod do not support Web service callouts Stack Trace: nullI know I need some kind of test class, and through my research I tihnk I can make use of Static Resources to offer salesforce a way to test my code without actually preforming a web service callout. I just dont know how best to proceed. If I upload some sample JSON code into a static resource then make a test class that tests my code, how do I tell salesforce to use the static resource over the actual web service callout? Here is a link to what I plan on using as a recource: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_static.htm
And here is my code:
Wrapper:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
JSON Parser:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('**********************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); try{ HttpResponse response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
My main question is this: How do I modify my desearialize method so that I can make use of a static resource for testing purposes?
- William Roach-Barrette
- June 06, 2018
- Like
- 0
INVALID_CROSS_REFRENCE_KEY when upserting a list of contacts
I have developed some code that pulls some data from one of my companies servers in the form of JSON, then convert that into information I can store in contact records. Im trying to update a set of checkboxes and text fields, nothing big. I have my code set to upsert based on users salesforce ID. Im able to reject all the values but ones with the 003 prefix indicating a contact record "this is because there are both leads and contacts in my data" but now im running into an error that seems to occur a lot on salesforce. I cant find my exact use case so I am posting the error here in the hopes someone might figure it out. I will also include my code:
ERROR:
CODE:
ERROR:
10:44:05.11 (1893389505)|EXCEPTION_THROWN|[60]|System.DmlException: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 10:44:05.11 (1932769677)|HEAP_ALLOCATE|[60]|Bytes:144 10:44:05.11 (1932836358)|VARIABLE_SCOPE_BEGIN|[64]|e|System.DmlException|true|false 10:44:05.11 (1932899329)|VARIABLE_ASSIGNMENT|[64]|e|"common.apex.runtime.impl.DmlExecutionException: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []"|0x6c9dcbb8 10:44:05.11 (1932909864)|STATEMENT_EXECUTE|[64] 10:44:05.11 (1932912315)|STATEMENT_EXECUTE|[65] 10:44:05.11 (1932918693)|HEAP_ALLOCATE|[65]|Bytes:33 10:44:05.11 (1932990589)|HEAP_ALLOCATE|[65]|Bytes:140 10:44:05.11 (1933013744)|HEAP_ALLOCATE|[65]|Bytes:173 10:44:05.11 (1933035079)|USER_DEBUG|[65]|DEBUG|An unexpected error has occured: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 10:44:05.11 (1933053862)|METHOD_EXIT|[29]|01p1F000000ul4n|JSONDeserialize.UpdateData(GDPRWrapper) 10:44:05.933 (1933069808)|CUMULATIVE_LIMIT_USAGE 10:44:05.933 (1933069808)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 282 out of 10000 Maximum CPU time: 137 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 1 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 1 Number of Mobile Apex push calls: 0 out of 10 10:44:05.933 (1933069808)|CUMULATIVE_LIMIT_USAGE_END 10:44:05.11 (1933107902)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize 10:44:05.11 (1933924669)|EXECUTION_FINISHED
CODE:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ toInsert.Id = wrapper.GDPRList[i].contactId; } } toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ upsert contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
- William Roach-Barrette
- May 30, 2018
- Like
- 0
Cannot update contact records: |EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Afternoon, I am working on a program that will take in data our users give us regarding their contact prefrences, this data llives on a company server, and import that data into salesforce. I have created a program that pulls JSON from an endpoint and stores it in a wrapper. Then I attempt to convert the information im given into a list of contacts that I then use to update my contacts list of perrences "in this case thats just a few checkboxes" Im running into this error that might be due to the fact that the salesforceIDs of the contacts in my sandbox dont match the values in my production org. Still I thought I would post my code and the error to see what the group thinks:
ERROR:
CODE:
TRIGGER:
ERROR:
15:44:26.17 (858546781)|EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] 15:44:26.17 (858854119)|HEAP_ALLOCATE|[56]|Bytes:114 15:44:26.17 (858886596)|VARIABLE_SCOPE_BEGIN|[60]|e|System.DmlException|true|false 15:44:26.17 (858943076)|VARIABLE_ASSIGNMENT|[60]|e|"common.apex.runtime.impl.DmlExecutionException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []"|0x302efa02 15:44:26.17 (858951078)|STATEMENT_EXECUTE|[60] 15:44:26.17 (858953210)|STATEMENT_EXECUTE|[61] 15:44:26.17 (858957978)|HEAP_ALLOCATE|[61]|Bytes:33 15:44:26.17 (858999597)|HEAP_ALLOCATE|[61]|Bytes:110 15:44:26.17 (859015344)|HEAP_ALLOCATE|[61]|Bytes:143 15:44:26.17 (859034345)|USER_DEBUG|[61]|DEBUG|An unexpected error has occured: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] 15:44:26.17 (859050401)|METHOD_EXIT|[29]|01p1F000000y10l|JSONDeserialize.UpdateData(GDPRWrapper) 15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE 15:44:26.859 (859062803)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 282 out of 10000 Maximum CPU time: 139 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 1 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 1 Number of Mobile Apex push calls: 0 out of 10 15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE_END 15:44:26.17 (859096399)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize 15:44:26.17 (860011897)|EXECUTION_FINISHED
CODE:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; // toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ update contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } static void testParse() { System.debug('Entered test Method'); System.debug('Made it to start of deserialization'); Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('https://www.gdpr.rajant.com/pilot/api/gdpr/allOptInUsers'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); System.debug('Authorization Header: ' + authorizationHeader); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); System.debug('Response: ' + response.getBody()); List<GDPRData> obj = parse(response); System.assert(obj!=null); for(Integer i = 0; i < obj.size(); i ++){ System.debug(obj[i]); } } }
TRIGGER:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false){ JSONDeserialize.deserialize(); } }I am still extremely new to APEX so this might be a simple fix. I apoligize for the lack of expirence in advanced. And appreciate any and all feedback
- William Roach-Barrette
- May 29, 2018
- Like
- 0
Future method cannot be called from a future or batch method
Dear Forum,
I am trying to develope an Apex program and corresponding trigger that will grab JSON from an endpoint im hosting and based on values inside that json file make updates to custom fields "checkboxes" under the contacts object. Right now I have a wrapper that stores my JSOn, a program that grabs my data from an endpoint then stores it, and finally a piece of data that creates a list of contact objects that I then use to update my database. The problem is I am now getting this error: "Future method cannot be called from a future or batch method." Below are snippits of code showing what I am doing, as well as the corresponding apex trigger. I am still very new to Apex development and apologize if this question was ansered in another post somewhere else.
I am trying to develope an Apex program and corresponding trigger that will grab JSON from an endpoint im hosting and based on values inside that json file make updates to custom fields "checkboxes" under the contacts object. Right now I have a wrapper that stores my JSOn, a program that grabs my data from an endpoint then stores it, and finally a piece of data that creates a list of contact objects that I then use to update my database. The problem is I am now getting this error: "Future method cannot be called from a future or batch method." Below are snippits of code showing what I am doing, as well as the corresponding apex trigger. I am still very new to Apex development and apologize if this question was ansered in another post somewhere else.
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} public JSONDeserialize(){ deserialize(); } @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('***********************'); Blob headerValue = Blob.valueOf('*****************'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ insert contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } } }
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { JSONDeserialize.deserialize(); }
- William Roach-Barrette
- May 29, 2018
- Like
- 0
cannot create permissions set for salesforce inbox
I am having some trouble setting up salesforce inbox and hope you can help. I am using Classic right now (in the process of upgrading to lightning) and I am following this tutorial: http://salesforce.vidyard.com/watch/MqnjffCiLSJZf5EkF47xSd I installed the Salesforce Inbox Control Pannel from the appexchange and am trying to create a permissions set, however when I select my license and try to save I get this error: "You may not turn off permission Read Opportunities for this License Type" I am including a photo for refrence, do you know what might be causing this
- William Roach-Barrette
- August 15, 2018
- Like
- 0
Cant get salesforce to test using mock Web Service callout
No matter what I do I cant get my code to validate using the test methods I made. I want to test a web service callout and already created the test class and static resource that mock uses to make the response. In order to properly test I needed to return the httpresponse I recieved in my desearialize method. This ment I couldnt make it @future(callout=true) so I created another method with the future tag that gets called by my trigger, the test method can call deserialize directly and use it to compare the expected httpResponse to the actual response. But no matter what I seem to do the program wont validate using my test code. It just immeaditally fails saying it cant test webservice callouts. Can anyone offer me a solution that lets me test my respose while maintaining the future callout and in such a way that salesforce will accept it?
WRAPPER:
Web callout class:
Test Class:
Trigger:
Errors:
WRAPPER:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
Web callout class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} public HttpResponse httpResponse {get;set;} @Future(callout=true) Public static void firecallout(string str) { System.debug('@@@ ' + str); JSONDeserialize.deserialize(); } public static HttpResponse deserialize() { GDPRWrapper wrapper; HttpResponse httpResponse = new HttpResponse(); Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('PRIVATE'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = new HttpResponse(); try{ response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } return response; } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
Test Class:
@isTest private class JSONDeserializeTest { @isTest static void testCalloutEithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); HttpResponse res = JSONDeserialize.deserialize(); System.assertEquals('PRIVATE DATA', res.getBody()); System.assertEquals(200, res.getStatusCode()); System.assertEquals('application/json', res.getHeader('Content-Type')); } }
Trigger:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false) { JSONDeserialize.firecallout('callout fires'); } }
Errors:
- William Roach-Barrette
- June 08, 2018
- Like
- 0
Validating webService callout using mock and @future causing deployment failure
I am currently trying to deploy some code on my production org and can only atchieve 64% code coverage. I am trying to test a web callout and have created a static resource that houses some sample JSON I want to parse. I belive my issue has to do with needing an @future call while also needing to return the HTTP response to validate my code while testing. I am getting the following erros on the following tests right now:
Here is my wrapper:
My web callout class:
My test class:
And finally, here is my current trigger, I think I might have some issues with this as well but one thing at a time:
Can someone please help me figure out why my code is failing and what I need to do to midigate some of the errors
Here is my wrapper:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
My web callout class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} //@Future(callout=true) public static HttpResponse deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('PRIVATE'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = new HttpResponse(); try{ response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } return response; } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
My test class:
@isTest private class JSONDeserializeTest { @isTest static void testCalloutEithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); HttpResponse res = JSONDeserialize.deserialize(); System.assertEquals('PRIVATE DATA', res.getBody()); System.assertEquals(200, res.getStatusCode()); System.assertEquals('application/json', res.getHeader('Content-Type')); } }
And finally, here is my current trigger, I think I might have some issues with this as well but one thing at a time:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false){ JSONDeserialize.deserialize(); } }
Can someone please help me figure out why my code is failing and what I need to do to midigate some of the errors
- William Roach-Barrette
- June 07, 2018
- Like
- 0
How to make production org use a static resource for web service callout validation
I have been trying to use static resources to test a piece of code I wrote and am trying to validate and deploy on my production org. I created a static resource called GDPR that houses the JSON. Then I wrote a private test class that mocks up a response then calls my method. The problem is no matter what I do I cant seem to make salesforce use the static resource when its validating my code. I will include all my code below, what do I need to modify to make this work properly?
WRAPPER:
Web Callout Class:
Test Class:
When I run the basic validation now I get four errors, all of them the same
WRAPPER:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
Web Callout Class:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('**********************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); try{ HttpResponse response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
Test Class:
@isTest private class desearializeTest{ @isTest static void testWebCalloutWithStaticResource(){ StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('GDPR'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, mock); JSONDeserialize.deserialize(); } }
When I run the basic validation now I get four errors, all of them the same
Methods defined as TestMethod do not support Web service callouts Stack Trace: null
- William Roach-Barrette
- June 07, 2018
- Like
- 0
INVALID_CROSS_REFRENCE_KEY when upserting a list of contacts
I have developed some code that pulls some data from one of my companies servers in the form of JSON, then convert that into information I can store in contact records. Im trying to update a set of checkboxes and text fields, nothing big. I have my code set to upsert based on users salesforce ID. Im able to reject all the values but ones with the 003 prefix indicating a contact record "this is because there are both leads and contacts in my data" but now im running into an error that seems to occur a lot on salesforce. I cant find my exact use case so I am posting the error here in the hopes someone might figure it out. I will also include my code:
ERROR:
CODE:
ERROR:
10:44:05.11 (1893389505)|EXCEPTION_THROWN|[60]|System.DmlException: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 10:44:05.11 (1932769677)|HEAP_ALLOCATE|[60]|Bytes:144 10:44:05.11 (1932836358)|VARIABLE_SCOPE_BEGIN|[64]|e|System.DmlException|true|false 10:44:05.11 (1932899329)|VARIABLE_ASSIGNMENT|[64]|e|"common.apex.runtime.impl.DmlExecutionException: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []"|0x6c9dcbb8 10:44:05.11 (1932909864)|STATEMENT_EXECUTE|[64] 10:44:05.11 (1932912315)|STATEMENT_EXECUTE|[65] 10:44:05.11 (1932918693)|HEAP_ALLOCATE|[65]|Bytes:33 10:44:05.11 (1932990589)|HEAP_ALLOCATE|[65]|Bytes:140 10:44:05.11 (1933013744)|HEAP_ALLOCATE|[65]|Bytes:173 10:44:05.11 (1933035079)|USER_DEBUG|[65]|DEBUG|An unexpected error has occured: Upsert failed. First exception on row 7 with id 0031B00002YY6VIQA1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 10:44:05.11 (1933053862)|METHOD_EXIT|[29]|01p1F000000ul4n|JSONDeserialize.UpdateData(GDPRWrapper) 10:44:05.933 (1933069808)|CUMULATIVE_LIMIT_USAGE 10:44:05.933 (1933069808)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 282 out of 10000 Maximum CPU time: 137 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 1 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 1 Number of Mobile Apex push calls: 0 out of 10 10:44:05.933 (1933069808)|CUMULATIVE_LIMIT_USAGE_END 10:44:05.11 (1933107902)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize 10:44:05.11 (1933924669)|EXECUTION_FINISHED
CODE:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ toInsert.Id = wrapper.GDPRList[i].contactId; } } toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ upsert contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
- William Roach-Barrette
- May 30, 2018
- Like
- 0
Cannot update contact records: |EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Afternoon, I am working on a program that will take in data our users give us regarding their contact prefrences, this data llives on a company server, and import that data into salesforce. I have created a program that pulls JSON from an endpoint and stores it in a wrapper. Then I attempt to convert the information im given into a list of contacts that I then use to update my contacts list of perrences "in this case thats just a few checkboxes" Im running into this error that might be due to the fact that the salesforceIDs of the contacts in my sandbox dont match the values in my production org. Still I thought I would post my code and the error to see what the group thinks:
ERROR:
CODE:
TRIGGER:
ERROR:
15:44:26.17 (858546781)|EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] 15:44:26.17 (858854119)|HEAP_ALLOCATE|[56]|Bytes:114 15:44:26.17 (858886596)|VARIABLE_SCOPE_BEGIN|[60]|e|System.DmlException|true|false 15:44:26.17 (858943076)|VARIABLE_ASSIGNMENT|[60]|e|"common.apex.runtime.impl.DmlExecutionException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []"|0x302efa02 15:44:26.17 (858951078)|STATEMENT_EXECUTE|[60] 15:44:26.17 (858953210)|STATEMENT_EXECUTE|[61] 15:44:26.17 (858957978)|HEAP_ALLOCATE|[61]|Bytes:33 15:44:26.17 (858999597)|HEAP_ALLOCATE|[61]|Bytes:110 15:44:26.17 (859015344)|HEAP_ALLOCATE|[61]|Bytes:143 15:44:26.17 (859034345)|USER_DEBUG|[61]|DEBUG|An unexpected error has occured: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] 15:44:26.17 (859050401)|METHOD_EXIT|[29]|01p1F000000y10l|JSONDeserialize.UpdateData(GDPRWrapper) 15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE 15:44:26.859 (859062803)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 282 out of 10000 Maximum CPU time: 139 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 1 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 1 Number of Mobile Apex push calls: 0 out of 10 15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE_END 15:44:26.17 (859096399)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize 15:44:26.17 (860011897)|EXECUTION_FINISHED
CODE:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; // toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ update contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } static void testParse() { System.debug('Entered test Method'); System.debug('Made it to start of deserialization'); Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('https://www.gdpr.rajant.com/pilot/api/gdpr/allOptInUsers'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); System.debug('Authorization Header: ' + authorizationHeader); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); System.debug('Response: ' + response.getBody()); List<GDPRData> obj = parse(response); System.assert(obj!=null); for(Integer i = 0; i < obj.size(); i ++){ System.debug(obj[i]); } } }
TRIGGER:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { if(System.IsBatch() == false && System.isFuture() == false){ JSONDeserialize.deserialize(); } }I am still extremely new to APEX so this might be a simple fix. I apoligize for the lack of expirence in advanced. And appreciate any and all feedback
- William Roach-Barrette
- May 29, 2018
- Like
- 0
Future method cannot be called from a future or batch method
Dear Forum,
I am trying to develope an Apex program and corresponding trigger that will grab JSON from an endpoint im hosting and based on values inside that json file make updates to custom fields "checkboxes" under the contacts object. Right now I have a wrapper that stores my JSOn, a program that grabs my data from an endpoint then stores it, and finally a piece of data that creates a list of contact objects that I then use to update my database. The problem is I am now getting this error: "Future method cannot be called from a future or batch method." Below are snippits of code showing what I am doing, as well as the corresponding apex trigger. I am still very new to Apex development and apologize if this question was ansered in another post somewhere else.
I am trying to develope an Apex program and corresponding trigger that will grab JSON from an endpoint im hosting and based on values inside that json file make updates to custom fields "checkboxes" under the contacts object. Right now I have a wrapper that stores my JSOn, a program that grabs my data from an endpoint then stores it, and finally a piece of data that creates a list of contact objects that I then use to update my database. The problem is I am now getting this error: "Future method cannot be called from a future or batch method." Below are snippits of code showing what I am doing, as well as the corresponding apex trigger. I am still very new to Apex development and apologize if this question was ansered in another post somewhere else.
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} public JSONDeserialize(){ deserialize(); } @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('***********************'); Blob headerValue = Blob.valueOf('*****************'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); HttpResponse response = h.send(request); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } try{ insert contactPref; } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } } }
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) { JSONDeserialize.deserialize(); }
- William Roach-Barrette
- May 29, 2018
- Like
- 0