[SalesForce] force:recordData getNewRecord method doesn’t create record

I'm creating custom component for creating account record in lightning. I'm using force:recordData tag and getNewRecord method. Problem is when I call getNewRecord method, callback function is never called back and record is never filled. I've tried to fix this problem but without luck.

Component.cmp

<force:recordData 
    aura:id="accountRecordCreator"
    layoutType="FULL"
    mode="EDIT"
    targetRecord="{!v.account}"
    targetFields="{!v.simpleAccount}"
    targetError="{!v.accountError}" />

ComponentController.js

var accountCreator = component.find("accountRecordCreator");
accountCreator.getNewRecord("Account", null, false, $A.getCallback(function() {
    component.set("v.isWaiting", false);
}));

Here is the warning from the Chrome
Chrome console

Here is the response from server
Chrome network

Best Answer

getNewRecord will give you the template for creating record. You can verify the record object type in callback function using apiname.

below is the component built for account page to create contact.

component: `

<force:recordData aura:id="contactRecordCreator"
                 layoutType="FULL"
                 targetRecord="{!v.newContact}"
                 targetFields="{!v.simpleNewContact}"
                 targetError="{!v.newContactError}" />

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<lightning:card iconName="action:new_contact" title="Create Contact" >
    <div class="slds-p-horizontal--small">
        <lightning:input aura:id="contactField" label="First Name" value="{!v.simpleNewContact.FirstName}" />
        <lightning:input aura:id="contactField" label="Last Name" value="{!v.simpleNewContact.LastName}" />
        <lightning:input aura:id="contactField" label="Title" value="{!v.simpleNewContact.title}" />
        <br />
        <lightning:button label="Save Contact" variant="brand" onclick="{!c.handleSaveContact}" />
    </div>
</lightning:card>
<aura:if isTrue="{!not(empty(v.newContactError))}" >
    <div class="recordError">
        {!v.newContactError}
    </div>
</aura:if>

`

Controller:

({
    myAction : function(component, event, helper) {

    },
    doInit : function(component, event, helper) {
        component.find('contactRecordCreator').getNewRecord(
        'Contact',
         null,
         false,
            $A.getCallback(function(){
                var rec = component.get('v.newContact');
                var error = component.get('v.newContactError');
                if(error || rec === null){
                    console.log('Error initializing record template: ' +error+rec);
                }
                else {
                    console.log('Record template initialized:'+rec.apiName);
                }
            }));
    },
    handleSaveContact : function(component,event,helper) {
        if(helper.validateContactForm(component)){
            component.set("v.simpleNewContact.AccountId",component.get("v.recordId"));
            //component.set("v.simpleNewContact.AccountId", component.get("v.recordId"));
            component.find('contactRecordCreator').saveRecord(function(saveResult){
                if(saveResult.state === 'SUCCESS' || saveResult.state === 'DRAFT'){
                    var resultToast = $A.get("e.force:showToast");
                    resultToast.setParams({
                        "title" : "SUCCESS",
                        "message" : "The record saved successfully"
                    });
                    resultToast.fire();
                } else if (saveResult.state === 'INCOMPLETE'){
                    console.log("User is offline, device doesn't support drafts.");
                } else if (saveResult.state === 'ERROR' ) {
                    console.log('Problem saving contact, error: ' +JSON.stringify(saveResult.error));
                } else {
                    console.log('Unknown problem, state: ' + saveResult.state +
                                ', error: ' + JSON.stringify(saveResult.error));
                }
            });
        }
    }

})`
Related Topic