0

First off...I am new to SF development and only dabble in javascript. My first "project" was creating an APEX class which calls an external web service (which i created and hosted on AWS). This class returns a single string value:

public class myUtils {

   @AuraEnabled
   public static string retValue(){
     String value;
     String payLoad = 'eyJhbGciOiJIUzI1NiJ9.eyJzZXJ2ZXIiOiJodHRwczovL2RlbW8udHNlcnZlcnRydXN0LnRhYmxlYXUuY29tL3RydXN0ZWQvIiwidX';
     HttpRequest req = new HttpRequest();
     req.setEndpoint('https://myServerOnAWS/endpoint/doSomething/');
     req.setMethod('POST');
     req.setHeader('content-type', 'application/json');

     req.setBody(payLoad);

     Http http = new Http();
     HTTPResponse res = http.send(req);

     String htmlRsp = res.getBody().replace('\'','\\\'');

     string regex = '"(.*)"';
     Pattern p = Pattern.compile(regex);
     Matcher m = p.matcher(htmlRsp);

     if(m.find()){
       value = m.group(1);
     }

        return(value);

    }
}

Successfully tested this using a simple VF page:

<apex:page controller="myUtils">
    <apex:outputText value="{!retValue}" />
</apex:page>

Now, I am struggling to understand the best way to call this apex class in lightning which would return this string value. I created an Aura component:

<aura:component implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" access="global" >
    <lightning:input aura:id="url" label="url" name="url" placeholder="url" type="url"/><br/>
    <lightning:button label="Send Request" onclick="{!c.thirdpartyClientCall}"/>
</aura:component>

And controller:

({ 

    thirdpartyClientCaller : function(cmp, event, helper) { 
        var action = cmp.get("c.retValue"); 

        action.setCallback(this, function(response) { 
            var state = response.getState(); 
            if (state === "SUCCESS") { 
                console.log("SUCCESS");
            } 
            else { 
                console.log("FAILED");
            } 
        }); 

        $A.response(action); 
        } 
    })

When i inspect the page, i see nothing in the console. Any thoughts on what i'm missing?

  • 3
    thirdpartyClientCall and thirdpartyClientCaller. Check the spelling in aura component – Rohit Mourya Jan 8 at 17:15
  • Second @RohitMourya suggestion – Bryan Anderson Jan 8 at 17:17
  • @BryanAnderson Not sure, can you suggest it? – Rohit Mourya Jan 8 at 17:18
4

i have found 4 issues.

  1. your lightning component doesn't have Apex controller you have to add controller like below

    aura:component controller="myUtils" implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" access="global" >
    
  2. check spelling: thirdpartyClientCall and thirdpartyClientCaller

  3. check user profile Access for myUtils apex class.

  4. and replace $A.response(action); in your js controller with $A.enqueueAction(action);
| improve this answer | |
  • good catch. I missed the controller part. +1 – Rohit Mourya Jan 8 at 17:28
  • Wow...thank you for the quick feedback! I have fixed/validated the three issues you identified and redeployed. Currently, when i click the "Send Request" button, nothing happens. When i inspect the page, there's simply nothing being returned. I would expect to see something in the console b/c of the controller code. Thoughts? – rdugge Jan 8 at 18:15
  • updated with 4th issue please consider and let me know – sdandamud1 Jan 8 at 18:19
  • Yes! Now i'm getting "Success". Last question, how do I return the actual value returned from the apex class? – rdugge Jan 8 at 18:40
  • response.getReturnValue() gives Apex method return value, i would recommend go through developer.salesforce.com/docs/atlas.en-us.lightning.meta/…. if your issue resolve please upvote my solution for help to others – sdandamud1 Jan 8 at 18:48
3

The function call in the aura component doesn't matches with the controller function name. thirdpartyClientCall and thirdpartyClientCaller

And rightly added by @sdandamud1, the controller is not mentioned in Aura Component.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.