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
MRITYUNJAY PATEL 3MRITYUNJAY PATEL 3 

if i want to insert the new account record using toggle button.

Hi there,
I want to insert the new account record by clicking the toggle. if the user has not an existing account in the dropdown menu. so the new account is not inserted. please solve my problem. 

// .cmp code
<aura:component controller="AuraApex" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="FirstName" type="String"  />
    <aura:attribute name="LastName" type="String"  />
    <aura:attribute name="AccountList" type="List" />
     <aura:attribute name="NewAccount" type="String" />
    <div>
         <lightning:input type="String" label="FirstName" aura:id="firstn" value="{!v.FirstName}" required="true"/>
         <lightning:input type="String" label="LastName" aura:id="lastn" value="{!v.LastName}" required="true" />
         <div>
             <div aura:id="DivIDI" class="remove">
        <lightning:select name="select1" label="List Of Existing Account"  aura:id="AccountId"   required="false" >
            <option value="" label="--None--">
                  </option>
            <aura:iteration items="{!v.AccountList}" var="ac" >
                <option value="{!ac.Id}" label="{!ac.Name}" />
            </aura:iteration>
        </lightning:select>
             </div></div>
        <div>
        <lightning:select name="select2" label="List Of Industry"  aura:id="IndId"   required="false" >
            <option value="" label="--None--">
                  </option>
            <aura:iteration items="{!v.AccountList}" var="in" >
                <option value="{!in.Industry}" label="{!in.Industry}" />
            </aura:iteration>
        </lightning:select>
    </div>
        <br/>
        <br/>
        <div class="slds-form-element">
    <label class="slds-checkbox_toggle slds-grid">
      <span class="slds-form-element__label slds-m-bottom_none">New Account Create</span>
       <ui:inputCheckbox change="{!c.selectChange}"/>
      <span id="toggle-desc" class="slds-checkbox_faux_container" aria-live="assertive">
        <span class="slds-checkbox_faux"></span>
        <span class="slds-checkbox_on">New</span>
        <span class="slds-checkbox_off">Old</span>
      </span>
    </label>
  </div>
<!--PART 2 : create a Div with slds-hide class, control by toggle checkbox-->        
   <div aura:id="DivID" class="slds-hide">
      <lightning:input type="String" label="Account Name" aura:id="AcId" value="{!v.NewAccount}"/>
   </div>
        
        <br/>
        <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{! c.handleClick }" />    
      
    </div>
</aura:component>

//controller.js
({
    doInit : function(component, event, helper) {
        helper.helperMethod(component, event, helper);
    },
    
   
    selectChange : function(component, event, helper) {
        // first get the div element. by using aura:id
      var changeElement = component.find("DivID");
       var changeElement1 = component.find("DivIDI");
       if(changeElement != null)
       // $A.util.removeClass(changeElement, "slds-hide");
        // by using $A.util.toggleClass add-remove slds-hide class
       {
           $A.util.toggleClass(changeElement, "slds-hide");
       }
        else
        {
            $A.util.removeClass(changeElement1, "remove");
        }
      
      },
 
    
    handleClick: function(component, event, helper){
    helper.createContact(component, event, helper);
    helper.createIndustry(component, event, helper);
}
})

//helper.js
({
    helperMethod : function(component, event, helper) {
        var action=component.get("c.getAccount");
        action.setCallback(this,function(response){
            console.log(response.getReturnValue());
            if(response.getReturnValue()!=null){
                component.set("v.AccountList",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    createContact: function(component, event, helper) {
     var acid=component.find('AccountId').get('v.value');
      var newacc=component.find('AcId').get('v.value');  
     var fname=component.find('firstn').get('v.value');
     var lname=component.find('lastn').get('v.value');
     
        var action=component.get("c.AddContact");
         action.setParams({
             idnew:newacc,
            id1:acid,
            fn:fname,
            ln:lname          
        });
        action.setCallback(this,function(response){
           
            if(response.getReturnValue()!=null){
                console.log("inside callback");
                 console.log(JSON.stringify(response.getReturnValue()));
               var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
        "title": "Success!",
        "type" : 'success',
        "mode" : 'dismissible',
        "duration" : 5000,
        "message": "The record has been created successfully."
    });
                toastEvent.fire();
                
            }
        });
        $A.enqueueAction(action);
          component.set("v.FirstName", ""); 
          component.set("v.LastName", "");
        component.set("v.AccountList", "");
        
    },
    
     createIndustry: function(component, event, helper) {
     var acido=component.find('AccountId').get('v.value');
      var indu=component.find('IndId').get('v.value');
         console.log('AccountId',+acido);
         console.log('Industry',+indu);
        var action=component.get("c.Addind");
         action.setParams({
            ida:acido,
             indus:indu,         
        });
        
        action.setCallback(this,function(response){
           
            if(response.getReturnValue()!=null){
                console.log("inside callback");
                 console.log(JSON.stringify(response.getReturnValue()));
               
            }
        });
        $A.enqueueAction(action);
    }
})

//Apex class
public with sharing class AuraApex {
   @AuraEnabled(cacheable = true)
    public static list<account> getAccount(){
        List<account> AccList = new list<account>();
        AccList = [select Id,Name,Industry from Account limit 100];
        system.debug('ac'+Acclist);
        return AccList;
    }
    
    @auraenabled
    public static contact AddContact(id id1,string indus, string fn,string ln,string idnew){
        
        contact c =new contact();
        if(id1 != null)
        {
        c.AccountId = id1;
        c.FirstName = fn;
        c.LastName = ln;

        }
        else 
        {
        Account n=new Account();
        n.Name =idnew;
        insert n;
        c.AccountId = idnew;
        c.FirstName = fn;
        c.LastName = ln;   
        }
        insert c;
        return c;
    }
    
    @auraenabled
    public static Account Addind(id ida,string indus){
        
        Account a=new Account();
        a.id=ida;
        a.Industry=indus;
        insert a;
        return a;
    }
}

Thanks in advance
 
SwethaSwetha (Salesforce Developers) 
HI Mrityunjay,
Where are you stuck? Are you seeing any errors? Thanks
MRITYUNJAY PATEL 3MRITYUNJAY PATEL 3
@swetha
I have no error in this code but not insert the new account