Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)

I was trying to create an apex method which will return a string whenever we click on the lightning component button. But it is giving no response. This is my Code :

           COMPONENT: 

            <aura:component controller="CustomMassDownload" implements="force:appHostable,force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" > 

                <div class="slds-p-top_xx-large">

                       <button type="button" onclick="{!c.downloadFile}" >Download</button> 

                </div>

            </aura:component>

            CONTROLLER:

            ({

                downloadFile : function(component, event, helper) {

                    console.log('why?');

                    helper.getString(component,event,helper);

                }

            })

            HELPER:

            ({

getString : function(component,event,helper) {

    console.log('owl');

    var action = component.get("c.ReturnString");

    action.setParams({

        abc: "djflskj"

    });

    console.log('puppy' + action);

    action.setCallback(this,function(response){

        console.log('issuccess');

        var state = response.getState();

        if(state === "SUCCESS"){

            console.log('love');

        }else{

            console.log('hate');

        }

    });

}

APEX :

public class CustomMassDownload{

                @AuraEnabled

                public static String ReturnString(String abc){

                system.debug('aaaaaaaaaa');

                return abc;

                }

            }

Can anyone help me where I was doing wrong?

1 Answer

0 votes
by (26.7k points)
edited by

So basically, you are not calling your apex controller. You have to add $A.enqueueAction in your getString. Then only you can able to call Apex controller.

getString : function(component,event,helper) {

    console.log('owl');

    var action = component.get("c.ReturnString");

    action.setParams({

        abc: "djflskj"

    });

    console.log('puppy' + action);

    action.setCallback(this,function(response){

        console.log('issuccess');

        var state = response.getState();

        if(state === "SUCCESS"){

            console.log('love');

        }else{

            console.log('hate');

        }

    });

    $A.enqueueAction(action);

}

I hope this will help.

Want to become a Salesforce expert? join Salesforce Developer Training now!!

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...