You need to sign in to do that
Don't have an account?
Pritesh Khole 1
Lightning component on Lead Not saving the record
Hi Team,
I have created a lightning component on leads, however I am not able to save the record. Below is my code:
LeadParentPage.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller = "LeadLightningApexController" >
<aura:attribute name="recordDetails" type="Lead[]"/>
<aura:attribute name="simpleNewLead" type="Object"/>
<aura:attribute name="newLeadError" type="String"/>
<aura:attribute name="Lead"
type="Lead"
default="{ 'sobjectType': 'Lead', 'LastName': 'Unknown', 'Status':''}"/>
<aura:attribute name="er" type="boolean" default="false"/>
<!-- <force:recordData aura:id="leadRecordCreator"
layoutType="FULL"
targetRecord="{!v.newLead}"
targetFields="{!v.simpleNewLead}"
targetError="{!v.newLeadError}" /> -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="SIPEligibleReferral" type="String[]" default="None,Yes,No"/>
<aura:attribute name="parentOptions" type="List"/>
<aura:attribute name="dependentPicklist" type="Object"/>
<aura:attribute name="dependentOptions" type="List"/>
<aura:attribute name="disabledPick" type="Boolean" default="true"/>
<aura:attribute name="openModal" type="boolean" default="true"/>
<div class="slds-m-around--xx-large">
<aura:if isTrue="{!v.openModal}">
<!--Modal Box Start-->
<div role="dialog" class="slds-modal slds-fade-in-open ">
<div class="slds-modal__container">
<div class="slds-modal__header">
<button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.handleCloseModal}">
X
<span class="slds-assistive-text">Close</span>
</button>
<h1 class="slds-text-heading--medium">Lead Page</h1>
</div>
<!--Modal Box Header-->
<div class="slds-modal__content slds-p-around--medium">
<p><b>
<h1 class="slds-section-title--divider">Account Information</h1>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
Account <force:inputField aura:id="Lead Field" value="{!v.Lead.Account__c}"/>
</div>
</label>
<label class="slds-form-element__label" for="input-id-02">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Customer Company Name" value="{!v.Lead.Company}" />
</div>
</label>
<h2 class="slds-section-title--divider">Contact Information</h2>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
Contact <force:inputField aura:id="Lead Field" value="{!v.Lead.Contact__c}"/>
</div>
</label>
<br></br>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="First Name" value="{!v.Lead.FirstName}"/>
</div>
</label>
<label class="slds-form-element__label" for="input-id-02">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Last Name" value="{!v.Lead.LastName}" required ="true" />
</div>
</label>
<h3 class="slds-section-title--divider">Lead Details</h3>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Lead Name" value="{!v.Lead.Lead_Name__c}" />
</div>
<lightning:select name="parentPicklist" label="Lead Status" required= "true" aura:id="parentPicklist" onchange="{!c.PickChange}">
<option value="">None</option>
<aura:iteration items="{!v.parentOptions}" var="p">
<option value="{!p.value}" default= "New">{!p.text}</option>
</aura:iteration>
</lightning:select>
<lightning:select name="dependentPicklist" label="Reason For Disqualification" disabled="{!v.disabledPick}">
<option value="">None</option>
<aura:iteration items="{!v.dependentOptions}" var="d">
<option value="{!d.value}">{!d.text}</option>
</aura:iteration>
</lightning:select>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Reason Description" value="{!v.Lead.Reason__c}"/>
</div>
</label>
</b>
</p>
</div>
<!--Modal Box Button-->
<div class="slds-modal__footer">
<button class="slds-button slds-button--brand" onclick="{!c.handleCloseModal}">Cancel</button>
<button class="slds-button slds-button--brand" onclick="{!c.doSave}">Submit</button>
</div>
</div>
</div>
<div class="slds-backdrop slds-backdrop--open"></div>
</aura:if>
</div>
</aura:component>
LeadParentPageController.js
({
doInit : function(component, event, helper) {
var pVals = [
{text:"New", value: "New"},
{text:"Qualified", value: "Qualified"},
{text:"Unqualified", value: "Unqualified"}
];
var dPick = {
"Unqualified" : [
{text:"Change in Fleet / Not Applicable", value: "Change in Fleet / Not Applicable"},
]
};
component.set('v.parentOptions', pVals);
component.set('v.dependentPicklist', dPick);
helper.getAllRecords(component);
},
doSave : function(component, event, helper) {
var Lead = component.get("v.Lead");
helper.doSave(component, Lead);
},
PickChange : function(component, event, helper) {
var parentValue = component.find('parentPicklist').get('v.value');
component.set('v.dependentOptions', component.get('v.dependentPicklist')[parentValue]);
if(parentValue != '')
component.set('v.disabledPick',false);
else
component.set('v.disabledPick',true);
},
handleOpenModal: function(component, event, helper) {
//For Display Modal, Set the "openModal" attribute to "true"
component.set("v.openModal", true);
},
handleCloseModal: function(component, event, helper) {
//For Close Modal, Set the "openModal" attribute to "fasle"
component.set("v.openModal", false);
},
})
LeadParentPageHelper.js
({
getAllRecords: function(component){
var action = component.get("c.getAllRecords");
action.setCallback(this, function(response){
var state = response.getState();
if(component.isValid() && state =="Success"){
component.set("v.recordDetails", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
doSave: function(component, recordDetail){
this.upsertRecord(component, recordDetails, function(a){
var recordDetails = component.get("v.recordDetails");
recordDetails.push(a.getReturnValue());
component.set("v.recordDetails", recordDetails);
});
},
upsertRecord : function(component, recordDetail, callback){
var action = component.get("c.saveRecord");
action.setParams({
"recordDetail": recordDetail
});
if(callback){
action.setCallback(this, callback);
}
$A.enqueueAction(action);
}
})
LeadLightningApexController.apxc
public with sharing class LeadLightningApexController {
@AuraEnabled
public static List<Lead> getAllRecords(){
return [Select id, Name, Status From Lead];
}
@AuraEnabled
public static Lead saveRecord(Lead recordDetail){
upsert recordDetail;
return recordDetail;
}
}
I have created a lightning component on leads, however I am not able to save the record. Below is my code:
LeadParentPage.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller = "LeadLightningApexController" >
<aura:attribute name="recordDetails" type="Lead[]"/>
<aura:attribute name="simpleNewLead" type="Object"/>
<aura:attribute name="newLeadError" type="String"/>
<aura:attribute name="Lead"
type="Lead"
default="{ 'sobjectType': 'Lead', 'LastName': 'Unknown', 'Status':''}"/>
<aura:attribute name="er" type="boolean" default="false"/>
<!-- <force:recordData aura:id="leadRecordCreator"
layoutType="FULL"
targetRecord="{!v.newLead}"
targetFields="{!v.simpleNewLead}"
targetError="{!v.newLeadError}" /> -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="SIPEligibleReferral" type="String[]" default="None,Yes,No"/>
<aura:attribute name="parentOptions" type="List"/>
<aura:attribute name="dependentPicklist" type="Object"/>
<aura:attribute name="dependentOptions" type="List"/>
<aura:attribute name="disabledPick" type="Boolean" default="true"/>
<aura:attribute name="openModal" type="boolean" default="true"/>
<div class="slds-m-around--xx-large">
<aura:if isTrue="{!v.openModal}">
<!--Modal Box Start-->
<div role="dialog" class="slds-modal slds-fade-in-open ">
<div class="slds-modal__container">
<div class="slds-modal__header">
<button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.handleCloseModal}">
X
<span class="slds-assistive-text">Close</span>
</button>
<h1 class="slds-text-heading--medium">Lead Page</h1>
</div>
<!--Modal Box Header-->
<div class="slds-modal__content slds-p-around--medium">
<p><b>
<h1 class="slds-section-title--divider">Account Information</h1>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
Account <force:inputField aura:id="Lead Field" value="{!v.Lead.Account__c}"/>
</div>
</label>
<label class="slds-form-element__label" for="input-id-02">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Customer Company Name" value="{!v.Lead.Company}" />
</div>
</label>
<h2 class="slds-section-title--divider">Contact Information</h2>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
Contact <force:inputField aura:id="Lead Field" value="{!v.Lead.Contact__c}"/>
</div>
</label>
<br></br>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="First Name" value="{!v.Lead.FirstName}"/>
</div>
</label>
<label class="slds-form-element__label" for="input-id-02">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Last Name" value="{!v.Lead.LastName}" required ="true" />
</div>
</label>
<h3 class="slds-section-title--divider">Lead Details</h3>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Lead Name" value="{!v.Lead.Lead_Name__c}" />
</div>
<lightning:select name="parentPicklist" label="Lead Status" required= "true" aura:id="parentPicklist" onchange="{!c.PickChange}">
<option value="">None</option>
<aura:iteration items="{!v.parentOptions}" var="p">
<option value="{!p.value}" default= "New">{!p.text}</option>
</aura:iteration>
</lightning:select>
<lightning:select name="dependentPicklist" label="Reason For Disqualification" disabled="{!v.disabledPick}">
<option value="">None</option>
<aura:iteration items="{!v.dependentOptions}" var="d">
<option value="{!d.value}">{!d.text}</option>
</aura:iteration>
</lightning:select>
<label class="slds-form-element__label" for="input-id-01">
<div style="width:200px;" class="slds-form-element__control">
<lightning:input aura:id="LeadField" label="Reason Description" value="{!v.Lead.Reason__c}"/>
</div>
</label>
</b>
</p>
</div>
<!--Modal Box Button-->
<div class="slds-modal__footer">
<button class="slds-button slds-button--brand" onclick="{!c.handleCloseModal}">Cancel</button>
<button class="slds-button slds-button--brand" onclick="{!c.doSave}">Submit</button>
</div>
</div>
</div>
<div class="slds-backdrop slds-backdrop--open"></div>
</aura:if>
</div>
</aura:component>
LeadParentPageController.js
({
doInit : function(component, event, helper) {
var pVals = [
{text:"New", value: "New"},
{text:"Qualified", value: "Qualified"},
{text:"Unqualified", value: "Unqualified"}
];
var dPick = {
"Unqualified" : [
{text:"Change in Fleet / Not Applicable", value: "Change in Fleet / Not Applicable"},
]
};
component.set('v.parentOptions', pVals);
component.set('v.dependentPicklist', dPick);
helper.getAllRecords(component);
},
doSave : function(component, event, helper) {
var Lead = component.get("v.Lead");
helper.doSave(component, Lead);
},
PickChange : function(component, event, helper) {
var parentValue = component.find('parentPicklist').get('v.value');
component.set('v.dependentOptions', component.get('v.dependentPicklist')[parentValue]);
if(parentValue != '')
component.set('v.disabledPick',false);
else
component.set('v.disabledPick',true);
},
handleOpenModal: function(component, event, helper) {
//For Display Modal, Set the "openModal" attribute to "true"
component.set("v.openModal", true);
},
handleCloseModal: function(component, event, helper) {
//For Close Modal, Set the "openModal" attribute to "fasle"
component.set("v.openModal", false);
},
})
LeadParentPageHelper.js
({
getAllRecords: function(component){
var action = component.get("c.getAllRecords");
action.setCallback(this, function(response){
var state = response.getState();
if(component.isValid() && state =="Success"){
component.set("v.recordDetails", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
doSave: function(component, recordDetail){
this.upsertRecord(component, recordDetails, function(a){
var recordDetails = component.get("v.recordDetails");
recordDetails.push(a.getReturnValue());
component.set("v.recordDetails", recordDetails);
});
},
upsertRecord : function(component, recordDetail, callback){
var action = component.get("c.saveRecord");
action.setParams({
"recordDetail": recordDetail
});
if(callback){
action.setCallback(this, callback);
}
$A.enqueueAction(action);
}
})
LeadLightningApexController.apxc
public with sharing class LeadLightningApexController {
@AuraEnabled
public static List<Lead> getAllRecords(){
return [Select id, Name, Status From Lead];
}
@AuraEnabled
public static Lead saveRecord(Lead recordDetail){
upsert recordDetail;
return recordDetail;
}
}
Any update on this?
Regards
Pritesh