0

I am working on SOAP integration i am getting the date format from third party is

'yyyy-mm-dd' i want to insert this format into salesforce. my webservice is accepting this format 'dd-mm-yyyy'.. how can change my code to accept 'yyyy-mm-dd' this format i want to store into salesforce i tried like this, below code is accepting format is'dd-mm-yyyy'...?

  String[] str = ocDate.split(' ');
      String[] dts = str[0].split('/');
Date myDate = date.newinstance(Integer.valueOf(dts[2]), Integer.valueOf(dts[0]), Integer.valueOf(dts[1]));
  op.CloseDate = myDate;

How can i change my code to accept 'yyyy-mm-dd' format

2
  • Sathya please try to avoid using the very general salesforce-crm tag and choose tags which are more closely related to the subject of your question. – Samuel De Rycke Feb 24 '14 at 13:53
  • @SamuelDeRycke ok sure thanq for editing. – Sathya Feb 24 '14 at 13:55
4

I'd say your current code is actually taking a date of the format mm/dd/yyyy, as you are splitting the string on a slash character ('/') and then passing the third element as the year parameter, the first element as the month parameter and the second element as the day parameter.

To handle a date of the form yyyy-mm-dd you would use:

String[] dts = str[0].split('-');

Date myDate = date.newinstance(Integer.valueOf(dts[0]), Integer.valueOf(dts[1]), Integer.valueOf(dts[2]));
4
  • when i use your code it storing 11/21/2014 like this how can i change 21/11/2014 to store this format. – Sathya Feb 24 '14 at 14:16
  • That sounds like the date you are receiving is in a different format to the one you specified, and is actually yyyy-dd-mm. Simply replace the swap the first two parameters to the newinstance method: Date myDate = date.newinstance(Integer.valueOf(dts[1]), Integer.valueOf(dts[0]), Integer.valueOf(dts[2])); – Bob Buzzard Feb 24 '14 at 14:32
  • when i use above your new code at that time it showing an error System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Close Date: invalid date: Mon Oct 19 00:00:00 GMT 179: [CloseDate] – Sathya Feb 24 '14 at 14:39
  • That sounds like you are using the american date format then. The format of the date in the code I posted is correct and the date is being stored as expected, but when it is displayed to you it is in american format which is mm/dd/yyyy. This is down to your user or org configuration I would say. – Bob Buzzard Feb 24 '14 at 14:57

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.