1

I wanted to call my web service class from a custom button on a vf page ,so i can make a API call when I am on the page itself ,and populate that values on the page, Shall i use Apex:actionfunction or javascript Remoting.

    global class SoapServicePP{
   public Date strPeildatum {get;set;}
    public SoapServicePP(PartnerRegController controller) {

    }

    public static boolean check = false;
    public static string performAuthentatication(){
        PP_Credentials__mdt [] objReference = [Select Password__c,Realm__c,Username__c,MasterLabel from PP_Credentials__mdt where MasterLabel='Credentials'];
        String userName;
        String pass;
        String companyName;
        String securityToken;
        for( PP_Credentials__mdt objIterate :objReference){
                userName = objIterate.Username__c;
                pass = objIterate.Password__c;
                companyName = objIterate.Realm__c;
        }
        mijnPensioenperfectNlSoapdocAuthenti.SoapApiAuthenticatePort obj = new mijnPensioenperfectNlSoapdocAuthenti.SoapApiAuthenticatePort();
        securityToken =obj.authenticate(companyName,userName,pass); 
        system.debug('securityToken '+securityToken);
        check = obj.isAuthenticated(securityToken);
        system.debug('check '+check);
        return securityToken;
    }

   // @RemoteAction
    webservice static void searchObject(Id contactId) {
        if(contactId!=null){
            String bsn='';
            String contractName='';
            String peildatum ='' ;
            //contactId = lstContactIds[0];
            Contact con = [SELECT id,BSN__c,Contract__c,Peildatum__c from Contact where Id =:contactId];
            bsn= string.valueof(con.BSN__c);
            contractName=con.Contract__c;
            String sMonth = String.valueof(con.Peildatum__c.month());
            String sDay = String.valueof(con.Peildatum__c.day());
            if(sMonth.length()==1){
              sMonth = '0' + sMonth;
            }
            if(sDay.length()==1){
              sDay = '0' + sDay;
            }
            peildatum = String.valueof(con.Peildatum__c.year()) +'-'+String.valueof(sMonth) +'-'+ String.valueof(sDay) ;
            system.debug('sMonth' + sMonth);
            system.debug('sDay' + sDay);
            system.debug('peildatum'+peildatum);
            system.debug('bsn'+bsn);
            system.debug('contractName'+contractName);
            if(!string.isBlank(peildatum) &&  !string.isBlank(bsn) && !string.isBlank(contractName)) {
                callDeelnemerWaardeOpPeildatum(bsn,contractName,peildatum,contactId);
            }
        }
    }

    @future(Callout = true)
    public static void callDeelnemerWaardeOpPeildatum(String bsnNumber,String contractName, String divorceDate,Id contactId) {
        String token=performAuthentatication();
        //SoapServicePP dummyReference = new SoapServicePP();
        system.debug('check'+check);
        if(check && !String.isBlank(token)){
            mijnPensioenperfectNlCalculat3.SoapApiCalculatorPort objCall =new mijnPensioenperfectNlCalculat3.SoapApiCalculatorPort();
            mijnPensioenperfectNlCalculat3.SoapApiCalculatorWaardeOpPeildatum objectData =objCall.deelnemerWaardeOpPeildatum(bsnNumber,contractName,divorceDate,token);
            system.debug('objectData '+objectData);
            string responeStr = JSON.serialize(objectData);
            system.debug('responeStr'+responeStr);
            JSON2Apex myClass = JSON2Apex.parse(responeStr);
            String strWaardeOP = myClass.waardeOP;
            String strWaardeNP = myClass.waardePP;
            system.debug('strWaardeOP'+strWaardeOP);
            system.debug('strWaardeNP'+strWaardeNP);
            updateScheiden(strWaardeOP,strWaardeNP,contactId);
        }
    } 

    public static void updateScheiden(String valueOP ,String valueNP ,Id contactId){
        List<Scheiden__c> lstScheiden =[Select id,Name,Contact__c,Waarde_NP__c,Waarde_OP__c from Scheiden__c where Contact__c=:contactId];
        List<Scheiden__c>lstToUpdate=new List<Scheiden__c>();
        if(lstScheiden.size()==1){
            Scheiden__c objScheiden = new Scheiden__c(
            Id =lstScheiden[0].Id,
            Contact__c=contactId,
            Waarde_OP__c=Decimal.valueof(valueOP),
            Waarde_NP__c=Decimal.valueof(valueNP)
            );
            lstToUpdate.add(objScheiden);
        }
        else if(lstScheiden.size()==0){
            Scheiden__c objScheiden = new Scheiden__c(
            Contact__c=contactId,
            Waarde_OP__c=Decimal.valueof(valueOP),
            Waarde_NP__c=Decimal.valueof(valueNP)
            );
            lstToUpdate.add(objScheiden);
        }
        if(lstToUpdate.size()>0){
            upsert lstToUpdate;
        }
    }  

}

I want to call searchObject method from my vf page

0

If you want to use the form and hold the view state then use actionFunction. You can refer my blog post for detailed code sample Visualforce webservice callout synchronously & asynchronously

Otherwise, if you need asynchronous processing and don't want to hold the view state then go for javascript remoting.

For more information, refer When to Use JavaScript Remoting

  • I have added the code ..BTW congratulations on your Technical Architect. – Raju Sen May 8 '18 at 9:55
  • btw, I am not a TA. – Santanu Boral May 8 '18 at 9:57
  • ohh sorry ,i saw an update recently from your side..might be system architect then..or maybe it might be something else. – Raju Sen May 8 '18 at 9:59
0

So you are basically asking if you should use Javascript Remoting or Apex Action function.

The decision can be easily made when you consider View State.

  1. Action function, Command Button, CommandLink etc preserve the view state. Which means whatever custom variables you have defined in Apex Controller/Extension are preserved and can be used in any Apex method of controller.

  2. Remoting : Its stateless, as its signature states static . Thus it cannot use the variables defined in Apex controller.

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.