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
Jim WilliamsJim Williams 

Calling Web Service from VF page automatically upon load

My requirement is: I have a complex VF page that contains contact information with phone numbers (it is part of a custom automated outbound calling app).  There is a lot of logic that fires off via the "action" attribute on the "page" tag.  The action calls a method called  fetchMemberToProcess()  in the custom Apex controller.

I have been asked to incorporate calls to an external web service to determine the federal Do Not Call status of each phone number on the VF page.  So, as soon as a page is displayed, it will display said status next to each phone numebr.

I have the web services call/response working fine when I run my code through Execute Anonymous Apex window in the Developer console.

However, when I try to work the code into the fetchMemberToProcess()  method, I run into one of two errors depending on when I call the code:

1. If I call it after any DML in the fetchMemberToProcess() method, I of course get the known "Error:
You have uncommitted work pending. Please commit or rollback before calling out."

2. however, if I put the code before any DML in the fetchMemberToProcess() method,  I get an error saying that certain code references are now NULL and the page will not load.  For example "The value 'null' is not valid for operator '>'
Error is in expression '{!openActivity.size > 0}' in component <apex:pageBlockTable> in page agent"  - this refers to a list called openActivity in the fetchMemberToProcess() method.  If I remove the code that invokes the web service , the NULL problem disappears - so it is not data-related.   So it appears that control is never going back to the calling method after the web service is invoked.

I did research that there is a way to make separate JavaScript functions and use AJAX methodology to do a callout and refresh certain page elements; however, we do not want to require manual intervention (i.e. a pushbutton) to invoke the service.

Is there any way possible to make this work? 

Here is the first line of the VF page:  <apex:page controller="AgentController" docType="html-5.0" standardStylesheets="false" action="{!fetchMemberToProcess}" tabStyle="Call_Center_Agents__tab">

Here is the method I wrote in the controller that makes the callout.  gcc.CertifyPhoneNumbers() is the call I am trying to make to the custom class I had created that contains the routings to make the call.  ContactInfo.MobilePhone and ContactInfo.HomePhone are variables that already exist as per the page controller class via the fetchMemberToProcess() method .  MobilePhoneStatusDNC and HomePhoneStatusDNC are class variables in the page controller class.

public void GryphonCertifyPhones(){
                    // Jim Williams BEGIN GRYPHON
                    // This method must be called AFTER the initial method becuase a web service callout cannot be in same transaction as DML
                    GryphonCertifyContact gcc = New GryphonCertifyContact();
                    gcc.AddPhone(ContactInfo.MobilePhone, 'Mobile');
                    gcc.AddPhone(ContactInfo.HomePhone, 'Home');
                    gcc.CertifyPhoneNumbers();
                    MobilePhoneStatusDNC = gcc.GetPhoneCertStatus('Mobile');
                    HomePhoneStatusDNC = gcc.GetPhoneCertStatus('Home');
                    // Jim Williams END GRYPHON 
                    
}