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
bathybathy 

Apex class with SOQL query to return a list of opportunties based on logged in User Id

Hi Gurus,

I developed an APex class to return a list of opportunities in a particular stage and used this list to display in a visualforce page. 
My code:
public with sharing class mycurrentplanningopps {

   

   public list <Opportunity> Opp {get; set;}
   public String UserId {get; set;}
   
    public list <Opportunity> getmyincompleteopps()
     {
       
       UserId = UserInfo.getUserId();
     
     
     if(UserId == '00590000001CxIm')
     {
          Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where StageName =:'Signed Financial Plan and Insurance'];
               
      
      }
      
      
      else
      { 
         Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where Ownerid =:UserId and StageName =:'Signed Financial Plan and Insurance'];
       }        
        return Opp;
    

    }



 


}

It is working fine for other users which means else block is working. But it is not showing the list for the user I am checking here. I am using the visualforce page in a visualforce page area and placed it on home page layout for the users


Can any one please help me in this?

Thanks in advance.
 
Best Answer chosen by bathy
karthikeyan perumalkarthikeyan perumal
Hello 

Use Below Code, 
 
public with sharing class mycurrentplanningopps {

   

   public list <Opportunity> Opp {get; set;}
    public User activeUser {get;set;}
   
    public list <Opportunity> getmyincompleteopps()
     {
       
      

		User activeUser  = [ SELECT id FROM User WHERE Id=:UserInfo.getUserId()];
     
     if(activeUser.id  == '00590000001CxIm') 
     {
          Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where StageName =:'Signed Financial Plan and Insurance'];
               
      
      }
      
      
      else
      { 
         Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where Ownerid =:activeUser.id  and StageName =:'Signed Financial Plan and Insurance'];
       }        
        return Opp;
    

    }
}

Hope this will help you. 

Thanks
karthik
 

All Answers

Beer NutthawanBeer Nutthawan
Hey bathy

I think your Id below should be 18 characters not 15 characters. 
if(UserId == '00590000001CxIm')
Can you add debug log and check?

Cheers!! 

Nutthawan P.
 
karthikeyan perumalkarthikeyan perumal
Hello 

Use Below Code, 
 
public with sharing class mycurrentplanningopps {

   

   public list <Opportunity> Opp {get; set;}
    public User activeUser {get;set;}
   
    public list <Opportunity> getmyincompleteopps()
     {
       
      

		User activeUser  = [ SELECT id FROM User WHERE Id=:UserInfo.getUserId()];
     
     if(activeUser.id  == '00590000001CxIm') 
     {
          Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where StageName =:'Signed Financial Plan and Insurance'];
               
      
      }
      
      
      else
      { 
         Opp = [select name,contact_s_Fullname__c,CreatedDate,CloseDate,
                   First_3_stages_of_Audit_pack_up_complete__c, 
                   Cancel_any_existing_insurance_cover__c,
                   Complete_any_remaining_rollovers__c,
                   Confirm_if_revenue_has_been_received__c,
                   Ongoing_advice_to_be_implemented__c,
                   Email_the_clients_and_CC_the_planner_in__c,
                   Current_FSG_signed_and_on_file__c,
                   Client_consent_to_prepare_the_SOA__c,
                   Signed_option_to_quote_TFN__c,
                   ATP_Gearing_ATP_if_applicable__c,
                   Signed_Ongoing_advice_authority__c,
                   Copies_of_all_application_held_on_Serve__c,
                   Copy_of_SOA_on_server__c,
                   Copy_of_Fact_find_file_note_on_serve__c,
                   Confirmation_of_transaction_where_online__c,Id
               from Opportunity where Ownerid =:activeUser.id  and StageName =:'Signed Financial Plan and Insurance'];
       }        
        return Opp;
    

    }
}

Hope this will help you. 

Thanks
karthik
 
This was selected as the best answer
bathybathy
Sorry for the delay guys.

Thanks for your responses guys.
@Karthik your solution worked. Thank you so much.