1

Component

<aura:component >
    <aura:attribute name="myfields" type="String[]" default="['FirstName', 'LastName', 'Email']"/>
    <aura:attribute name="con" type="Object"/>
    <aura:attribute name="Newcon" type="Object"/>
   <!-- Here mydata is to use in controller -->
    <aura:handler name="init" value="{!this}" action="{!c.IniMe}"/>
    <force:recordData aura:id="mydata" fields="{!v.myfields}" targetFields="{!v.con}" targetRecord="{!v.Newcon}"/>
    
    <lightning:card title="New Contact Creation">
        <lightning:input label="Contact First Name" value="{!v.con.FirstName}"/>
        <lightning:input label="Contact Last Name" value="{!v.con.LasttName}"/>
        <lightning:input label="Contact Email" value="{!v.con.Email}"/>
        
        <lightning:button label="save" onclick="{!c.saveme}"/>
    </lightning:card>
</aura:component>

Controller

({
    IniMe : function(component, event, helper) {
        component.find("mydata").getNewRecord(
                                                "Contact",
                                                null,
                                                false,
                                                $A.getCallback(function(){})
                                                );
        
    },
    saveme : function(component, event, helper){
        component.find("mydata").saveRecord(function(result){
            alert('Created'+result.recordId);
        });
    }
})

Application

<aura:application extends="force:slds">
    <c:LC_1603_LDSProgramming_CreateCon/>
    
</aura:application>
5
  • You are missing the whole exception handling in your LDS, also there is a typo in result.redordId – Raul Mar 16 at 13:54
  • Does this answer your question? ID of record created with force:recordData, the Lightning Data Service – Raul Mar 16 at 13:55
  • Ah I see, here is an example in ldsSaveRecordController.js. Basically in the saveRecord function, you need to check the state and exact error so as to notify the user about it. That would give you clue on why the insert fails. – Raul Mar 16 at 14:06
  • @Mahi Five of your last six questions have been problems caused by misspellings in your code. We're happy to help, but please take some time to carefully proofread your code. These errors are easily avoidable, and I'm sure it's frustrating to you too! – David Reed Mar 17 at 1:11
  • @DavidReed I am Checking the code quite a few times. Unable to find the error. Can't explain how disappointing it is if code fails bcz of small typo and unable to find. I am glad that you guys are kind enough to help everytime which makes me keep going. Is there any way to findout the location of error? Tq..David – Mahi Mar 17 at 5:50
1

You have a typo in a critical field for Contact creation - the LastName field.

Change this:

<lightning:input label="Contact Last Name" value="{!v.con.LasttName}"/>

To this:

<lightning:input label="Contact Last Name" value="{!v.con.LastName}"/>

And I'm pretty sure it'll work. Even with the typo mentioned in the comments, it'll still work, but you'll need to fix that too, to get a visible result in your component.

Also, to capture any errors on contact creation, you should check for errors:

component.find("mydata").saveRecord(function(result){
    if (result.state == 'ERROR'){
        alert('ERROR!! ' + result.error[0].message);
    }
    else {
        alert('Created!! ' + result.recordId);
    }
});

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.