You need to sign in to do that
Don't have an account?
unable to save 'child results' into 'parent component' while search results
Hi All,
I can able to fetch select 'Search Results' in child component from Parent Component.
But when i try to save the record , its unable to save the record.
can some one pls check and let me know whats wrong in the code
Event :
skillCompEvent :::
<aura:event type="COMPONENT">
<aura:attribute name="recordByEvent" type="List"/>
</aura:event>
----------------------- child Comp--------------
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="GlobalSearchHandler">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
<aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>
<aura:attribute name="showSearchResults" type="Boolean" default="false"/>
<aura:attribute name="searchKey" type="String"/>
<aura:attribute name="accountList" type="List" default="Skill_Management__c[]"/>
<aura:attribute name="accountColumns" type="List"/>
<aura:attribute name="Spinner" type="boolean" default="false"/>
<aura:attribute name="label" type="string" default="" description="Label will be displayed above input Box" />
<aura:attribute name="selectedAccts" type="List"/>
<aura:attribute name="value" type="String" default="" />
<aura:registerEvent name="oSelectedRecordEvent" type="c:skillCompEvent"/>
<aura:if isTrue="{!v.Spinner}">
<div aura:id="spinnerId" class="slds-spinner_container">
<div class="slds-spinner--brand slds-spinner slds-spinner--large slds-is-relative" role="alert">
<span class="slds-assistive-text">Loading</span>
<div class="slds-spinner__dot-a"></div>
<div class="slds-spinner__dot-b"></div>
</div>
</div>
</aura:if>
<!-- Loading spinner end-->
<lightning:input name="searchKey" placeholder="Search Skill Management" value="{!v.searchKey}"/>
<lightning:button variant="brand" label="Search" title="Search" onclick="{!c.search}" class="SearchButton"/>
<!-- </div> -->
<aura:if isTrue="{!v.showSearchResults}">
<lightning:layout multipleRows="true">
<lightning:layoutItem padding="around-small" size="12">
<span style="font-size:16px;font-weight:bold">Skill Management</span>
<lightning:datatable keyField="id"
data="{!v.accountList}"
columns="{!v.accountColumns}"
maxRowSelection="1"
onrowselection="{! c.getSelectedName }"
/>
</lightning:layoutItem>
</lightning:layout>
</aura:if>
</aura:component>
controller:
({
doInit : function(component, event, helper) {
component.set('v.accountColumns', [
{label: 'Name', fieldName: 'Name', type: 'text'},
{label: 'Skill Category', fieldName: 'Skill_Category__c', type: 'text'},
{label: 'Skill', fieldName: 'Skills__c', type: 'text'}
]);
},
search : function(component, event, helper) {
helper.getSearchResultsFromHandler(component,helper);
component.set("v.showSearchResults",true);
},
// this function automatic call by aura:waiting event
showSpinner: function(component, event, helper) {
// make Spinner attribute to false for hide loading spinner
component.set("v.Spinner", false);
},
getSelectedName: function (component, event,helpder) {
var selectedRows = event.getParam('selectedRows');
var setRows = [];
// Display that fieldName of the selected rows
for (var i = 0; i < selectedRows.length; i++){
setRows.push(selectedRows[i]);
}
component.set('v.value',selectedRows);
var compEvent = component.getEvent("oSelectedRecordEvent");
alert('called compEvent');
compEvent.setParams({
"recordByEvent" : setRows[0]
});
compEvent.fire();
// ended code
}
})
Helper :
({
getSearchResultsFromHandler : function(component,helper){
var action = component.get("c.getSearchRecords");
action.setParams({ searchKey : component.get("v.searchKey") });
// callback that is executed after the server-side action returns
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var result = response.getReturnValue();
// SOSL will always return the list in the order they were queried
component.set("v.accountList",result[0]);
}
else if (state === "INCOMPLETE") {
// do something
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +errors[0].message);
}
}
else {
console.log("Unknown error");
}
}
});
$A.enqueueAction(action);
},
})
---------------------Parent component---------------
<aura:component controller="PickListController" implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader">
<aura:attribute name="newresource" type="Resourse__c"/>
<aura:attribute name="newSkill" type="Skill__c" default="{ 'sobjectType': 'Skill__c'}"/>
<aura:attribute name="recordId" type="String"/>
<aura:attribute name="picvalue" type="List"/>
<aura:attribute name="value" type="String" />
<aura:attribute name="selectedSkills" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:handler name="oSelectedRecordEvent" event="c:skillCompEvent" action="{!c.handleComponentEvent}"/>
<div class="slds-page-header" role="banner">
<h2 class="slds-page-header__title slds-m-right_small
slds-align_absolute-center">Add Skill</h2>
</div>
<c:GlobalSearchComponent accountList="Skill_Management__c" label="Skill Management" value="{!v.value}" />
<lightning:select value="{!v.newSkill.Rating__c}" label="Rating">
<option value="choose">Choose one...</option>
<aura:iteration items="{!v.picvalue}" var="s">
<option value="{!s}">{!s}</option>
</aura:iteration>
</lightning:select>
<br/> <br/>
<lightning:layout>
<lightning:layoutItem>
<lightning:button label="Cancel" onclick="{!c.handleCancel}" class="slds-m-top_medium" />
<lightning:button label="Create New SKill" variant="brand" onclick="{!c.handleSaveContact}" class="slds-m-top_medium"/>
</lightning:layoutItem>
</lightning:layout>
</aura:component>
controller :
({
doInit : function(component) {
var pickvar = component.get("c.getPickListValuesIntoList");
pickvar.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list = response.getReturnValue();
component.set("v.picvalue", list);
}
else if(state === 'ERROR'){
alert('ERROR OCCURED.');
}
})
$A.enqueueAction(pickvar);
var action = component.get("c.getResource");
action.setParams({"resourceId": component.get("v.recordId")});
// Configure response handler
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
component.set("v.account", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
},
// This function call when the end User Select any record from the result list.
handleComponentEvent : function(component, event, helper) {
var selectedAccountGetFromEvent = event.getParam("recordByEvent");
component.set("v.value" , selectedAccountGetFromEvent);
},
handleSaveContact: function(component, event, helper) {
var action = component.get("c.insertValues");
action.setParams({
ac : component.get("v.newSkill"),
"resourceId": component.get("v.recordId")
});
action.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list = response.getReturnValue();
component.set("v.picvalue", list);
component.set("v.newSkill", list);
// Prepare a toast UI message
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"message": "The Record Is Created Sucessfully"
});
// Update the UI: close panel, show toast, refresh account page
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
else if(state === 'INCOMPLETE'){
alert('Something is missing');
}
else if(state === 'ERROR'){
alert('Insertion Failed');
}
})
$A.enqueueAction(action);
},
handleCancel: function(component, event, helper) {
$A.get("e.force:closeQuickAction").fire();
}
})
Apex class :
public class PickListController {
@AuraEnabled
public static List<String> getPickListValuesIntoList(){
List<String> pickListValuesList = new List<String>();
Schema.DescribeFieldResult fieldResult = Skill__c.Rating__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
pickListValuesList.add(pickListVal.getLabel());
System.debug('Values in Rating are: '+pickListValuesList);
}
return pickListValuesList;
}
@AuraEnabled
public static Skill__c insertValues(Skill__c ac,Id resourceId){
system.debug('acc'+ac);
insert ac;
return ac;
}
}