1

I have the JSON string below that is being sent to a Salesforce Webservice:

{"Opportunity":
{"Id":"006g000000IiM5z",
"Appraisal_Date__c":"2019-11-22"}

I serialize the request into a Map and am trying to get the Date field value but get an error from Apex when trying to send from Postman: '?

System.TypeException: Invalid date: 2019-11-22

I've tried date.valueof and casting using (date) but no luck with either. Not sure why the below code will not work (all other field types work fine):

RestRequest req = RestContext.request;
String requestBody = req.requestBody.toString();
Map<String, Object> oppMap = (Map<String, Object>)JSON.deserializeUntyped(requestBody);

Opportunity o = new Opportunity();

if ((Object)oppMap.get('Appraisal_Date__c') != null) { 
    o.Appraisal_Date__c = Date.valueof(oppMap.get('Appraisal_Date__c'));
}
  • What's your user's locale? Try using Date.parse instead of Date.valueOf - just keep in mind locale restrictions – Glen De Marcos Nov 11 '19 at 23:08
  • Time Zone (GMT-05:00) Eastern Standard Time (America/New_York) Locale English (United States) Tried 'o.Appraisal_Date__c = Date.parse(oppMap.get('Appraisal_Date__c'));' but got the error 'Method does not exist or incorrect signature: void parse(Object) from the type Date' – Al K Nov 11 '19 at 23:17
3

You are not casting the object returned by (Map)JSON.deserializeUntyped(requestBody). Change to the following:

o.Appraisal_Date__c = Date.parse((String)oppMap.get('Appraisal_Date__c'));
| improve this answer | |
  • That did it, thanks so much Glen! – Al K Nov 11 '19 at 23:27
1

You could just do:

RestRequest req = RestContext.request;
String requestBody = req.requestBody.toString();
Opportunity o = (Opportunity) JSON.deserialize(requestBody,Opportunity.class);
| improve this answer | |
  • Thanks, I'll try that out. – Al K Nov 11 '19 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.