3. Call 'getMorosidadOfAccount()' Javascript function using Javascript in the page. It could be as  simple as a button onclick handler or a more sophisticated Javascript framework you use in the page.   A final note: RemoteActions are a good alternative when you are familiar to Javascript,  want to have full control of the page and you want to  use a specific Javascript library. Salesforce platform offers a lot of alternatives to bring external data to the Salesforce org or to the page from the external services.   If you want to learn more: For front end I recommend taking a look to LWC (Lighting Web Components): https://trailhead.salesforce.com/en/content/learn/trails/build-lightning-web-components     For integrations I recommend this module related to External Objects, it may be useful for your use case: https://trailhead.salesforce.com/en/content/learn/projects/quickstart-lightning-connect   Suerte! Angel", "upvoteCount": 2, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A9MJvSAN", "datePublished": "2021-06-26T03:13:34.000Z", "author": { "@type": "Person", "name": "Angel Abrego", "url": "https://trailblazers.salesforce.com/profileView?u=0053000000Bew9GAAR", "affiliation": { "@type": "Organization", "name": "salesforce.com" } } } ] } } Skip to main content

I have an API REST, I have access to response of API but I don't know how to PASS DATA 

to my VisualForce page FROM class Apex controller AND class Apex conection API REST

 

My Structure is:

1.- Class Apex of conection API REST.

2.- Class Apex Controller of VisualForce page.

3.- VisualForce page.

 

My API conection in APEX is:

@RemoteAction

public static Map<String, Object> morosidadInfo(String accountId){

Map<String, Object> datos = new Map<String, Object>();

datos.put('msj', '');

String ctaFact;

List<CuentaFactura__c> idBRM = [select Id, IdCuentaBRM__c from CuentaFactura__c where id = :accountId];

for(CuentaFactura__c cf : idBRM){

ctaFact = cf.IdCuentaBRM__c;

}

Http http = new Http();

HttpRequest request = new HttpRequest();

request.setEndpoint(EndPoint__c.getValues('CampoMorosidad').URL__c);

request.setMethod('POST');

request.setHeader('Content-Type', 'application/json;charset=UTF-8');

request.setHeader('Accept', 'application/json');

// SET THE BODY AS A JSON OBJECT.

String datosCuenta = '{"ACCOUNT_NO" : "' + ctaFact + '"}';

request.setBody(datosCuenta);

System.debug('datosCuenta: ' + datosCuenta);

HttpResponse response;

if (Test.isRunningTest()){

Test.setMock(HttpCalloutMock.class, new DASH_DashboardRelojCobranzaAct_Mock());

response = http.send(request);

} else{

system.debug('REQUEST JSON: ' + request.getBody());

response = http.send(request);

}

// PARSE THE JSON RESPONSE.

System.debug('response: '+ response);

if (response.getStatusCode() == 200){

datos.put('msj', 'exito');

datos = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());

System.debug('Estatus 200 Datos:::: ' + datos);

}else{

datos.put('msj', 'error');

datos.put('response', response);

}

System.debug('Return Datos:::: ' + datos);

return datos;

}

 

What is the Structure that i have to do in my code of my class Apex Controller and VisualForce page ?

#Visualforce Pages #Collaboration #Ask An Expert ##AskAPardotExpert #REST API #Lightning Web Components

1 risposta
  1. 26 giu 2021, 03:13

    Thanks for posting this question. I've seen you have made some good progress!

     

    Annotating a public static method with @RemoteAction` allows you to use it in the Visualforce page as a Javascript function.  What you can do next is the following:

     

    1. In you Visualforce page set the "controller" OR the "extensions" attribute to the name of the APEX class that contains your "@RemoteAction" method. You mentioned that you have a controller class already and a different class to make the APEX Rest request in so you could define the <apex:page> attributes of your Visualforce page as follows:

     

    <apex:page controller="MorosidadController" extension="MorosidadExtension">

    You can use whatever names your prefer, but in the example above MorosidadController.cls is the name of the controller and "MorosidadExtension" is the name of the class that contains the the "@RemoteAction" annotated method.

     

    2. In your Visualforce create a wrapper Javascript function that calls your remote APEX class method.

    You can see the details here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_RemoteAction.htm But It will look something like this snippet below:

    <script type="text/javascript">    function getMorosidadOfAccount() {        //Somewhere from the page you'll get the account Id        var accountId = document.getElementById('accountId').value;         // This will call the APEX controller method asynchronously!        Visualforce.remoting.Manager.invokeAction(            '{!$RemoteAction.MorosidadController.morosidadInfo}',            accountId,             handleResult        );    }     // This is the callback method that will be called once     //the response is received from the server     function handleResult(result, event) { ... }</script>

     

    3. Call 'getMorosidadOfAccount()' Javascript function using Javascript in the page. It could be as  simple as a button onclick handler or a more sophisticated Javascript framework you use in the page.

     

    A final note:

    RemoteActions are a good alternative when you are familiar to Javascript,  want to have full control of the page and you want to  use a specific Javascript library. Salesforce platform offers a lot of alternatives to bring external data to the Salesforce org or to the page from the external services.

     

    If you want to learn more:

    For front end I recommend taking a look to LWC (Lighting Web Components): https://trailhead.salesforce.com/en/content/learn/trails/build-lightning-web-components 

     

    For integrations I recommend this module related to External Objects, it may be useful for your use case:

    https://trailhead.salesforce.com/en/content/learn/projects/quickstart-lightning-connect

     

    Suerte!

    Angel

0/9000