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
Margaret FleischakerMargaret Fleischaker 

Use returned value from apex controller in component init function

I have a lightning component that includes an apex class to return a string. I would like the string that is returned to set a value in an event listener in the component's doInit function.

I can tell that communityURL is eventually getting set with the right string, so my connection to the apex class is working. However, I believe because I'm using $A.enqueueAction(action), it's happening AFTER I've already tried to set vfOrigin. So my vfOrigin is null, instead of the returned string. But if I don't use $A.enqueueAction(action), it doesn't appear to call the apex class at all.

Any suggestions for what I'm missing?
 
({
    doInit : function(component, event, helper) {
        // create a one-time use instance of the getCommunityURL action
        // in the server-side controller
        var action = component.get("c.getCommunityURL");
        console.log("[mf] action: " + action);

        var communityURL = '';
        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());
                communityURL = response.getReturnValue();
                
            } else {
                console.log(state);
            }
        });
        $A.enqueueAction(action);

		// this section handles the reCaptcha that is in the embedded VF page
		// when the reCaptcha passes, make the submit button clickable
        // TODO: update with correct origin for production     
        //let vfOrigin = "https://mflei-eventbritecommunity.cs61.force.com";
        alert('[MF] communityURL: ' + communityURL);
        let vfOrigin = communityURL;
        window.addEventListener("message", function(event) {
            console.log(event.data);
            if (event.origin !== vfOrigin) {
                // Not the expected origin: Reject the message!
                alert('vfOrigin: ' + vfOrigin);
                alert('Not the expected origin: ' + event.origin);
                return;
            } 
            if (event.data==="Unlock"){ 
              let myButton = component.find("myButton");
              myButton.set('v.disabled', false);
            }            
        }, false);
    },

 
Best Answer chosen by Margaret Fleischaker
Sampath SuranjiSampath Suranji
Hi Margaret,
Try something like below.
({
    doInit : function(component, event, helper) {
        // create a one-time use instance of the getCommunityURL action
        // in the server-side controller
        var action = component.get("c.getCommunityURL");
        console.log("[mf] action: " + action);
        
        var communityURL = '';
        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());
                communityURL = response.getReturnValue();
                alert('[MF] communityURL: ' + communityURL);
                let vfOrigin = communityURL;
                window.addEventListener("message", function(event) {
                    console.log(event.data);
                    if (event.origin !== vfOrigin) {
                        // Not the expected origin: Reject the message!
                        alert('vfOrigin: ' + vfOrigin);
                        alert('Not the expected origin: ' + event.origin);
                        return;
                    } 
                    if (event.data==="Unlock"){ 
                        let myButton = component.find("myButton");
                        myButton.set('v.disabled', false);
                    }            
                }, false);
                
            } else {
                console.log(state);
            }
        });
        $A.enqueueAction(action);
        
        
    },
})

regards
Sampath(Sampathjt@gmail.com)

 

All Answers

Sampath SuranjiSampath Suranji
Hi Margaret,
Try something like below.
({
    doInit : function(component, event, helper) {
        // create a one-time use instance of the getCommunityURL action
        // in the server-side controller
        var action = component.get("c.getCommunityURL");
        console.log("[mf] action: " + action);
        
        var communityURL = '';
        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());
                communityURL = response.getReturnValue();
                alert('[MF] communityURL: ' + communityURL);
                let vfOrigin = communityURL;
                window.addEventListener("message", function(event) {
                    console.log(event.data);
                    if (event.origin !== vfOrigin) {
                        // Not the expected origin: Reject the message!
                        alert('vfOrigin: ' + vfOrigin);
                        alert('Not the expected origin: ' + event.origin);
                        return;
                    } 
                    if (event.data==="Unlock"){ 
                        let myButton = component.find("myButton");
                        myButton.set('v.disabled', false);
                    }            
                }, false);
                
            } else {
                console.log(state);
            }
        });
        $A.enqueueAction(action);
        
        
    },
})

regards
Sampath(Sampathjt@gmail.com)

 
This was selected as the best answer
Margaret FleischakerMargaret Fleischaker
That worked! Thank you!