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
Sayantika DuttaSayantika Dutta 

I need to create a form, there will be few text boxes and after clicking the save button the records will be saved in the org.

public class CreateContactcls {
	@Auraenabled
    public static string createContact(Contact contObj){
    	//System.debug('contact::'+contObj.LastName);
        upsert contObj;
        return contObj.Id;
    }
}


<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="CreateContactcls">
	<aura:attribute name="contactObj" type="Contact" default="{'sobjectType':'Contact',
                                                              'FirstName':'',
                                                              'LastName':'',
                                                              'Phone':''}"/>
    <aura:attribute name="contactId" type="String"/>
    
    <article class="slds-card">
        <div class="slds-card__header slds-grid">
    <header class="slds-media slds-media_center slds-has-flexi-truncate">
        <div class="slds-media__figure">
            <span class="slds-icon_container slds-icon-standard-account" title="account">
                <lightning:icon iconName="standard:contact" alternativeText="Contact"/>
            </span>
        </div>
        <div class="slds-media__body">
            <h2 class="slds-card__header-title">
                <a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="Accounts">
                    <span>Contact</span>
                </a>
            </h2>
        </div>
        
            </header>
        </div>
        <div class="slds-card__body slds-card__body_inner">
            <div class="slds-size_1-of-3">
    	<lightning:input value="{!v.contactObj.FirstName}" label="First Name" required="true" placeholder="Enter first name..."/>
                   
        <lightning:button variant="neutral" label="Reset" type="Submit" iconName="utility:close" iconPosition="left" onclick="{!c.hide}" />
        <lightning:input value="{!v.contactObj.LastName}" label="Last Name" required="true" placeholder="Enter last name..."/>
    	<lightning:input value="{!v.contactObj.Phone}" label="Phone" required="true" placeholder="Enter phone no..." name="tel"  pattern="^[0-9_ ]*$" maxlength="11"
                     messageWhenPatternMismatch="Phone number is not valid"
                    
                    />
                <div class="slds-no-flex">
        <center><lightning:button variant="brand" label="Save" title="Save" onclick="{!c.doSave}"/></center>
                    
            
        </div>
        </div>
        </div>
    
            
            </article>
            
                     
</aura:component>


({
	doSave : function(component, event, helper) {
		var action = component.get("c.createContact");
        action.setParams({'contObj':component.get('v.contactObj')});
        action.setCallback(this,function(data){
            component.set('v.contactId',data.getReturnValue())
        });
        $A.enqueueAction(action);
       var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Success!",
            "message": "The record has been Created successfully."
        });
        toastEvent.fire();
        
    },
    checkValidity : function(component, event, helper) {
        var validity = event.getSource().get("v.validity");
        console.log(validity)
    },
      hide : function(component,event,helper){
        component.set('v.contactObj.FirstName','');
    },
    
    
    
})
When I'm clicking the save button, 

This page has an error. You might just need to refresh it. Action failed: c:ContactRecCreation$controller$doSave [Cannot read property 'setParams' of undefined] Failing descriptor: {c:ContactRecCreation$controller$doSave}

This error is coming. Can anyone please help me solving the issue?

 
Best Answer chosen by Sayantika Dutta
Maharajan CMaharajan C
Hi Sayantika,

Don't put the toastEvent after the enqueueAction.

Please use the below doSave JS method :
 
doSave : function(component, event, helper) {
        var action = component.get("c.createContact");
        //action.setParams({contObj : component.get('v.contactObj')});
        action.setParams({ contObj : component.get("v.contactObj")});
        action.setCallback(this,function(data){
            console.log(' response==> ' + data.getReturnValue());
            component.set('v.contactId',data.getReturnValue());
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
                "title": "Success!",
                "message": "The record has been Created successfully."
            });
            toastEvent.fire();
            
        });
        $A.enqueueAction(action);
    },


Thanks,
Maharajan.C