function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bretondevbretondev 

Lightning: Issue with force:createRecord and new Date()

Hello

I am having an issue with force:createRecord event to create a new record.
When I invoke this function, the record must be created with a date field called DateMAJCreditsafe__c with today's date by default.

When I click on the button that triggers force:createRecord, I land on Account creation page, and my field is set to today's date, as expected :
User-added image

But when I save the record, the field is empty :(

User-added image

I don't understand what I am doing wrong.

This is the call to force:createRecord :
 
var createRecordEvent = $A.get("e.force:createRecord");
                    createRecordEvent.setParams({
                        "entityApiName": "Account",
                        "defaultFieldValues": {
                            'Name' : name,
                            //set some other fields
                            'DateMAJCreditsafe__c' : new Date()
                        }
                    });
                    createRecordEvent.fire();


 
Best Answer chosen by bretondev
bretondevbretondev
Hi,

I have found what was causing the issue.
FLS was set to read-only for that field for the profile I was using.
I unticked Read-only for this profile, then it worked.

Thanks though

All Answers

Alain CabonAlain Cabon
Hello,

That should work. I made simple tests without problem with the same createRecordEvent and 'DateMAJCreditsafe__c' : new Date()

Are you sure of DateMAJCreditsafe__c ? (exact value, case sensitive and type (not a formula))
bretondevbretondev
Hi,

I have found what was causing the issue.
FLS was set to read-only for that field for the profile I was using.
I unticked Read-only for this profile, then it worked.

Thanks though
This was selected as the best answer