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
sravya sadhusravya sadhu 

How to use webservices in salesforce

whenever user enters data in email and move cursor to next field it has to call a webservice and validate email.If it is valid we have a field called email validity which is checkbox the result should display in this check like if it is valid Check if not uncheck. same thing for phone.But somehow iam able to call email but not phone.Could anyone help me plz.here is my code.

public with sharing class myController {

    public PageReference Cancel() {
        return null;
    }

    Account account;

    public Account getAccount() {
        if(account == null) account = new Account();
        return account;
    }
    
    public PageReference save() {
     
        // Add the account to the database. 
        insert account;
        // Send the user to the detail page for the new account.
        PageReference acctPage = new ApexPages.StandardController(account).view();
        acctPage.setRedirect(true);
        return acctPage;
        
       
    }
    public PageReference validEmailAddress() {
               
        if(account.Email__c != '') {
        account.Email_Validity__c= true;
        string baseUrl1 = 'http://fusionq-2022595240.us-west-2.elb.amazonaws.com/EmailValidator/webresources/verify?email='+account.Email__c;
      
         // Prepare request
        Http http = new Http();
        
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint(baseUrl1);
       
        //req.setMethod(method);
        req.setTimeout(60000);
        //req.setBody(postParams);
        
        try {
            HttpResponse res = http.send(req);
            // Analyze and log response.
            String response = res.getBody();
            System.debug('Method callApi response: ' + response);
            System.debug(res.toString());
            System.debug('STATUS:'+res.getStatus());
            System.debug('STATUS_CODE:'+res.getStatusCode());
            
            
            JSON2Apex obj = (JSON2Apex) System.JSON.deserialize(response, JSON2Apex.class);
            System.debug('obj::::::'+obj);
            System.debug('obj.verificationResults.entry[1].value::::::'+obj.verificationResults.entry[1].value);
            if (obj.verificationResults.entry[1].value == 'FAIL'){
                    account.Email_Validity__c= true;
            }
            for(integer i=0; i<obj.verificationResults.entry.size(); i++) {
                System.debug('obj.verificationResults.entry['+i+'].value::::::'+obj.verificationResults.entry[i].value);
            
                if(obj.verificationResults.entry[i].value =='FAIL') {
                     account.Email_Validity__c= false;
                     break;
                
                } 
             } 
        } catch(System.CalloutException e) {
            System.debug('CalloutException:'+e);
        
        }
        
        }
       return null;
       
      } 

    public PageReference validPhone() {
               
        if(account.Phone__c != '') {
        account.Phone_Validity__c= true;
       String baseUrl2 = 'http://fusionq-2022595240.us-west-2.elb.amazonaws.com/phonevalidator/webresources/service/phonenumber='+account.Phone__c;
         // Prepare request
        Http http = new Http();
        
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint(baseUrl2);
       
        //req.setMethod(method);
        req.setTimeout(60000);
        //req.setBody(postParams);
        
        try {
            HttpResponse res = http.send(req);
            // Analyze and log response.
            String response = res.getBody();
            System.debug('Method callApi response: ' + response);
            System.debug(res.toString());
            System.debug('STATUS:'+res.getStatus());
            System.debug('STATUS_CODE:'+res.getStatusCode());
            
            
            JSON2Apex obj = (JSON2Apex) System.JSON.deserialize(response, JSON2Apex.class);
            System.debug('obj::::::'+obj);
            System.debug('obj.verificationResults.entry[1].value::::::'+obj.verificationResults.entry[1].value);
            if (obj.verificationResults.entry[1].value == 'FAIL'){
                    account.Phone_Validity__c= true;
            }
            for(integer i=0; i<obj.verificationResults.entry.size(); i++) {
                System.debug('obj.verificationResults.entry['+i+'].value::::::'+obj.verificationResults.entry[i].value);
            
                if(obj.verificationResults.entry[i].value =='FAIL') {
                     account.Phone_Validity__c= false;
                     break;
                
                } 
             } 
        } catch(System.CalloutException e) {
            System.debug('CalloutException:'+e);
        
        }
        
        }
       return null;
       
      } 

    
    public class Entry {
        public String key;
        public String value;
    }
    public class JSON2Apex {

        public String contact;
        public String domain;
        public String emailAddress;
        public String errorText;
        public String mailHost;
        public String user;
        public VerificationResults verificationResults;


    }

    public class VerificationResults {
        public List < Entry > entry;
    }