+ Start a Discussion
MisbahTehseen Parathanalli 3MisbahTehseen Parathanalli 3 

creating a record in a custom object using the form approach in lightning component, but it is returning a null id.

creating a record in a custom object using the form approach in lightning component, but it is returning a null id.
following is the code of component:-
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId"
                access="global" controller="createUserRecord">
    <aura:attribute name="newUser" type="User_Request__c" default="{'sobjectType':'User_Request__c',
                                                                   'First_Name__c':'',
                                                                   'Last_Name__c    ':'',
                                                                   'Email__c':'',
                                                                   'X521_ID__c':'',
                                                                   'Company_Phone__c':'',
                                                                   'Model_User__c':'',
                                                                   'Requestor_s_521__c':'',
                                                                   'Username__c':'',
                                                                   'Manager_Email__c':'',
                                                                   'Manager__c':'false' ,
                                                                   'Country_Code__c':'',
                                                                   'Deactivation_Date__c':''}"  />

        <fieldset class="myBoxArea">
    <lightning:layout verticalAlign="end">
        <lightning:layoutItem >
            <div class="blue">                 
                envision CRM and Commercial Application Access Request ALCON 
            </div>
            <div class="white">
            </div>
        </lightning:layoutItem>
    </lightning:layout>
        <div class="slds-align_absolute-center slds-box lightblue" style="height: 5rem;">
            USER INFORMATION
        </div>  
        <lightning:layout class="slds-box">
            <lightning:layoutItem size="6" class="left-align">
                <div >
                    <form class="slds-form slds-form_horizontal">          
                        <lightning:input aura:id="itemform" label="First Name"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.First_Name__c}"/> 
                        <lightning:input aura:id="itemform" label="Username"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.Username__c}"/> 
                        <lightning:input aura:id="itemform" label="Manager's Email"
                                         name="itemname"
                                         type="email"
                                         value="{!v.newUser.Manager_Email__c}"/>
                        <lightning:input aura:id="itemform" label="Copy profile from"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.Model_User__c}"/>
                        <lightning:input aura:id="itemform" label="Access End Date"
                                         name="itemname"
                                         type="date"
                                         value="{!v.newUser.Deactivation_Date__c}"/> 
                        <lightning:input type="toggle" label="Manager"
                                         name="option1"
                                         aura:id="itemform" 
                                         value="{!v.newUser.Manager__c}"/> 
                    </form>
                </div>
            </lightning:layoutItem>
            <lightning:layoutItem size="6" class="right-align">
                <div >
                    <form class="slds-form slds-form_horizontal">          
                        <lightning:input aura:id="itemform" label="Last Name"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.Last_Name__c}"/> 
                        <lightning:input aura:id="itemform" label="Email Address"
                                         name="itemname"
                                         type="email"
                                         value="{!v.newUser.Email__c}"/>
                        <lightning:input aura:id="itemform" label="User 5-2-1"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.X521_ID__c}"/>
                        <lightning:input aura:id="itemform" label="Company/Mobile Phone #"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.Company_Phone__c}"/> 
                        <lightning:input aura:id="itemform" label="Requestor 5-2-1"
                                         name="itemname"
                                         type="input text"
                                         value="{!v.newUser.Requestor_s_521__c}"/> 
                        <lightning:input aura:id="itemform" label="Country"
                                         name="itemname"
                                         type="input text"
                                         required="true"
                                         value="{!v.newUser.Country_Code__c}"/>
                    </form>
                </div>
            </lightning:layoutItem>
        </lightning:layout>
        <lightning:layout>
            <lightning:layoutItem class="slds-align_absolute-center">        
                        <lightning:button label="Send for approval" 
                                          class="slds-m-top--medium"
                                          variant="brand"
                                          onclick="{!c.save}"/>
            </lightning:layoutItem>
        </lightning:layout>      
    </fieldset>
</aura:component>
apex class is:-
public class createUserRecord {
    @auraenabled
    public static id createUserRecordNew(User_Request__c UR){
     upsert UR;
    return UR.id;
}    }
controller i am using is:-
({
 save : function(component, event, helper) {     
     var action = component.get("c.createUserRecordNew");
            action.setParams({"UR":component.get("v.newUser")});
            action.setCallback(this,function(result){
            component.set("v.isShow",false);
            var userId = result.getReturnValue();
            alert('userId'+userId); 
        });
         $A.enqueueAction(action);
 }
})
app for execution:-
<aura:application extends="force:slds">
    <c:newTest/>    
</aura:application>
Raj VakatiRaj Vakati
The following code looks good and there might be some error while saving the data so change you code as below and try  ..If you get any error then we will see how to fix it .. 




 
({
 save : function(component, event, helper) {     
     var action = component.get("c.createUserRecordNew");
            action.setParams({"UR":component.get("v.newUser")});
            action.setCallback(this,function(result){
			var state = result.getState();
            if (state === "SUCCESS") {

            component.set("v.isShow",false);
            var userId = result.getReturnValue();
            alert('userId'+userId); 
			}
			else  {
			var errors = response.getError();

				alert('Erooor'+errors); 

			}
        });
         $A.enqueueAction(action);
 }
})

 
Dinesh_Kumar_NagarathinamDinesh_Kumar_Nagarathinam
Why not you use the lightning data service? So that you no need to call the server call. Also you will get the record id too. Follow the below link
https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/example#lightningcomponentdemo:exampleRecordCreate