1

I am new to salesforce lightning. I have a recordEditForm that I want to submit and before submit I want to make set field TODAY's date from the lightning component controller.I am not able to set the vale and throw error saying

"{"message":"Value for field 'fieldName_Date__c' is not in ISO 8601 format, Value: 1/20/2019 11:19 AM, Runtime class: java.lang.String","detail":"","output":{},"error":{"type":"TRANSPORT_ERROR","details":{}}}"

JavaScript controller

onSubmitofRecordEditForm:function(component, event, helper)
{
    var recordEditFormList = component.find("newFormtoUpdate");
    // get all fields using event.getParam("fields") and set some field values in the backend onsubmit of the recordEditForm
    var eventFields = event.getParam("fields");
    var timezone = $A.get("$Locale.timezone");
    console.log('Time Zone Preference in Salesforce ORG :'+timezone);// Result::: Time Zone Preference in Salesforce ORG :America/New_York
    var mydate = new Date().toLocaleString("en-US", {timeZone: timezone})
    console.log('Date Instance with Salesforce Locale timezone : '+mydate);// Result::: Date Instance with Salesforce Locale timezone : 1/20/2019, 11:14:03 AM
    var date = mydate.replace(/\,/, '');
    var lastModifiedDate = date.replace(/:[0-9]{2}\s/, ' ');
    console.log(lastModifiedDate); // Result ::: 1/20/2019 11:14 AM

    //salesforce field format is 1/20/2019 11:14 AM
    // not able to set the vale and throw error saying 
   /* {"message":"Value for field 'fieldName_Date__c' is not in ISO 8601 format, Value: 1/20/2019 11:19 AM, 
    Runtime class: java.lang.String","detail":"","output":{},"error":{"type":"TRANSPORT_ERROR","details":{}}}*/
            eventFields["fieldName_Date__c"] = lastModifiedDate; 

            // global function submit used to submit the recordEditForm
            recordEditFormList.submit(eventFields);

    }
2

When you use new Date() , the date you receive is already datetime of the browser ie its already in user locale( $Locale.timezone gives timezone of the user not of your org). You don't have to convert it anything.

Just, use toISOString() on the date and it gives you Salesforce compatible format.

Thus your code will be minimal.

({
    onSubmitofRecordEditForm:function(component, event, helper){
        event.preventDefault(); 

        var recordEditFormList = component.find("newFormtoUpdate");
        // get all fields using event.getParam("fields") and set some field values in the backend onsubmit of the recordEditForm
        var eventFields = event.getParam("fields");

        //get current  time in ISO 8601 format
        var dateTimeNow = new Date().toISOString();
         console.log(dateTimeNow);
        eventFields['fieldName_Date__c'] = dateTimeNow;

    // global function submit used to submit the recordEditForm
    recordEditFormList.submit(eventFields);

    }
})

UPDATE: From Comments the solution was to use Aura Localization service . The AuraLocalizationService JavaScript API provides methods for formatting and localizing dates.

({
        onSubmitofRecordEditForm:function(component, event, helper){
            event.preventDefault(); 

            var recordEditFormList = component.find("newFormtoUpdate");
            // get all fields using event.getParam("fields") and set some field values in the backend onsubmit of the recordEditForm
            var eventFields = event.getParam("fields");

            //get current  time in ISO 8601 format
            var dateTimeNow = $A.localizationService.formatDateTime(new Date(), "YYYY-MM-DDThh:mm:ssZ");
             console.log(dateTimeNow);
            eventFields['fieldName_Date__c'] = dateTimeNow;

        // global function submit used to submit the recordEditForm
        recordEditFormList.submit(eventFields);

        }
    })
  • By using you code I am getting this value dateTimeNow=====> 2019-01-21T01:44:48.645Z. where my date is 1/2082019 and time is 8.44 pm. In my saleforce org my time is 1/2082019 and time is 8.44 pm as well. also I try with var formatDate = $A.localizationService.formatDate(dateTimeNow, "MM/DD/YYYY hh:mm a"); but still it is throwing alue for field 'fieldName_Date__c' is not in ISO 8601 format. my salesforce ORG date time field format is like this "1/20/2019 8:43 PM " – demolightning Jan 21 at 1:50
  • @demolightning Salesforce stores DateTime in format YYYY-MM-DDThh:mm:ssZ , I can see toISOString also returned in same format. Whats the issue exactly? help.salesforce.com/articleView?id=000004680&type=1 – Pranay Jaiswal Jan 21 at 14:20
  • 1
    It is working by changing the format $A.localizationService.formatDateTime(new Date(), "YYYY-MM-DDThh:mm:ssZ");Thanks – demolightning Jan 21 at 16:43
  • @demolightning whats the difference between output of $A.localizationService.formatDateTime(new Date(), "YYYY-MM-DDThh:mm:ssZ"); and Op of new Date().toISOString(); – Pranay Jaiswal Jan 21 at 16:45
  • 1
    @ Pranay Jaiswal new Date().toISOString():::: Result### 2019-01-21T17:31:17.394Z and $A.localizationService.formatDateTime(new Date(), "YYYY-MM-DDThh:mm:ssZ")::: Result #### 2019-01-21T12:31:17-05:00. Thanks all of you for the help. – demolightning Jan 21 at 17: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.