You need to sign in to do that
Don't have an account?
Manuel 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
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
In order to gide you we need to know if you will use rest api or soap?
Kind regards
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
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,
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,