0

I've developed a Visualforce button that callout an external Webservice and fills object fields with the obtained information.

I cant find the way to create button to close the VF page and refresh the Object that was modified and refresh the page to show the changes.

Is this possible? What can I try to achieve this?

EDIT

Visualforce button.

<apex:page standardController="Account" extensions="EquifaxEmpresaController" action="{!consultaEmpresa}">
   <input type="hidden" value="{!account.Equifax__c}"></input>   

<apex:pageBlock >

  <apex:form >
  {!theJSON}
   </apex:form>

  </apex:pageBlock>

</apex:page>

Extension Class

public class EquifaxEmpresaController {

    private final Account acct;
    public EquifaxEmpresa theEmpresa {get;set;}
    public String theJSON {get;set;}
    public String aux {get;set;}

    public EquifaxEmpresaController(ApexPages.StandardController stdController){
        this.acct = (Account)stdController.getRecord();
   }

    public void consultaEmpresa(){

        string msj = '{'+
                     '"NroDocumento":"'+acct.Equifax__c+'",'+
                     '"Consulta":"Empresa"'+
                     '}';

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('some endpoint here');
        req.setBody(msj);
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setTimeout(12000);

        HttpResponse res = new HttpResponse();

        res = Http.send(req);
        if(res.getStatusCode() == 200){
            String body = res.getBody();
            theEmpresa = EquifaxEmpresa.parse(body);
            Account tempAcct = [select id, BC_IE_Razon_Social__c from account where id =: acct.Id];

            tempAcct.BC_IE_Razon_Social__c = theEmpresa.Empresa[0].RazonSocial;
            tempAcct.Name = theEmpresa.Empresa[0].NombreComercial;
            tempAcct.BC_IE_Tipo_de_Contribuyente__c = theEmpresa.Empresa[0].TipoContribuyente;
            tempAcct.BC_IE_Estado_de_Contribuyente__c = theEmpresa.Empresa[0].EstadoContribuyente;
            tempAcct.BC_IE_Condicion_Contribuyente__c = theEmpresa.Empresa[0].CondicionContribuyente;
            tempAcct.BC_IE_Razon_Dependencia__c = theEmpresa.Empresa[0].Dependencia;
            tempAcct.BC_IE_Codigo_CIIU__c = theEmpresa.Empresa[0].CodigoCIIU;
            tempAcct.BC_IE_Descripcion_CIIU__c = theEmpresa.Empresa[0].DescripcionCIIU;
            tempAcct.BC_IE_Inicio_Actividades__c = theEmpresa.Empresa[0].InicioActividades;
            tempAcct.BC_IE_Actividad_Comercio_Exterior__c = theEmpresa.Empresa[0].ActividadComercioExterior;
            tempAcct.BC_IE_Numero_Trabajadores__c = theEmpresa.Empresa[0].NumeroTrabajadores;

            update tempAcct;

            theJSON = 'Datos correctamente insertados, puede cerrar esta ventana.';

        }else{
            theJSON = 'Error :'+res.getBody()+' '+res.getStatusCode();
        }

    }

Thanks

2
  • Yes it can be done. The how would depend on your implementation. Would be very helpful if you could share the relevant code, otherwise we are just guessing
    – Eric
    Feb 18, 2020 at 20:16
  • I'll edit, thanks. Feb 18, 2020 at 20:39

1 Answer 1

1

to return back to the account record simply add this to your method

public PageReference consultaEmpresa(){

    return new PageReference('/' + acct.Id);

}

You can remove theJSON from the page and the controller as it will never be shown

In case you need to setRedirect which you should not

public PageReference consultaEmpresa(){

        PageReference pr = new PageReference('/' + acct.Id);
        pr.setRedirect(true);
        return pr;

    }
7
  • i dont need a button that method to executes the function, because is executing when the page is loaded, what I need is a button that returns to the object and refresh with changes Feb 18, 2020 at 20:58
  • @ÓscarLobos doh. Missed the action parameter. Check updated answer
    – Eric
    Feb 18, 2020 at 20:59
  • this return inside the consultaEmpresa method? o should i create a new method for the PageReference? (consultaEmpresa() is a public void) Feb 18, 2020 at 21:04
  • @ÓscarLobos add the return at the END of the consultaEmpresa method. Make sure to update the return type. When it is done executing it will return you to the account page
    – Eric
    Feb 18, 2020 at 21:05
  • thanks @Eric, this worked, but there is anyway to refresh and show the changes when It went back to the pageReference? Feb 18, 2020 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .