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
Muhammad Jawwad 16Muhammad Jawwad 16 

how to call an apex controller from lightning component button?

public class BatchApexController{
 
      public void callBatch(){
      
        database.executebatch(new LoanofficerBatch());
    database.executebatch(new RealtorBatch());
    database.executebatch(new BuilderBatch());
      }
}
please help
 
Ajay K DubediAjay K Dubedi
Hi Muhammad,

You need to create a lightning component that implements force: lightningQuickAction. After this, you will be able to override your Action using this lightning component. You can program your client-side controller to set a callback function, that would call your Apex class to perform some logical calculation. This is fairly simple and you can find numerous example on the trailhead.

var action = cmp.get("c.YourMethodName");
action.setParams({
     param1: cmp.get("v.something1"),
     param2: cmp.get("v.something2")
});
action.setCallback(this, function(response) {
   if (response.state === "SUCCESS"){
       var serverResponse = response.getReturnValue();
   }    
});
$A.enqueueAction(action);

Check out this link:
https://trailhead.salesforce.com/en/content/learn/modules/lex_dev_lc_basics/lex_dev_lc_basics_server
https://wedgecommerce.com/calling-apex-controller-method-lightning-component/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

 
@@@we@@@@@@we@@@
you  Just create Simple Button   inside your component button  UI  . Create one Apex class  and call inside apex your batch clas   
Sharat C 2Sharat C 2
Hi,
You can use below code first to create a component 

Component 
<aura:component implements="force:lightningQuickAction,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" controller="BatchApexController" >
	<aura:handler name="init" value="{!this}" action="{!c.myAction}"/>	
</aura:component>

JS Controller
({
	myAction : function(component, event, helper) {
		var action = cmp.get("c.callBatch");		
		action.setCallback(this, function(response) {
   		
        if (response.state === "SUCCESS"){
        	var serverResponse = response.getReturnValue();
   		}
            
		});
		$A.enqueueAction(action);	
	}
})

Once this is done, Follow the below step
  1. Go To the Object Definition page
  2. Click on Button, Links and Action
  3. Click on New Action
  4. In Action Type Select Lightning Component
  5. Select the component Which you had just created
  6. Fill other Mandatory fields and Save
  7. Add Quick Action to Page Layout by editing Page Layout