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
Lokesh Rayapati4Lokesh Rayapati4 

Create a form to insert account withoutt record forms using Aura component

Hi, 
 I'm new to Aura components concept. Iam trying to Create a form to insert account withoutt record forms using Aura component. Please help me in solving this.

Thank you in advance
Best Answer chosen by Lokesh Rayapati4
PriyaPriya (Salesforce Developers) 
Hi Lokesh,

Follow the steps below to create an Account Records Using Salesforce Lightning Component without using record form :- 

1. Create a Lightning Component 
 
<aura:component controller=”createAccountRecords”>
    <aura:attribute name=”createAcc” type=”Account” default=”{‘sObjectType’ : ‘Account’,’Name’ : ”,’Rating’ : ”}”/>
    <aura:attribute name=”objName” type=”String” default=”Account”/>
    <aura:attribute name=”fldName” type=”String” default=”Rating”/>
    <aura:attribute name=”ratingList” type=”List”/>
    <aura:handler name=”init” value=”{!this}” action=”{!c.doInit}”/>
    <div class=”slds-p-around_small”>
        <lightning:input type=”Text” label=”Name” value=”{!v.createAcc.Name}”/>
        <lightning:input type=”Text” label=”Account Number” value=”{!v.createAcc.AccountNumber}”/>
        <lightning:input type=”Email” name=”email2″ value=”{!v.createAcc.Email}” label=”Email ID”/>
        <lightning:input type=”Phone” label=”Phone Number” value=”{!v.createAcc.Phone}”/>
        <lightning:select label=”Rating” value=”{!v.createAcc.Rating}”>
            <option value=””>—None—</option>
            <aura:iteration items=”{!v.ratingList}” var=”ac”>
                <option value=”{!ac}”>{!ac}</option>
            </aura:iteration>
        </lightning:select>
        <lightning:button label=”Save” iconPosition=”left” variant=”brand” onclick=”{!c.doSave}”/>
        <lightning:button label=”Cancel” iconPosition=”right” variant=”destructive” onclick=”{!c.docancel}”/>
    </div>
</aura:component>

2. Create a Lightning Controller for the component
 
({
    doInit : function(component, event, helper) {
        var action = component.get(‘c.getPickList’);
        action.setParams({
            objName : component.get(‘v.objName’),
            fldName : component.get(‘v.fldName’)
        });
        action.setCallback(this,function(result){
            var resultValue = result.getReturnValue();
            component.set(‘v.ratingList’,resultValue);
        });
        $A.enqueueAction(action);
    },
    doSave : function(component, event, helper) {
        var action = component.get(‘c.createAccount’);
        action.setParams({
            ac : component.get(‘v.createAcc’)
        });
        action.setCallback(this,function(result){
            var getAllValue = component.get(‘v.createAcc’);
            alert(JSON.stringify(getAllValue));
        });
        $A.enqueueAction(action);
    },
    docancel : function(component, event, helper) {
        component.set(‘v.createAcc’,”);
    },
})

3. Create an Apex class and define the method to create the record
public class createAccountRecords {
    @AuraEnabled
    public static List<String> getPickList(String objName,String fldName) {
        List<String> pkList = new List<String>();Map<String,Schema.SObjectType> allObj = Schema.getGlobalDescribe();
        Map<String,Schema.SObjectField> allFlds = allObj.get(objName).getDescribe().fields.getMap();
        List<Schema.PicklistEntry> pickList = allFlds.get(fldName).getDescribe().getPickListValues();
        for(Schema.PicklistEntry pk : pickList){
            pkList.add(pk.getValue());
        }
        return pkList;
    }
    @AuraEnabled
    public static Account createAccount(Account ac){
        insert ac;
        return ac;
    }
}

4.  Create a Lightning application which will host the component in the App
<aura:application extends=”force:slds” >
    <c:createAccountRecords/>
</aura:application>


5. Test the Application

Save and Click on Update Preview to view your created Application. Your page will look something like this.
User-added image

Note:- You might need to modify it as per your requirement, this is just a sample code.​​​​​​​​​​​​​​

PLEASE MARK IT AS BEST ANSWER SO THAT IT CAN HELP OTHERS TOO.

Regards,

Priya Ranjan

All Answers

PriyaPriya (Salesforce Developers) 
Hi Lokesh,

Follow the steps below to create an Account Records Using Salesforce Lightning Component without using record form :- 

1. Create a Lightning Component 
 
<aura:component controller=”createAccountRecords”>
    <aura:attribute name=”createAcc” type=”Account” default=”{‘sObjectType’ : ‘Account’,’Name’ : ”,’Rating’ : ”}”/>
    <aura:attribute name=”objName” type=”String” default=”Account”/>
    <aura:attribute name=”fldName” type=”String” default=”Rating”/>
    <aura:attribute name=”ratingList” type=”List”/>
    <aura:handler name=”init” value=”{!this}” action=”{!c.doInit}”/>
    <div class=”slds-p-around_small”>
        <lightning:input type=”Text” label=”Name” value=”{!v.createAcc.Name}”/>
        <lightning:input type=”Text” label=”Account Number” value=”{!v.createAcc.AccountNumber}”/>
        <lightning:input type=”Email” name=”email2″ value=”{!v.createAcc.Email}” label=”Email ID”/>
        <lightning:input type=”Phone” label=”Phone Number” value=”{!v.createAcc.Phone}”/>
        <lightning:select label=”Rating” value=”{!v.createAcc.Rating}”>
            <option value=””>—None—</option>
            <aura:iteration items=”{!v.ratingList}” var=”ac”>
                <option value=”{!ac}”>{!ac}</option>
            </aura:iteration>
        </lightning:select>
        <lightning:button label=”Save” iconPosition=”left” variant=”brand” onclick=”{!c.doSave}”/>
        <lightning:button label=”Cancel” iconPosition=”right” variant=”destructive” onclick=”{!c.docancel}”/>
    </div>
</aura:component>

2. Create a Lightning Controller for the component
 
({
    doInit : function(component, event, helper) {
        var action = component.get(‘c.getPickList’);
        action.setParams({
            objName : component.get(‘v.objName’),
            fldName : component.get(‘v.fldName’)
        });
        action.setCallback(this,function(result){
            var resultValue = result.getReturnValue();
            component.set(‘v.ratingList’,resultValue);
        });
        $A.enqueueAction(action);
    },
    doSave : function(component, event, helper) {
        var action = component.get(‘c.createAccount’);
        action.setParams({
            ac : component.get(‘v.createAcc’)
        });
        action.setCallback(this,function(result){
            var getAllValue = component.get(‘v.createAcc’);
            alert(JSON.stringify(getAllValue));
        });
        $A.enqueueAction(action);
    },
    docancel : function(component, event, helper) {
        component.set(‘v.createAcc’,”);
    },
})

3. Create an Apex class and define the method to create the record
public class createAccountRecords {
    @AuraEnabled
    public static List<String> getPickList(String objName,String fldName) {
        List<String> pkList = new List<String>();Map<String,Schema.SObjectType> allObj = Schema.getGlobalDescribe();
        Map<String,Schema.SObjectField> allFlds = allObj.get(objName).getDescribe().fields.getMap();
        List<Schema.PicklistEntry> pickList = allFlds.get(fldName).getDescribe().getPickListValues();
        for(Schema.PicklistEntry pk : pickList){
            pkList.add(pk.getValue());
        }
        return pkList;
    }
    @AuraEnabled
    public static Account createAccount(Account ac){
        insert ac;
        return ac;
    }
}

4.  Create a Lightning application which will host the component in the App
<aura:application extends=”force:slds” >
    <c:createAccountRecords/>
</aura:application>


5. Test the Application

Save and Click on Update Preview to view your created Application. Your page will look something like this.
User-added image

Note:- You might need to modify it as per your requirement, this is just a sample code.​​​​​​​​​​​​​​

PLEASE MARK IT AS BEST ANSWER SO THAT IT CAN HELP OTHERS TOO.

Regards,

Priya Ranjan

This was selected as the best answer
Lokesh Rayapati4Lokesh Rayapati4
Thank you so much Priya Ranjan. 

  The answer you gave was really helpful. Can you tell how to make fields empty after submitting