Sample Code:
CreateAccountComponent.cmp
<aura:component implements="flexipage:availableForAllPageTypes">
<lightning:button label="Create Account" variant="brand" onclick="{!c.createAccount}"/>
</aura:component>
CreateAccountComponentcontroller.js
({
createAccount: function (component) {
var createRecordEvent = $A.get('e.force:createRecord');
if ( createRecordEvent ) {
createRecordEvent.setParams({
'entityApiName': 'Account',
'defaultFieldValues': {
'Type' : 'Prospect',
'Industry' : 'Apparel',
'Rating' : 'Hot'
}
});
createRecordEvent.fire();
} else {
/* Create Record Event is not supported */
alert("Account creation not supported");
}
}
})
Add the lightning component to any page. I have added it to the Account detail page.
Output:
CreateAccountComponent.cmp
<aura:component implements="flexipage:availableForAllPageTypes">
<lightning:button label="Create Account" variant="brand" onclick="{!c.createAccount}"/>
</aura:component>
CreateAccountComponentcontroller.js
({
createAccount: function (component) {
var createRecordEvent = $A.get('e.force:createRecord');
if ( createRecordEvent ) {
createRecordEvent.setParams({
'entityApiName': 'Account',
'defaultFieldValues': {
'Type' : 'Prospect',
'Industry' : 'Apparel',
'Rating' : 'Hot'
}
});
createRecordEvent.fire();
} else {
/* Create Record Event is not supported */
alert("Account creation not supported");
}
}
})
Add the lightning component to any page. I have added it to the Account detail page.
Output:
I tried putting this in a Lighting Application:
ReplyDeleteBut the call to force:createRecord failed. Any idea why?
This event is handled by the one.app container. It’s supported in Lightning Experience, the Salesforce app, and Lightning communities. This event presents a standard page to create a record. That is, it doesn’t respect overrides on the object’s create action.
DeleteGood post on how to pre-populate fields.
ReplyDeleteThanks it solved my problem.
Magulan, hope you remember me! I have question on this - with force:createRecord when it opens record creation page, looks like, it is not opening record type selector. it is taking profile's default record type. Is there any way, we could open record type selector page? User has access to multiple record type.
ReplyDeleteNope. There is no straight forward way for this. First show them the screen to show the record types, then call the event.
Deletei have tried but not all the fields are populating(i included all the fields & got no errors but still not diplaying few field values)
ReplyDeleteCheck the Field level security of the missing fields.
Deletei am using code
ReplyDeletecreateNew : function (component, event, helper) {
var createRecordEvent = $A.get("e.force:createRecord");
var recId = component.get("v.recordId");
var LOOKUP = 'LOOKUP';
createRecordEvent.setParams({
"entityApiName": "Object__c",
"panelOnDestroyCallback": function(event) {
$A.get('e.force:refreshView').fire();
},
"navigationLocation":LOOKUP,
"defaultFieldValues": {
'RelObj__c' : recId
}
});
createRecordEvent.fire();
},
i need to do custom validation on click save. i have to show warning after validation
Option 1: Do your validation in the JavaScript before calling force:createRecord event.
DeleteOption 2: Add the validation rules in the object__c.
I need salesforce Id of record created using force:createrecord? How can I get that?
ReplyDeleteforce:createRecord redirects the user to the newly created record.
Delete