+ Start a Discussion
murdocmurdoc 

Current user Id in apex class

I need to compare Owner ID and Opportunity Team Member to a User ID in an Apex Class, if the ID's don't match they can't access the VF page. Any idea what this would like. Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen
I think I see what you're trying to do now. What I would do is run a SOQL query for the related child OpportunityTeamMember record using the current user and current opportunity id. Something like [SELECT Id, UserId, OpportunityId FROM OpportunityTeamMember WHERE UserId = :theUserId AND OpportunityId = :oppId] I would do this in your SAPSecurityClass and use the fact whether a value was returned or not from the query as T/F condition to allow the VF page to render.

All Answers

k_bentsenk_bentsen

By current user, you mean the user viewing the record/page, correct? If so, then you can use UserInfo.getUserId(); method to retrieve the id of said user.

murdocmurdoc

Thanks, that worked, but now I'm still running into issues with the Opportunity Member ID. Any ideas

k_bentsenk_bentsen

I'd love to help, but your issue is a little vague. Do you mean you're having issues obtaining the Opp Member Id? Or how to use it to retrieve OpportunityMemeber data? Or using the data to compare against the current user id and/or opportunity owner id?

 

Also, a little more information on the usage of your VF page would be helpful. Showing your code is probably the easiest way to help, but I can understand if you don't want to.

murdocmurdoc

Right now obtaining the ID would be good. Having a hard time calling it out. I can do the camparison if I could find out how to obtain the Team Member ID.The code is below.

 

<apex:page sidebar="false" standardController="Opportunity" showHeader="false" extensions="SAPSecurityClass">
<script>
    function SAPReport(){
    
        //var SAPUrl = "http:\/\/dalsserv.freemanco.com\/irj\/servlet\/prt\/portal\/prtroot\/pcd!3aportal_content!2fcom.sap.pct!2fplatform_add_ons!2fcom.sap.ip.bi!2fiViews!2fcom.sap.ip.bi.bex?QUERY=Z0PS_C041_Q0010"; 

var SAPUrl = "http:\/\/salesforce.freemanco.com\/Reports\/BEXProxy.html?"; 

//var SAPUrl = "http:\/\/devaaron.com\/bexproxy.html?"; 

var TestUrl = "https:\/\/www.google.com\/"; 

var p1 = "0S_PRJ"; //=jobnumber 
var p2 = "ZPRJ_SET"; //=projectset 
//var p3 = "0S_FYEAR"; //=fiscalYear 
//var p4 = "0I_PER3"; //=period 
var p5 = "ZPSTNG_DT_SEL"; //=postingDateRange 
var p6 = "0S_COCD"; //=companyCode 
var p7 = "0IEP_CUR"; //=currencyType 
var p8 = "0P_VERS"; //=version 
var versionValue = "200";   

//Company Code 
var HiddenJobNumber = '{!Opportunity.Job_Number_Hidden__c}'; 
var CompanyCode = HiddenJobNumber.substring(0, 1); 
if (CompanyCode < 10) {CompanyCode = '0'+CompanyCode} 


//Project Set ID If Empty 

//var ProjectSetID = "P789"; 
//var JobNumber = 1234; 
var p1value; 
var p2value; 

if ('{!Opportunity.Project_Set_ID__c}' == "" || '{!Opportunity.Project_Set_ID__c}' == null ) { 
p1value = '{!Opportunity.Event_Number__c}'; 
} 
else 
{ 
p2value = '{!Opportunity.Project_Set_ID__c}'; 
} 


//Producing Branch = Chicago 
if ('{!Opportunity.Producing_Branch__c}' == "FDC CHICAGO (114)"){ 
CompanyCode = "01%3B06" 
} 

//Producing Branch = Toronto 
if ('{!Opportunity.Producing_Branch__c}' == "FDL TORONTO (833)"){ 
CompanyCode = "08" 
} 

//Producing Branch = Ottawa 
if ('{!Opportunity.Producing_Branch__c}' == "FDL OTTAWA (844)"){ 
CompanyCode = "08" 
} 

//Producing Branch = Montreal 
if ('{!Opportunity.Producing_Branch__c}' == "FDL MONTREAL (836)"){ 
CompanyCode = "08" 
} 

//get Posting End Date 

var today = new Date(); 
var dd = today.getDate(); 
//var dd = 18; 
var PostingStartDate = "07/01/2006"; 
var PostingEndDate = "Hello"; 
var m1 = new Date(today.getFullYear(), today.getMonth(), 0); 
var m2 = new Date(today.getFullYear(), today.getMonth()-1, 0); 
var OneMonthAgo = (m1.getMonth()+1)+'/'+m1.getDate()+'/'+m1.getFullYear(); 
var TwoMonthsAgo = (m2.getMonth()+1)+'/'+m2.getDate()+'/'+m2.getFullYear(); 

if ((m1.getMonth()+1) < 10) 
{ 
OneMonthAgo = '0'+(m1.getMonth()+1)+'/'+m1.getDate()+'/'+m1.getFullYear(); 
} 

if ((m2.getMonth()+1) < 10) 
{ 
TwoMonthsAgo = '0'+(m2.getMonth()+1)+'/'+m2.getDate()+'/'+m2.getFullYear(); 
} 

if (dd == 1) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 2) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 3) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 4) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 5) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 6) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 7) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 8) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 9) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 10) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 11) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 12) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 13) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 14) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 15) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 16) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 17) 
{ 
PostingEndDate = TwoMonthsAgo; 
} 
else if (dd == 18) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 19) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 20) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 21) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 22) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 23) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 24) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 25) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 26) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 27) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 28) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 29) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 30) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else if (dd == 31) 
{ 
PostingEndDate = OneMonthAgo; 
} 
else 
{ 
PostingEndDate = "ERROR"; 
} 

//alert(TestUrl); 
//alert(OneMonthAgo); 

if (p1value == " " || p1value == null){ 

var SAPUrlp2 = SAPUrl + "&" + p2 + "=" + p2value + "&" + p5 + "=" + PostingStartDate + "%20-%20" + PostingEndDate + "&" + p6 + "=" + CompanyCode + "&" + p7 + "=" + "USD" + "&" + p8 + "=" + versionValue; 

//alert(SAPUrlp2); 

window.open(SAPUrlp2);  


} else { 

var SAPUrlp1 = SAPUrl + "&" + p1 + "=" + p1value + "&" + p5 + "=" + PostingStartDate + "%20-%20" + PostingEndDate + "&" + p6 + "=" + CompanyCode + "&" + p7 + "=" + "USD" + "&" + p8 + "=" + versionValue; 

//alert(SAPUrlp1); 

window.open(SAPUrlp1); 


}
    }
</script>
  <a href="#" onclick="SAPReport()">SAP Report</a>
   <!-- <apex:outputLink onclick="SAPReport()">SAP Report</apex:outputLink> -->
   <!-- <br /><apex:outputText rendered="{!hasAccess}" value="{!IF(Opportunity.OwnerId = $User.Id, 'You are the Opportunity owner or are part of the Opportunity Team and can click the link', 'SAP Report link is not displayed because you are not the Opportunity Owner or a member of Opportunity Team')}"></apex:outputText> --> 
   <!-- <br /><br /><apex:outputText value="{!Debug}"></apex:outputText> -->
</apex:page>

 

k_bentsenk_bentsen
I think I see what you're trying to do now. What I would do is run a SOQL query for the related child OpportunityTeamMember record using the current user and current opportunity id. Something like [SELECT Id, UserId, OpportunityId FROM OpportunityTeamMember WHERE UserId = :theUserId AND OpportunityId = :oppId] I would do this in your SAPSecurityClass and use the fact whether a value was returned or not from the query as T/F condition to allow the VF page to render.
This was selected as the best answer
murdocmurdoc

This worked great. Thanks for prompt and accurate solution!

k_bentsenk_bentsen

Glad to help!