0

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

2
  • out of curiosity, where are you using the component (in what kind of page, home, record...?) – glls Jul 23 '17 at 7:00
  • I'm using it in the standalone lightning application. Later is will be part of another lightning component to create account without going to created account record after and sending component event with created record. – Marko Dvečko Jul 24 '17 at 7:42
0

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));
                }
            });
        }
    }

})`

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.