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
Akhil Katkam 5Akhil Katkam 5 

why apex is not calling from controller method in developer console

Hi Developer Community, 
I was not able to find why my apex is not calling from controller method ,
can anyone please tell me the reason for this.

please check my code below 

request1 :  function(component, event, helper) {
            var recId = component.get("v.selectedId");
            alert('hi');
         //   var recordId =component.get("v.recordId");
            var action = component.get("c.fetchCompetitions");
              action.setParams({
            "recordId" : component.get("v.recordId"),
                  "recId" : recId
                             
        });
            $A.enqueueAction(action);
}

APEX code:

  @AuraEnabled
    public static void fetchCompetitions(String recordId, String recId){
                  System.debug('hi');
                   system.debug('recordId' +recordId);
                    system.debug('recId' +recId);
        List<Opportunity> opp=new List<Opportunity>();
        Opportunity o = [select id,Name,MainCompetitors__c from Opportunity where id=:recordId];
        List<Competitor__c> cmptList=[select id,Name  from Competitor__c where id=:recId];
        for(Competitor__c comp : cmptList){
           Opportunity opList = new Opportunity();    
           opList.MainCompetitors__c = comp.Name;        
            opp.add(opList);
        }
       update opp; 
        system.debug('opp' +opp);

    }   
    
Best Answer chosen by Akhil Katkam 5
ShivankurShivankur (Salesforce Developers) 
Hi Akhil,

Please try to use code like below:
request1: function(component, event, helper) {
        var recId = component.get("v.selectedId");
        alert('hi');
        var action = component.get("c.fetchCompetitions");
        action.setParams({
            "recordId": component.get("v.recordId"),
            "recId": recId
        }); // Create a callback that is executed after 
        action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") { // Alert the user with the value returned 
                    // from the server 
                    alert("From server: " + response.getReturnValue());
                    // You would typically fire a event here to trigger 
                    // client-side notification that the server-side 
                    // action is complete 
                } else if (state === "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");
                    }
                }
                $A.enqueueAction(action);
            }
        }

Apex code can be kept as is.Also ensure you are using controller="ApexClassName" in your component definition file.

Refer this documentation for more help:
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi akhil,

Have you add Controller attribute in <aura:Component tag to link the Apex Class like <aura:component controller="CLASSNAME">?

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
ShivankurShivankur (Salesforce Developers) 
Hi Akhil,

Please try to use code like below:
request1: function(component, event, helper) {
        var recId = component.get("v.selectedId");
        alert('hi');
        var action = component.get("c.fetchCompetitions");
        action.setParams({
            "recordId": component.get("v.recordId"),
            "recId": recId
        }); // Create a callback that is executed after 
        action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") { // Alert the user with the value returned 
                    // from the server 
                    alert("From server: " + response.getReturnValue());
                    // You would typically fire a event here to trigger 
                    // client-side notification that the server-side 
                    // action is complete 
                } else if (state === "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");
                    }
                }
                $A.enqueueAction(action);
            }
        }

Apex code can be kept as is.Also ensure you are using controller="ApexClassName" in your component definition file.

Refer this documentation for more help:
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
This was selected as the best answer
mukesh guptamukesh gupta
Hi Akhil,

You need to use below line in your component
<aura:component controller="YOUR APEX CLASS NAME">


if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh