• Alan DePew
  • NEWBIE
  • 35 Points
  • Member since 2018
  • Asst Dir Enterprise Application Services
  • Franciscan University of Steubenville

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
I have an apex class that is being called from a Visualforce Page when a user clicks a button on the Contact page. How would I go about writing a test method for this apex class?
 
public class USPS_Contact {

    //Apex properties or variables
    public Id Id { get; set; }
    public Boolean MailingValidated;
    public String MailingStreet, MailingCity, MailingState, MailingZipCode;
    public String USPS_UID, BaseURL, ReturnValue, Endpoint, resXML;
    public String USPSAddress, USPSCity, USPSState, USPSZip5, USPSZip4, USPSDPV, USPSMsg, ErrorMsg;
    
    //Constructor to get the Contact record
    public USPS_Contact(ApexPages.StandardController controller) {
    controller.addFields(new String[]{
      'MailingStreet',
      'MailingCity',
      'MailingState',
      'MailingPostalCode',
      'Mailing_Address_Validated__c'
    });
      con =  (Contact)controller.getRecord();
      Id = con.Id;
        
      MailingStreet = con.MailingStreet;
      MailingCity = con.MailingCity;
      MailingState = con.MailingState;
      MailingZipCode = con.MailingPostalCode;
      MailingValidated = con.Mailing_Address_Validated__c;
            
    }//End Constructor

    //Method that can is called from the Visual Force page action attribute
    public PageReference USPSVerify() {
        IF(MailingStreet != NULL && MailingValidated == FALSE ){
        
    //Build your code logic here
    USPS_UID = 'Eradicated';
    BaseURL = 'http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=';
    ReturnValue = '<AddressValidateRequest USERID="' + USPS_UID + '"><Address ID="0">'; 
    ReturnValue += '<Address1></Address1>';
    ReturnValue += '<Address2>' + MailingStreet + '</Address2>';
    ReturnValue += '<City>' + MailingCity + '</City>';
    ReturnValue += '<State>' + MailingState + '</State>';
    ReturnValue += '<Zip5>' + MailingZipCode + '</Zip5>';
    ReturnValue += '<Zip4></Zip4>';    
    ReturnValue += '</Address></AddressValidateRequest>';
    ReturnValue = EncodingUtil.urlEncode(ReturnValue, 'UTF-8');
    ReturnValue = BaseURL + ReturnValue; 
        
        endpoint = ReturnValue;
        HttpRequest request=new HttpRequest();
        request.setEndPoint(endpoint);
        request.setMethod('GET');
        request.setHeader('Content-Type', 'application/xml');
        Http p=new Http();
        HttpResponse response=p.send(request);
        resXML=response.getBody();

        Dom.Document docx = new Dom.Document();
    	docx.load(resXML);
        Dom.XmlNode xroot = docx.getRootElement();

        List<Dom.XmlNode> childelements = xroot.getChildElements();  //Get XML tags from response
    	String stringchildren = string.join(childelements,',');   //Convert XML tags into string
        Boolean result = stringchildren.contains('Error');      //Check for error tag in XML result      
        
    IF(result == TRUE) {
          ErrorMsg = xroot.getchildElement('Address', null).getchildElement('Error', null).getchildElement('Description',null).getText();
            System.debug(ErrorMsg);
            try {
          // Query for the contact, which has been associated with an account.
            Contact ErrorContact = [SELECT Id
                                      FROM Contact 
                                        WHERE Id = :Id];

            // Update the Validation Fields for Error Message
            ErrorContact.Mailing_Address_Validated__c = FALSE;
            ErrorContact.Mailing_Address_Validation_Message__c = ErrorMsg;
            ErrorContact.Mailing_Address_Validation_Timestamp__c = datetime.now();

            // Update the related account industry
            update ErrorContact;
      } catch(Exception e) {
          System.debug('An unexpected error has occurred: ' + e.getMessage());
      }
        }
        ELSE {
      try {
        USPSAddress = xroot.getChildElement('Address', null).getchildElement('Address2', null).getText();	
	USPSCity = xroot.getChildElement('Address', null).getchildElement('City', null).getText();
        USPSState = xroot.getChildElement('Address', null).getchildElement('State', null).getText();
        USPSZip5 = xroot.getChildElement('Address', null).getchildElement('Zip5', null).getText();                
        USPSZip4 = xroot.getChildElement('Address', null).getchildElement('Zip4', null).getText();                
        USPSMsg = 'Mailing Address Validated';
                
        // Query for the contact, which has been associated with an account.
            Contact SuccessContact = [SELECT Id
                                      FROM Contact 
                                        WHERE Id = :Id];

        // Update the Validation Fields for Success Message
                  SuccessContact.MailingStreet = USPSAddress;
                  SuccessContact.MailingCity = USPSCity;
                  SuccessContact.MailingState = USPSState;
                  SuccessContact.MailingPostalCode = USPSZip5+'-'+USPSZip4;
                  update SuccessContact;
	//We update the record twice to avoid turning off the validation fields when the address is changed.
                  SuccessContact.Mailing_Address_Validated__c = TRUE;
                  SuccessContact.Mailing_Address_Validation_Message__c = USPSMsg;
                  SuccessContact.Mailing_Address_Validation_Timestamp__c = datetime.now();
            update SuccessContact;
                
      } catch(Exception e) {
          System.debug('An unexpected error has occurred: ' + e.getMessage());
      }
        }
    }
        //Return to the Contact Page
        PageReference pageRef = new PageReference('/'+Id);
         pageRef.setRedirect(true);
         return pageRef;
    }
    

}

Here is the Visualforce Page:
<apex:page standardController="Contact" extensions="USPS_Contact" action="{!USPSVerify}">
<apex:form >
<apex:inputHidden value="{!contact.Id}"/>
</apex:form>
</apex:page>

 
Hello, 

I am having trouble getting my test class to work with my after update trigger. When I run my test it is showing 0% code coverage. Can anyone help me to understand why it is not working?

Here is the trigger code:
trigger FUS_UGGR_Application_Submitted on TargetX_SRMb__Application__c (After Update) {
	
    For (TargetX_SRMb__Application__c A : Trigger.New) {
    
    //Update Submitted Application Contact record to "Applicant"
    String ContactId = Trigger.newMap.get(A.ID).TargetX_SRMb__Contact__c;
    String New_Stage = Trigger.newMap.get(A.ID).TargetX_SRMb__Stage__c;
    String Old_Stage = Trigger.oldMap.get(A.ID).TargetX_SRMb__Stage__c;
    String App_Type = Trigger.newMap.get(A.ID).TargetX_SRMb__Application_Type__c;
        
    Contact C = [select id from Contact where id = :contactid];
    
 	IF(New_Stage <> Old_Stage && New_Stage == 'Submitted' && App_Type <> 'Undergraduate Non-Degree Post Secondary'){
    C.UGGR_Student_Stage__c = 'Applicant';
               	
    Update C;
            
        }        
    }
}

Here is my test class:
@isTest
public class FUS_Test_UGGR_App_Submit {

   //Perform Tests
    static testMethod void ValidateUGGRAppSubmit() 
    {

    Contact c = new Contact(
        FirstName = 'Test', 
        LastName = 'Test'
    );
    Insert c;
   
    TargetX_SRMb__Application__c a = new TargetX_SRMb__Application__c(
        TargetX_SRMb__Contact__c = c.Id, 
        TargetX_SRMb__Application_Type__c = 'Undergraduate',
        TargetX_SRMb__Stage__c = 'In Progress'
    );
    
    Insert a;
    
    Test.StartTest();
    	a.TargetX_SRMb__Stage__c = 'Submitted';
    	Update a;
    Test.stopTest();
    }
}

 
I'm trying to show the Year, Month, Days in word format between two date fields but I can't seem to get it to work right. Below is the formula that I have used. If I have a Lease Start Date as 01.01.2019 and the Lease End Date as 31.12.2019 the formula field shows as 0 Years, 12 Months, 4 Days. Anyone any idea on how I can correct this?

IF(Lease_End_Date__c >= Lease_Start_Date__c, 
TEXT(FLOOR((Lease_End_Date__c - Lease_Start_Date__c)/365)) & " Year(s) " & 
TEXT(FLOOR(MOD((Lease_End_Date__c - Lease_Start_Date__c),365)/30)) & " Month(s) " & 
TEXT(MOD(MOD((Lease_End_Date__c - Lease_Start_Date__c),365),30)) & " Day(s) ", NULL
)