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
aswin balajiaswin balaji 

How to write Test for this contact info

public void getContactDetails(){
    List<Opportunity> opportunityList;
    this.Id = ApexPages.currentPage().getParameters().get('Id');
    opportunityList= [Select AccountId,ContactId From Opportunity Where Id =: this.Id ];
    List<Contact> contactList = new List<Contact>();
    List<Account> AccountList = new List<Account>();
    List<Contact> ContactAccountList = new List<Contact>();
    for(Opportunity oppObj:opportunityList) {
      contactList = [Select Name, Phone, Zip_code__c,Email From Contact Where Id =:oppObj.ContactId ];
      AccountList = [Select BillingStreet,BillingCity From Account Where Id =:oppObj.AccountId];
      for(Contact contactobj:contactList){
          contName = contactobj.Name;
          contPhone = contactobj.Phone;
          contZipcode = contactobj.Zip_code__c;
          contEmail = contactobj.Email;
        system.debug('name '+contName);
        system.debug('phone '+contPhone);
        system.debug('zip '+contZipcode); 
        system.debug('Email '+contEmail); 
        for(Account accountobj:AccountList){
          acctCity = accountobj.BillingStreet;
          acctStreet = accountobj.BillingCity;
        }
      }
      if(oppObj.ContactId == null){
        ContactAccountList =  [Select Name, Phone, Zip_code__c,Email From Contact Where AccountId =:oppObj.AccountId ];
        for(Contact contactaccountobj:contactaccountList){
          contName = contactaccountobj.Name;
          contPhone = contactaccountobj.Phone;
          contZipcode = contactaccountobj.Zip_code__c;
          contEmail = contactaccountobj.Email;
          system.debug('name '+contName);
          system.debug('phone '+contPhone);
          system.debug('zip '+contZipcode); 
          system.debug('Email '+contEmail); 
          for(Account accountobj:AccountList){
            acctCity = accountobj.BillingStreet;
            acctStreet = accountobj.BillingCity;
          }
        }
      }
    }
}

 
Agustin BAgustin B
Hi, create a test using this as reference:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
You should also create before executing the test an opportunity,contact(related to the opportunity) and account(related to the opportunity) so you can cover most of the code.

If it helps please mark as correct, it may help others.
Bablu Kumar PanditBablu Kumar Pandit
Hii aswin,
please Refer Below Links
http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Hope It helps

Thanks Bablu