+ Start a Discussion
amateur1amateur1 

test case help

this is the controller please help me how to write test case for this

 

 

global class LeadService {
  
   webservice String area; 
   webservice String region; 
   
   //Define an object in apex that is exposed in apex web service
   global class LeadObject {
      webservice String fname;
      webservice String lname;
      webservice String email;
      webservice String phone;
      webservice String city;
      webservice String state;
      webservice String preference;
      webservice String leadId;
   }

   webservice static LeadObject createLead(LeadObject vLead,Travel_History__c THY,Academic_Profiles__c APS,Work_Experience__c WES) {
       
       Travel_History__c TH = new Travel_History__c();
       Academic_Profiles__c AP = new Academic_Profiles__c();
       Work_Experience__c WE = new Work_Experience__c();
       
       Lead lead = new Lead();
       lead.FirstName = vLead.fname; 
       lead.LastName = vLead.lname;
       lead.Email = vLead.email;
       lead.Phone = vLead.phone;
       lead.City = vLead.city;
       lead.State = vLead.state;
       lead.Description = vLead.preference;
       lead.Company = 'None';
       
       insert lead;
       
       TH.Lead__c = Lead.id;
       TH.Name = 'Country';
       Insert TH;
       
       AP.Lead__c= Lead.id;
       Insert AP;
       
       WE.Lead__c= Lead.id;
       WE.Work_Experience_details__c = 'Self';
       WE.name= 'Company';
       WE.Work_Experience_End_Date_MM_YYYY__c = '02/2012';
       WE.Work_Experience_Start_Date__c = '02/2012';
       Insert WE;
        
       
       vLead.leadId = lead.id;
       
       return vLead;
  }
  
  webservice static LeadObject getLead(string leadId) {
      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];
      
       LeadObject vLead = new LeadObject();
       vLead.fname = l.FirstName; 
       vLead.lname= l.LastName; 
       vLead.email= l.Email; 
       vLead.phone = l.Phone; 
       vLead.city = l.City ;
       vLead.state = l.State; 
       vLead.preference = l.Description;
       vLead.leadId = l.id;

      return vLead;
  }
  
  webservice static LeadObject getLeadByEmailId(string emailId) {
      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE Email = :emailId];
      
       LeadObject vLead = new LeadObject();
       vLead.fname = l.FirstName; 
       vLead.lname= l.LastName; 
       vLead.email= l.Email; 
       vLead.phone = l.Phone; 
       vLead.city = l.City ;
       vLead.state = l.State; 
       vLead.preference = l.Description;
       vLead.leadId = l.id;

      return vLead;
  }  
  webservice static void updateLead(LeadObject vLead) {

       String leadId = vLead.leadId;
       Lead lead = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];
       lead.FirstName = vLead.fname; 
       lead.LastName = vLead.lname;
       lead.Email = vLead.email;
       lead.Phone = vLead.phone;
       lead.City = vLead.city;
       lead.State = vLead.state;
       lead.Description = vLead.preference;
       
       update lead;
      

       
       
  }


}

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference

global class LeadService {

 

   webservice String area;

   webservice String region;

  

   //Define an object in apex that is exposed in apex web service

   global class LeadObject {

      webservice String fname;

      webservice String lname;

      webservice String email;

      webservice String phone;

      webservice String city;

      webservice String state;

      webservice String preference;

      webservice String leadId;

   }

 

   webservice static LeadObject createLead(LeadObject vLead,Travel_History__c THY,Academic_Profiles__c APS,Work_Experience__c WES) {

      

       Travel_History__c TH = new Travel_History__c();

       Academic_Profiles__c AP = new Academic_Profiles__c();

       Work_Experience__c WE = new Work_Experience__c();

      

       Lead lead = new Lead();

       lead.FirstName = vLead.fname;

       lead.LastName = vLead.lname;

       lead.Email = vLead.email;

       lead.Phone = vLead.phone;

       lead.City = vLead.city;

       lead.State = vLead.state;

       lead.Description = vLead.preference;

       lead.Company = 'None';

      

       insert lead;

      

       TH.Lead__c = Lead.id;

       TH.Name = 'Country';

       Insert TH;

      

       AP.Lead__c= Lead.id;

       Insert AP;

      

       WE.Lead__c= Lead.id;

       WE.Work_Experience_details__c = 'Self';

       WE.name= 'Company';

    //   WE.Work_Experience_End_Date_MM_YYYY__c = '02/2012';

    //   WE.Work_Experience_Start_Date__c = '02/2012';

       Insert WE;

       

       

       vLead.leadId = lead.id;

      

       return vLead;

  }

 

  webservice static LeadObject getLead(string leadId) {

      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];

     

       LeadObject vLead = new LeadObject();

       vLead.fname = l.FirstName;

       vLead.lname= l.LastName;

       vLead.email= l.Email;

       vLead.phone = l.Phone;

       vLead.city = l.City ;

       vLead.state = l.State;

       vLead.preference = l.Description;

       vLead.leadId = l.id;

 

      return vLead;

  }

 

  webservice static LeadObject getLeadByEmailId(string emailId) {

      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE Email = :emailId];

     

       LeadObject vLead = new LeadObject();

       vLead.fname = l.FirstName;

       vLead.lname= l.LastName;

       vLead.email= l.Email;

       vLead.phone = l.Phone;

       vLead.city = l.City ;

       vLead.state = l.State;

       vLead.preference = l.Description;

       vLead.leadId = l.id;

 

      return vLead;

  } 

  webservice static void updateLead(LeadObject vLead) {

 

       String leadId = vLead.leadId;

       Lead lead = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];

       lead.FirstName = vLead.fname;

       lead.LastName = vLead.lname;

       lead.Email = vLead.email;

       lead.Phone = vLead.phone;

       lead.City = vLead.city;

       lead.State = vLead.state;

       lead.Description = vLead.preference;

      

       update lead;

     

 

      

      

  }

 

  testMethod static void testWebSrvceAddNumbers()

{

    LeadService w= new LeadService ();

    LeadObject lo=new LeadObject();

    lo.fname='fn';

    lo.lname='ln';

    lo.email='aj@email.com';

    lo.phone='9999999999';

    lo.city='Noida';

    lo.state='up';

   

    Travel_History__c th=new Travel_History__c();

    Academic_Profiles__c ap=new Academic_Profiles__c();

    Work_Experience__c we=new Work_Experience__c();

   

   

    LeadService.createLead(lo,th,ap,we);

   

    Lead ld=new Lead(LAstname='ln',Company='com');

    insert ld;

    LeadService.getLead(ld.id);

   

    LeadService.getLeadByEmailId('aj@email.com');

   

}

 

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference

global class LeadService {

 

   webservice String area;

   webservice String region;

  

   //Define an object in apex that is exposed in apex web service

   global class LeadObject {

      webservice String fname;

      webservice String lname;

      webservice String email;

      webservice String phone;

      webservice String city;

      webservice String state;

      webservice String preference;

      webservice String leadId;

   }

 

   webservice static LeadObject createLead(LeadObject vLead,Travel_History__c THY,Academic_Profiles__c APS,Work_Experience__c WES) {

      

       Travel_History__c TH = new Travel_History__c();

       Academic_Profiles__c AP = new Academic_Profiles__c();

       Work_Experience__c WE = new Work_Experience__c();

      

       Lead lead = new Lead();

       lead.FirstName = vLead.fname;

       lead.LastName = vLead.lname;

       lead.Email = vLead.email;

       lead.Phone = vLead.phone;

       lead.City = vLead.city;

       lead.State = vLead.state;

       lead.Description = vLead.preference;

       lead.Company = 'None';

      

       insert lead;

      

       TH.Lead__c = Lead.id;

       TH.Name = 'Country';

       Insert TH;

      

       AP.Lead__c= Lead.id;

       Insert AP;

      

       WE.Lead__c= Lead.id;

       WE.Work_Experience_details__c = 'Self';

       WE.name= 'Company';

    //   WE.Work_Experience_End_Date_MM_YYYY__c = '02/2012';

    //   WE.Work_Experience_Start_Date__c = '02/2012';

       Insert WE;

       

       

       vLead.leadId = lead.id;

      

       return vLead;

  }

 

  webservice static LeadObject getLead(string leadId) {

      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];

     

       LeadObject vLead = new LeadObject();

       vLead.fname = l.FirstName;

       vLead.lname= l.LastName;

       vLead.email= l.Email;

       vLead.phone = l.Phone;

       vLead.city = l.City ;

       vLead.state = l.State;

       vLead.preference = l.Description;

       vLead.leadId = l.id;

 

      return vLead;

  }

 

  webservice static LeadObject getLeadByEmailId(string emailId) {

      Lead l = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE Email = :emailId];

     

       LeadObject vLead = new LeadObject();

       vLead.fname = l.FirstName;

       vLead.lname= l.LastName;

       vLead.email= l.Email;

       vLead.phone = l.Phone;

       vLead.city = l.City ;

       vLead.state = l.State;

       vLead.preference = l.Description;

       vLead.leadId = l.id;

 

      return vLead;

  } 

  webservice static void updateLead(LeadObject vLead) {

 

       String leadId = vLead.leadId;

       Lead lead = [SELECT id, FirstName, LastName, Email, Phone, City, State, Description FROM Lead WHERE id = :leadId];

       lead.FirstName = vLead.fname;

       lead.LastName = vLead.lname;

       lead.Email = vLead.email;

       lead.Phone = vLead.phone;

       lead.City = vLead.city;

       lead.State = vLead.state;

       lead.Description = vLead.preference;

      

       update lead;

     

 

      

      

  }

 

  testMethod static void testWebSrvceAddNumbers()

{

    LeadService w= new LeadService ();

    LeadObject lo=new LeadObject();

    lo.fname='fn';

    lo.lname='ln';

    lo.email='aj@email.com';

    lo.phone='9999999999';

    lo.city='Noida';

    lo.state='up';

   

    Travel_History__c th=new Travel_History__c();

    Academic_Profiles__c ap=new Academic_Profiles__c();

    Work_Experience__c we=new Work_Experience__c();

   

   

    LeadService.createLead(lo,th,ap,we);

   

    Lead ld=new Lead(LAstname='ln',Company='com');

    insert ld;

    LeadService.getLead(ld.id);

   

    LeadService.getLeadByEmailId('aj@email.com');

   

}

 

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

This was selected as the best answer
amateur1amateur1

hi i am getting this error when i am trying to save u r code

Error: Compile Error: Invalid type: LeadObject at line 8 column 23

amateur1amateur1

sorry navatar i got it sorry i was writting a different test class thats why i got the error sorry got 84 percent code coverage thank u