0

In lightning:recordForm we give objectAPIName but in force:recordForm we do not specify any such attribute So, How does force:recordData identify the sObjectType?

Assuming there is no value for recordId attribute in case of creating a new record.

1 Answer 1

1

if you want to create record by using force:recordData you don't need pass value for recordId attribute in force:recordData , that's how LDS detect it's for New record action.

How does force:recordData identify the sObjectType?

you to have to use getNewRecord method for loads the form and set to requested targetRecord attribute value, usually this method calls from doinit method. here is example code below.

cmp

 <aura:attribute name="newContact" type="Object"/>
<aura:attribute name="simpleNewContact" type="Object"/>
<aura:attribute name="newContactError" type="String"/>

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

<force:recordData aura:id="contactRecordCreator" 
                  layoutType="FULL"
                  targetRecord="{!v.newContact}"
                  targetFields="{!v.simpleNewContact}"
                  targetError="{!v.newContactError}" />
 <div class="slds-p-horizontal--small">              
            <lightning:input aura:id="contactField" label="Last Name" value="{!v.simpleNewContact.LastName}"/>                
            <br/>
            <lightning:button label="Save Contact" variant="brand" onclick="{!c.handleSaveContact}"/>
        </div>

controller.js

({
doInit: function(component, event, helper) {
    // Prepare a new record from template
    component.find("contactRecordCreator").getNewRecord(
        "Contact", // sObject type (objectApiName)
        null,      // recordTypeId
        false,     // skip cache?
        $A.getCallback(function() {
            var rec = component.get("v.newContact");
            var error = component.get("v.newContactError");
            if(error || (rec === null)) {
                // handle error
            }
            console.log("Record template initialized: " + rec.apiName);
        })
    );
},
// for create new contact record
handleSaveContact: function(component, event, helper) {
    if(helper.validateContactForm(component)) {            
        component.find("contactRecordCreator").saveRecord(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // record is saved successfully                

            } else if (saveResult.state === "INCOMPLETE") {
                // handle the incomplete state
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                // handle the error state
                console.log('Problem saving contact, error: ' + 
     JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        });
    }
}

})

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .