You need to sign in to do that
Don't have an account?
Srt
Lightning component basics question - creating a record
Below are my codes:
When I click on create , The record is getting inserted, but I am getting error as below:
This page has an error. You might just need to refresh it.
Error in $A.getCallback() [key.indexOf is not a function]
Callback failed: apex://createRecord/ACTION$createCampRec
Failing descriptor: {c:Camp1}
Please help
Component: <aura:component controller="createRecord"> <aura:attribute name="Campitem" type="Camp_Item__c" default="{'sobjectType': 'Camp_item__C', 'Name' : '', 'Description__c': '' }" /> <lightning:input label="Name" type="Text" name="Name" value="{!v.Campitem.Name}" /> <lightning:input label="Description" type="text" name="desc" value="{!v.Campitem.Description__c}" /> <lightning:button label="Create" onclick="{!c.createRec}"/> </aura:component>
Controller: ({ createRec : function(component, event, helper) { //getting the candidate information var camp = component.get("v.Campitem"); //Validation if($A.util.isEmpty(camp.Name) || $A.util.isUndefined(camp.Name)){ alert('Name is Required'); return; } if($A.util.isEmpty(camp.Description__c) || $A.util.isUndefined(camp.Description__c)){ alert('Desc is Required'); return; } //Calling the Apex Function var action = component.get("c.createCampRec"); console.log(camp); //Setting the Apex Parameter action.setParams({ cp : camp }); //Setting the Callback action.setCallback(this,function(a){ //get the response state var state = a.getState(); //check if result is successfull if(state == "SUCCESS"){ //Reset Form var ncamp = {'sobjectType': 'Camp_item__C', 'Name' : '', 'Description__c': '' } component.set({"v.Campitem": ncamp }); alert('Record is Created Successfully'); } else if(state == "ERROR"){ alert('Error in calling server side action'); } }); //adds the server-side action to the queue $A.enqueueAction(action); } })
Apex class: public with sharing class createRecord { @AuraEnabled public static void createCampRec(Camp_item__c cp ) { try{ System.debug('Check '+cp); if(cp != null){ insert cp; } } catch (Exception ex){ } } }
When I click on create , The record is getting inserted, but I am getting error as below:
This page has an error. You might just need to refresh it.
Error in $A.getCallback() [key.indexOf is not a function]
Callback failed: apex://createRecord/ACTION$createCampRec
Failing descriptor: {c:Camp1}
Please help
use component.set("v.Campitem" , ncamp); insteadOf
component.set({"v.Campitem": ncamp });i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others ,thanks
sfdcmonkey.com
All Answers
use component.set("v.Campitem" , ncamp); insteadOf
component.set({"v.Campitem": ncamp });i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others ,thanks
sfdcmonkey.com
Thanks for your help. It is working now.