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
Shiv ShankarShiv Shankar 

How to Pass Id from test class ?

Friends i have written a test class whic pulls the data from object for display information on a visulal foce page.

Class Code is give below.......In this class i m fatching current page id (which is needed to represent information)....after that i have written a Test class....My question is that how i get current page id for this....i have tried somethin but it's not working please check this........I have mentioned test class code also............Imeditely needed...thanks

 

//Origenal class

 

public with sharing class GoogleChart_OpptyStage_Subex_new {

    
  public list<Opportunity> OpptyList{get;set;}
  public list<Opportunity> OpptyList1{get;set;}
  
  public Decimal dropped{get;set;}
  public Decimal closedwon {get;set;}
  public Decimal closedlost {get;set;}
  
  public Decimal UQ{get;set;}
  public Decimal NI{get;set;}
  public Decimal OQ{get;set;}
  public Decimal RFI{get;set;}
  public Decimal RFP {get;set;}
  public Decimal Shortlisted{get;set;}
  public Decimal CN{get;set;}
  public Decimal PO {get;set;}
  
  //public List<Opportunity> opptyList {get;set;}
  
  public GoogleChart_OpptyStage_Subex_new(ApexPages.Standardcontroller controller) {
       OpptyList = new list<Opportunity> ();
       OpptyList1 = new list<Opportunity>();
      //OpptyList1=[select id, StageName,Name,CloseDate From Opportunity where StageName ='Closed Won' ];
      Account acc=[select Id,Name from Account where Id=: Apexpages.currentPage().getParameters().get('Id')];
      opptyList = [select id, StageName, Amount From Opportunity where (StageName ='Stage8: Closed Dropped' OR StageName ='Stage8: Closed Lost' OR StageName ='Stage8: Closed Won') AND AccountId =: acc.Id];
      opptyList1 = [select id, StageName, Amount From Opportunity where StageName !='Stage8: Closed Dropped' AND StageName !='Stage8: Closed Lost' AND StageName !='Stage8: Closed Won'  AND AccountId =: acc.Id];
      dropped = 0;
      closedwon = 0;
      closedlost = 0;
      
      UQ = 0;
      NI = 0;
      OQ = 0;
      RFI = 0;
      Shortlisted = 0;
      CN = 0;
      RFP = 0;
      PO = 0;
      
      for(Opportunity opp : opptyList ){
          if(opp.StageName  =='Stage8: Closed Won') {
            closedwon=closedwon+opp.Amount;    
          }
         if(opp.StageName  =='Stage8: Closed Lost') {
          closedlost=closedlost+opp.Amount;
         }
          if(opp.StageName  =='Stage8: Closed Dropped') {
            dropped=dropped+opp.Amount;  
          }
      }
      for(Opportunity opp1 : opptyList1 ){
          if(opp1.StageName  =='Stage 0: Unqualified / Inactive') {
            UQ=UQ+opp1.Amount;    
          }
          if(opp1.StageName  =='Stage 1: Need Identification') {
          NI=NI+opp1.Amount;
          }
          if(opp1.StageName  =='Stage 2: Opportunity Qualification') {
            OQ=OQ+opp1.Amount;  
          }
          
          if(opp1.StageName  =='Stage 3: RFI / Budgetary Proposal') {
            RFI=RFI+opp1.Amount;    
          }
          if(opp1.StageName  =='Stage 4: RFP / Proposal Evaluation') {
          RFP=RFP+opp1.Amount;
          }
          if(opp1.StageName  =='Stage 5: Shortlist') {
            Shortlisted=Shortlisted+opp1.Amount;  
          }
          if(opp1.StageName  =='Stage 6: Selected / Contract Negotiation') {
            CN = CN+opp1.Amount;  
          }
          if(opp1.StageName  =='Stage7: PO/Contract Under Process') {
            PO=PO+opp1.Amount;  
          }
      }
     }
public string test(){
    System.debug('----------------------------->hiii');
    return null;
}
}

 

 

 

--------------------------------------Test class------------------------------------------------

@isTest
private class TestClassForGoogleChart
{
static testMethod void MethodName()
{
Profile pf = [Select Id from Profile where Name = 'System Administrator'];
User u = new User();
u.FirstName = 'Test';
u.LastName = 'User';
u.Email = 'testuser@test123456789.com';
u.CompanyName = 'test.com';
u.Title = 'Test User';
u.Username = 'testuser@test123456789.com';
u.Alias = 'testuser';
u.CommunityNickname = 'Test User';
u.TimeZoneSidKey = 'America/Mexico_City';
u.LocaleSidKey = 'en_US';
u.EmailEncodingKey = 'ISO-8859-1';
u.ProfileId = pf.Id;
//u.Default_Group__c='All';
u.LanguageLocaleKey = 'en_US';
insert u;
system.runAs(u)
{
Account act = new Account();
act.Name='TestAccount';
act.Market_Unit__c='APAC';
act.Region__c='APAC';
act.Country__c='India';
act.Annual_Revenue__c='$10B+';
act.Rating='Tier 1';
act.Type='Customer';
act.Website='www.kvpcorp.com';
insert act;

Opportunity op = new Opportunity();
op.Name='TestOpp';
op.AccountId=act.Id;
op.Product__c='ROC CR';
op.RVP_Sales__c='Norbert Holst';
op.LeadSource='Advertising';
op.Sales_Channel__c='Direct';
op.Industry_Segments__c='Cable';
op.StageName='Stage 0: Unqualified / Inactive';
op.CloseDate=system.today();
op.ForecastCategoryName='Pipeline';
insert op;


ApexPages.StandardController sc = new ApexPages.StandardController(act);
GoogleChart_OpptyStage_Subex_new GCO = new GoogleChart_OpptyStage_Subex_new(sc );
ApexPages.currentPage().getParameters().put('ID',act.id);





}
}
}


Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi,

 

You can refer following code for your test class; this will solve your issue.

 

// Instantiate VisualForce Page
        
PageReference pgRef = Page.Appt_New; //Create Page Reference - 'Appt_New' is the name of Page
Test.setCurrentPage(pgRef); //Set the page for Test Method
ApexPages.currentPage().getParameters().put('id', appointment.id);//Pass Id to page

 

 

All Answers

MandyKoolMandyKool

Hi,

 

You can refer following code for your test class; this will solve your issue.

 

// Instantiate VisualForce Page
        
PageReference pgRef = Page.Appt_New; //Create Page Reference - 'Appt_New' is the name of Page
Test.setCurrentPage(pgRef); //Set the page for Test Method
ApexPages.currentPage().getParameters().put('id', appointment.id);//Pass Id to page

 

 

This was selected as the best answer
Shiv ShankarShiv Shankar

Thanku so much:smileyhappy: