+ Start a Discussion
Sainath VenkatSainath Venkat 

lightning component to create a record from other object

Hello Everyone, I am working on one lightning component where I need to create a record using quick action.
I have two objects
1) Custom_Program_Course_junction__c
2) EXECED_Event_Class__c
On Custom_Program_Course_junction__c object, I need to have a quick action from which I should create a record in EXECED_Event_Class__c.
Since EXECED_Event_Class__c does not have a look up to Custom_Program_Course_junction__c object, I decided to create a lightning component and call it using quick action.
But some how my component is not working, I need to populate EXECED_Event_Class__c fields with Custom_Program_Course_junction__c fields.
Can anyone help me out in this issue. My code is below.

Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="Custom_Program_Course_junction__c" type="Object" />
    <aura:attribute name="simpleCustom_Program_Course_junction__c" type="Object" />
    <aura:attribute name="Custom_Program_Course_junction__cError" type="String" />
    <force:recordData aura:id="Custom_Program_Course_junction__cRecordLoader"
                      recordId="{!v.recordId}"
                      fields="Name,Course__c"
                      targetRecord="{!v.Custom_Program_Course_junction__c}"
                      targetFields="{!v.simpleCustom_Program_Course_junction__c}"
                      targetError="{!v.Custom_Program_Course_junction__cError}" />
    <aura:attribute name="newEXECED_Event_Class__c" type="Object" access="private" />
    <aura:attribute name="simpleNewEXECED_Event_Class__c" type="Object" access="private" />
    <aura:attribute name="newEXECED_Event_Class__cError" type="String" access="private" />
    <force:recordData aura:id="EXECED_Event_Class__cRecordCreator"
                     layoutType="FULL"
                     targetRecord="{!v.newEXECED_Event_Class__c}"
                     targetFields="{!v.simpleNewEXECED_Event_Class__c}"
                     targetError="{!v.newEXECED_Event_Class__cError}" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />   
    <div class="slds-modal__header">
        <p class="slds-text-heading--label">{!v.simpleCustom_Program_Course_junction__c.Name}</p>
        <h1 class="slds-page-header__title slds-m-right--small slds-truncate slds-align-left">Create new Class</h1>
    </div>
    <aura:if isTrue="{!not(empty(v.Custom_Program_Course_junction__cError))}">
    	<div class="recordError">
        	<ui:message title="Error" severity="error" closable="true">
            	{!v.Custom_Program_Course_junction__cError}
            </ui:message>
        </div>
    </aura:if>
    <aura:if isTrue="{!not(empty(v.Custom_Program_Course_junction__cError))}">
    	<div class="recordError">
        	<ui:message title="Error" severity="error" closable="true">
            	{!v.newEXECED_Event_Class__cError}
            </ui:message>
        </div>
    </aura:if>
    <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
    	<lightning:input aura:id="EXECED_Event_Class__cField" name="Start__c" label="Start" type="Date"
                     	value="{!v.simpleNewEXECED_Event_Class__c.Start__c}" required="true" />
    
        <lightning:input aura:id="EXECED_Event_Class__cField" name="End__c" label="End" type="Date"
                         value="{!v.simpleNewEXECED_Event_Class__c.End__c}" required="true" />
    </div>
    <div class="slds-modal__footer">
        <lightning:button class="slds-button_neutral" label="Save" onclick="{!c.handleSaveEXECED_Event_Class__c}"/>
        <lightning:button class="slds-button_brand" label="Cancel" onclick="{!c.cancel}"/>
    </div>
</aura:component>
.JS:
({
	doInit : function(component, event, helper) {
        component.find("EXECED_Event_Class__cRecordCreator").getNewRecord(
        	"EXECED_Event_Class__c",
            null,
            false,
            $A.getCallback(function(){
                var rec = component.get("v.newEXECED_Event_Class__c");
                var error = component.get("v.newEXECED_Event_Class__cError");
                if(error || (rec === null)){
                    console.log("Error initializing record template: " + error);
                }else{
                    console.log("Record template initialized" + rec.sobjectType);
                }
            })
        );
	},

    handleSaveEXECED_Event_Class__c : function(component, event, helper){
        component.set("v.simpleNewEXECED_Event_Class__c.Course__c", component.get("v.simpleCustom_Program_Course_junction__c.Course__c"));
        component.set("v.simpleNewEXECED_Event_Class__c.Name", component.get("v.simpleCustom_Program_Course_junction__c.Name"));
        component.find("EXECED_Event_Class__cRecordCreator").saveRecord(function(saveResult){
            if(saveResult.state === "SUCCESS" || saveResult.state === "DRAFT"){
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title" : "Class saved",
                    "message" : "The new Class was created",
                    "type" : "success"
                });

                $A.get("e.force:closeQuickAction").fire();
                resultsToast.fire();
                $A.get("e.force:refreshView").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));
            }
        });
    },

    cancel : function(component, event, helper){
        $A.get("e.force:closeQuickAction").fire();
    }


})


 
Best Answer chosen by Sainath Venkat
Sainath VenkatSainath Venkat
I achieved this one finally.
Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	 <aura:attribute name="Custom_Program_Course_junction__c" type="Custom_Program_Course_junction__c"/>
    <aura:attribute name="Custom_Program_Course_junction__cObj" type="Custom_Program_Course_junction__c"/>
    <aura:attribute name="errorMessage" type="String"/>
    <force:recordData aura:id="Course__cRecordLoader"
        recordId="{!v.recordId}"
        fields="Course__c,CourseTextName__c,KFS_Number__c"
        targetRecord="{!v.Custom_Program_Course_junction__c}"
        targetFields="{!v.Custom_Program_Course_junction__cObj}"
        targetError="{!v.errorMessage}"
        recordUpdated="{!c.doInit}"
    /> 
</aura:component>
Controller:
({
 doInit : function(component, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
            //console.log("account loaded:::::" + JSON.stringify(component.get("v.Custom_Program_Course_junction__c")));
           
            var createRecordEvent = $A.get("e.force:createRecord");
            createRecordEvent.setParams({
                "entityApiName": "EXECED_Event_Class__c",
                "defaultFieldValues":{
                    "Course__c" : component.get("v.Custom_Program_Course_junction__cObj.Course__c"),
                    "Name" : component.get("v.Custom_Program_Course_junction__cObj.CourseTextName__c"),
                    "KFS_Number__c" : component.get("v.Custom_Program_Course_junction__cObj.KFS_Number__c")
                }
            });
            createRecordEvent.fire();
           
        } else if(eventParams.changeType === "REMOVED") {
        } else if(eventParams.changeType === "ERROR") {
        }
 }
})


 

All Answers

Maharajan CMaharajan C
add the force:lightningQuickAction,force:hasRecordId Interfaces in your fi.

Tell me one thing whether the below line is printing the name or Not?

<p class="slds-text-heading--label">{!v.simpleCustom_Program_Course_junction__c.Name}</p> 

Thanks,
Maharajan.C
Sainath VenkatSainath Venkat
Hello Maharajan
Hope you doing good.
for your question, yes it is showing the output, I mean the name is getting displayed.
Maharajan CMaharajan C
Your Component is creating the EXECED_Event_Class__c rceord or not when save is clicked ? And Please explain your issue Properly.
Sainath VenkatSainath Venkat
On clicking save, its not creating anything. I mean when I click on save button it is doing nothing, the quick action remains same and it is doing nothing.
Not saving record or not showing any error messages.
Sainath VenkatSainath Venkat
I achieved this one finally.
Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	 <aura:attribute name="Custom_Program_Course_junction__c" type="Custom_Program_Course_junction__c"/>
    <aura:attribute name="Custom_Program_Course_junction__cObj" type="Custom_Program_Course_junction__c"/>
    <aura:attribute name="errorMessage" type="String"/>
    <force:recordData aura:id="Course__cRecordLoader"
        recordId="{!v.recordId}"
        fields="Course__c,CourseTextName__c,KFS_Number__c"
        targetRecord="{!v.Custom_Program_Course_junction__c}"
        targetFields="{!v.Custom_Program_Course_junction__cObj}"
        targetError="{!v.errorMessage}"
        recordUpdated="{!c.doInit}"
    /> 
</aura:component>
Controller:
({
 doInit : function(component, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
            //console.log("account loaded:::::" + JSON.stringify(component.get("v.Custom_Program_Course_junction__c")));
           
            var createRecordEvent = $A.get("e.force:createRecord");
            createRecordEvent.setParams({
                "entityApiName": "EXECED_Event_Class__c",
                "defaultFieldValues":{
                    "Course__c" : component.get("v.Custom_Program_Course_junction__cObj.Course__c"),
                    "Name" : component.get("v.Custom_Program_Course_junction__cObj.CourseTextName__c"),
                    "KFS_Number__c" : component.get("v.Custom_Program_Course_junction__cObj.KFS_Number__c")
                }
            });
            createRecordEvent.fire();
           
        } else if(eventParams.changeType === "REMOVED") {
        } else if(eventParams.changeType === "ERROR") {
        }
 }
})


 
This was selected as the best answer