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
shubham soitkar 6shubham soitkar 6 

I'm not able to insert record in object, don't know what's going wrong.

Aura Component:-

<aura:component controller="createAccount" implements="force:hasRecordId,flexipage:availableForRecordHome" >
    <aura:attribute name="createAccount" type="Account[]" default="{
                                                                   
                                                          'sObjectType' : 'Account',
                                                          'FirstName'   : ' ' ,
                                                          'LastName'    : ' ',
                                                          'BillingAddress' : ' ',
                                                          'Rating' : ' '        
                    
                                                                        }" />
  <aura:attribute name="starRating" type="List" default="[
    {'label': 'Hot', 'value': 'Hot'},
    {'label': 'Warm', 'value': 'Warm'},
    {'label': 'Cold', 'value': 'Cold'},
    ]"/> 
    
    <lightning:input aura:id="input1" label="FirstName" value="{!v.createAccount.FirstName}" required="True"/>
    <lightning:input aura:id="input2" label="LastName" value="{!v.createAccount.LastName}" required="True"/>
    <lightning:input aura:id="input3" label="Address" value="{!v.createAccount.BillingAddress}" required="True"/>
     <lightning:combobox name="sRating" label="Rating" value="None" placeholder="Select Progress" options="{!v.starRating }" 
                                                            onchange="{!c.handleChange }"/> 

     <lightning:button title="Neutral action" 
                                  label="Save"   
                                  onclick="{!c.handleClick}"/>
    
    </aura:component>

JS Controller:-
({
    handleChange : function(component, event, helper) {
        
        let selectedOptionValue = event.getParam("value"); 
        component.set("v.createAccount.Rating",selectedOptionValue);
        let values= component.get("v.createAccount.Rating");
        alert(values); 
        console.log(values);
    },
    
    handleClick : function(component, event, helper) {
        var target = event.getSource();
        var id = target.get("v.label");     
        alert(id);
        
         var action = component.get("c.createAcc");
        action.setParams({ 
         Acc : component.get("v.createAccount") });
 
        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('Record added');// Alert the user with the value returned 
                // from the server
                alert("From server: " + response.getReturnValue());
                 console.log('Sucessfully added content to Object');
                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            else if (state === "INCOMPLETE") {
              alert("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");
                }
            }
        });
 
        // optionally set storable, abortable, background flag here
 
        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    

    
    }     
})

Apex Code:-

public class createAccount {
    
    @AuraEnabled
    public static Account createAcc(Account Acc){
                  
            insert Acc;
       
        return Acc;
    }
}
Best Answer chosen by shubham soitkar 6
Maharajan CMaharajan C
Hi Shubham,

1. For Account record creation Account Name Field is Mandatory. I don't know why you are using the FirstName ,LastName.  If you are using this for Person Account or Contact creation then first name and last name is fine otherwiae use Name.

2, you can't use the billing address directly. It is a compound field. so you have to refer seperately like BillingStreet, BillingCity, BillingState...

3. Put a debug log or use developer console log to see any backend exception is happening. Because you are not using the Aurahandled exception so you won't get any error from apex class in client side controller. because i am thinking there is error in apex class data insertion. use try catch also in class and see the error.

Thanks,
Maharajan.C

All Answers

SwethaSwetha (Salesforce Developers) 
Do you see any error message?Thanks
Sachin HoodaSachin Hooda
You're referencing incorrect method name,
var action = component.get("c.createAcc")
should be 
var action = component.get("c.createAccount")
Basically you're trying to queue a server side action which doesnt exist. So the code will fail silently.
Additionally, I would suggest changing either the method name of the attribute on your client side.
Maharajan CMaharajan C
Hi Shubham,

1. For Account record creation Account Name Field is Mandatory. I don't know why you are using the FirstName ,LastName.  If you are using this for Person Account or Contact creation then first name and last name is fine otherwiae use Name.

2, you can't use the billing address directly. It is a compound field. so you have to refer seperately like BillingStreet, BillingCity, BillingState...

3. Put a debug log or use developer console log to see any backend exception is happening. Because you are not using the Aurahandled exception so you won't get any error from apex class in client side controller. because i am thinking there is error in apex class data insertion. use try catch also in class and see the error.

Thanks,
Maharajan.C
This was selected as the best answer