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
Manuel DangondManuel Dangond 

How to call a .net web service from a custom button in Lightning?

Hello,

I'm new to Apex and need help writing a class or method that will call a .net web service when a user clicks on a custom button on the Loan record. Loan is a custom object as well. The class will need to query the Loan object and retrieve some fields from it as well as some fields from the related Account, and then call the .net web service and providing these fields to the service. We are using Lightning.

Thank you much!
Manuel Dangond
Tiago Armando CoelhoTiago Armando Coelho
Hi Manuel, 

In order to gide you we need to know if you will use rest api or soap?

Kind regards
Manuel DangondManuel Dangond
Hello Tiago, We will use rest api. Thanks so much in advance! Kind Regards, Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn
Tiago Armando CoelhoTiago Armando Coelho
Please find an example 

Apex controller -> 
public class AP_CallWebservice {

    // This will get My Object Information and will be colled by the CallWebserviceController.js
    @AuraEnabled
    public static Id getOpp(String oppId){         
       
        return [Select Id, Name, StageName from Opportunity where Id = : oppId];
    } 
    
    @AuraEnabled
    public static String callExternalWebservice(Opportunity opp){
        
        //Lets serialize the object
        String jsonData = JSON.serialize(opp);
        
        HttpRequest req = new HttpRequest();
        //set the callout endPoint 
        //I recomend you to use named Credentials \\ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm
        req.setEndpoint('callout:Mocky/5ab283d32e00003d044cc1d4');
        // set the method
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonData);
        
        Http http = new Http();
        HTTPResponse res = http.send(req);     
        return res.getBody(); 
    }
}


Lightninging Component Controller

public class AP_CallWebservice {

    // This will get My Object Information and will be colled by the CallWebserviceController.js
    @AuraEnabled
    public static Id getOpp(String oppId){         
       
        return [Select Id, Name, StageName from Opportunity where Id = : oppId];
    } 
    
    @AuraEnabled
    public static String callExternalWebservice(Opportunity opp){
        
        //Lets serialize the object
        String jsonData = JSON.serialize(opp);
        
        HttpRequest req = new HttpRequest();
        //set the callout endPoint 
        //I recomend you to use named Credentials \\ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm
        req.setEndpoint('callout:Mocky/5ab283d32e00003d044cc1d4');
        // set the method
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonData);
        
        Http http = new Http();
        HTTPResponse res = http.send(req);     
        return res.getBody(); 
    }
}

Component 

<aura:component implements="force:lightningQuickAction,force:hasRecordId" controller='AP_CallWebservice'>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:button variant="brand" label="Submit" onclick="{! c.handleClick }" />
</aura:component>

You need to create a quick action on the Opportunity Object and a new Name Credential
 
Manuel DangondManuel Dangond
Thank you so much Tiago!! Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn
Tiago Armando CoelhoTiago Armando Coelho
Hi Manuel, 

I have made some modifications to the example because it has some errors this one is working and it's more simple to understand.

Please a simplest one :

JS Controller:
({
    doInit : function(component, event, helper) {
    },
    
    handleClick: function(component, event, helper){ 
        var opportunityId = component.get("v.recordId");
        //let call our webservice
        var webCall = component.get("c.callExternalWebservice");
        // set paraments to the controller AP_CallWebservice.callExternalWebservice
        webCall.setParams({
            oppId : opportunityId
         });
                
        webCall.setCallback(this, function(response) {
            var res = response.getState();
            if(res === 'SUCCESS'){
                alert('Webservice executed with sucess' + res);
            }else {
                alert('Webservice executed with error');    
            }
        });
        $A.enqueueAction(webCall);
    }
})

AP_CallWebservice Apex Controller 
public class AP_CallWebservice{
    
    @AuraEnabled
    public static String callExternalWebservice(Id oppId){
        
        Opportunity opp = [Select Id, Name, StageName from Opportunity where Id = : oppId];
        //Lets serialize the object
        String jsonData = JSON.serialize(opp);
        
        HttpRequest req = new HttpRequest();
        //set the callout endPoint 
        //I recomend you to use named Credentials \\ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm
        req.setEndpoint('callout:Mocky/5ab283d32e00003d044cc1d4');
        // set the method
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonData);
        
        Http http = new Http();
        HTTPResponse res = http.send(req);     
        return res.getBody(); 
    }
}

Kind regards, 
Manuel DangondManuel Dangond
Awesome Tiago! Thanks a million!! Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn
Manuel DangondManuel Dangond
Hello Tiago, I have a couple of questions: 1. Will I need test code in order to deploy the class from our sandbox to production? If so, could you help me with this as well? 2. Since we’re using Lightning, I’m not sure that I can use Java Script. Does this mean I need to create a quick action and have the JS code run when the user clicks on the quick action button? Thanks again Tiago! You are THE BEST! Kind Regards, Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn From: Manuel Dangond Sent: Wednesday, March 21, 2018 10:43 AM To: 'reply' Subject: RE: (Salesforce Developers): New reply to your question. Awesome Tiago! Thanks a million!! Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn
Tiago Armando CoelhoTiago Armando Coelho
Hello Manuel, 
1- I will try to help you.
2- No you can have the code in the doInit method. This javascript method is executed when you click on the quick action button and can close the quick action when its over.

Kind regards, 
Manuel DangondManuel Dangond
Hello Tiago, Got it. Thanks so much for all your help!!!! Manuel Dangond Business Systems Analyst III Opus Bank 19900 MacArthur Blvd., Irvine, CA 92612 P. 949.251.8173 opusbank.com | Twitter | LinkedIn