function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SrtSrt 

Lightning component basics question - creating a record

Below are my codes:
 
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
Best Answer chosen by Srt
sfdcMonkey.comsfdcMonkey.com
hi your javaScript controller update the line below :

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

sfdcMonkey.comsfdcMonkey.com
hi your javaScript controller update the line below :

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 
This was selected as the best answer
sfdcMonkey.comsfdcMonkey.com
//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');
            }
SrtSrt
Hi Piyush_soni

Thanks for your help. It is working now.