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
SatmetrixSatmetrix 

To Search for contact from a VisualForceapge, But not able to write test method

Hi All,

 

I have a new apex class which can search for contacts from a page based on  the selected chack box.

but I am not able to write test methods.

 

Please help on this.

 

public class MassEmailFromContact{
public String aMessage {        get; set;       }

 
/* Constructor Function. The campaign id is captured in this function */
public MassEmailFromContact(ApexPages.StandardController controller)
{
/*cid=System.currentPageReference().getParameters().get('id');*/
}
/* Variable declarations */
Public String cid;    
           
            public list<cContact> contactList {get; set;}   
                             // Wrapper class which stores contact data and a boolean flag.public
                             public Boolean selectAllCheckbox {get; set;}                      
           // Stores checkbox data.public
           public Boolean errormsg=false;       
          String userinput;                            
               String userinp;                                                                                                                        // Contact EmailString userinp;                                                               // Contact NamePublic boolean displayboxes;
Public List<Contact> results = new List<Contact>();
                                     // Contact search results.
Public List<Contact> selectedContactsstep2 = new List<Contact>();     // Selcted Contacts
 /*End of variable declarations */
/* Getter and setter methods for getting the user input ie. Contact name and email from the UI */
public String getuserinput()
{
return userinput;
}
public void setuserinput(String userinp)
{
this.userinput=userinp;
}
public String getuserinp()
{
return userinp;
}
public void setuserinp(String userinp)
{
this.userinp=userinp;
}
/*End of Getter and Setter methods */
/* Method to Search the contact database to fetch the query results */
public List<Contact> contactsearch()

   errormsg=false; 
   contactList = new List<cContact>();
    for(Contact c : [select Account.Name,Name,FirstName,LastName,Email,Department,NPS__c,title,Id from Contact where Department like :userinput+'%' and Name like :userinp+'%'])
    {     
   contactList.add(new cContact(c)); 
   }
return null;
}
/* End of method */
/* Method for returning the contact search results to the UI */
public List<cContact> getresults()
{
 return contactList;
}
/* End of Method */
/* Wrapper class to contain contact record and a boolean flag */
public class cContact
{
public Contact con {get; set;}
public Boolean selected {get; set;}
 /*This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false*/
public cContact(Contact c)
 {  
  con = c;
    selected = false;
}
}
/* end of Wrapper class */
/* Method to fetch the selected records and send email to them */
public Pagereference processSelected()

List<Contact> selectedContacts = new List<Contact>();
 if (contactList!= null)
 {
    for(cContact cCon : getresults())
    {      
     if(cCon.selected == true)
        {       
             selectedContacts.add(cCon.con);
        }       
    }                    

    System.debug('These are the selected Contacts...');
   
 //   Integer numSelectedContacts = selectedContacts.size();
 //       Integer counter = 0;
   System.Debug(selectedContacts);
           String content = '';
  for(Contact con : selectedContacts)
        {
           content =  + con.ID  +'#' +  con.Email  + '#' + con.FirstName  + '#' + con.LastName  + '|' + content;
      }
  
    /*
    for (Integer i = 0, j = 0; i < 1000;i++)
    {
     strEmailIds = strEmailIds + '|' + i + '|' + 'crnvpwitness@gmail.com'
     strIDs  = strIDs + '|' + i + '|' + '1001';
    }
     */
    /*HttpRequest req = new HttpRequest();       
    Http http = new Http();         
    req.setHeader('content-type', 'UTF-8'); 
    req.setEndpoint('http://localhost:8080/Webservice/fetchVariables.jsp');
    req.setEndpoint('http://www.satmetrix.com/');
    req.setMethod('POST');
    req.setBody('emails='+EncodingUtil.urlEncode(strEmailIds, 'UTF-8'));             
    req.setBody('IDs='+EncodingUtil.urlEncode(strIDs , 'UTF-8')); 
     req.setBody('Firstnames='+EncodingUtil.urlEncode(strFirstNames , 'UTF-8')); 
      req.setBody('LastNames='+EncodingUtil.urlEncode(strLastNames , 'UTF-8')); 
    req.setCompressed(true);
    HttpResponse res = http.send(req);
    System.debug(res.toString()); */
    //Pagereference p=new Pagereference ('http://localhost:8080/Webservice/index.jsp?language=en_US&emailID=CRNVPWITNESS@GMAIL.COM');
    Pagereference p=new Pagereference ('http://localhost:8080/Webservice/fetchVariables.jsp');
    p.getParameters().put('content', EncodingUtil.urlEncode(content, 'UTF-8'));          
   // p.getParameters().put('IDs', EncodingUtil.urlEncode(strIDs , 'UTF-8'));
    //p.getParameters().put('FirstNames', EncodingUtil.urlEncode(strFirstNames , 'UTF-8'));
   // p.getParameters().put('LastNames', EncodingUtil.urlEncode(strLastNames , 'UTF-8'));  
            //Pagereference p=new Pagereference ('http://localhost:8080/Webservice/fetchVariables.jsp');
    return p;         
  }            
    else
        return new Pagereference ('/apex/Campaign');
}
public List<SelectOption> getItems()
{
 List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('YES','YES'));
 options.add(new SelectOption('NO','NO'));
 return options;
}
/* return error message if no contact is selected
*/
public boolean geterrormsg()
{
 return errormsg;
}   
       static testMethod void testAccountTrigger(){
       
        //First, prepare 200 contacts for the test data
        Account acct = new Account(name='test account');
        insert acct;
       
        Contact[] contactsToCreate = new Contact[]{};
        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(AccountId=acct.Id,lastname='testing',firstname='apex');
            contactsToCreate.add(ct);
        }
       
        //Now insert data causing an contact trigger to fire.
        Test.startTest();
        insert contactsToCreate;
        Test.stopTest();   
   
}
     public Pagereference Cancel()
{    
  Pagereference p =new Pagereference('/');  
     return p;
}   

 

   }