You need to sign in to do that
Don't have an account?
Carlos Labastida
userInfo.getusername() not returning expected value
Hello. I am having an issue with UserInfo.getUserName() apparently not returning the expected result in a query inside an Apex class. The code is as follows:
public class MACTicketsByDoctorController {
private String sortOrder = 'Bug_Open_days__c';
private String doctorName = 'Greg Thompson';
public List<SFDC_Ticket__c> getTickets() {
List<SFDC_Ticket__c> results = Database.query(
'SELECT Id, Title__c, MCS_Assignment__c, CreatedDate, Medical_Resource_Assignment__r.UserName, Total_Business_Days_Open__c, Status__c ' +
'FROM SFDC_Ticket__c ' +
'WHERE Medical_Resource_Assignment__r.Username = \''+UserInfo.getUserName()+'\'' +
'AND Status__c = \'Closed\' ' +
'ORDER BY ' + sortOrder + ' DESC ' +
'LIMIT 10'
);
return results;
}
}
This controller is being used in the following Visualforce page:
<apex:page controller="MACTicketsByDoctorController">
<apex:form >
<apex:pageBlock title="Open MAC Tickets" id="tickets_list">
<!-- Tickets List -->
<apex:pageBlockTable value="{! tickets }" var="tix" rendered="{!tickets.size>0}">
<apex:column headerValue="Ticket">
<apex:outputLink target="_parent" value="/{!tix.Id}">{! tix.Title__c }</apex:outputLink>
</apex:column>
<apex:column headerValue="Assigned to" value="{! tix.MCS_Assignment__c }"/>
<apex:column value="{! tix.CreatedDate }"/>
<apex:column value="{! tix.Medical_Resource_Assignment__r.UserName }"/>
<apex:column headerValue="Days open" value="{! tix.Total_Business_Days_Open__c }"/>
<apex:column value="{! tix.Status__c }"/>
</apex:pageBlockTable>
<apex:pageMessage summary="No Tickets assigned" severity="info" rendered="{!tickets.size=0}" />
</apex:pageBlock>
</apex:form>
</apex:page>
Medical_Resource_Assignment__c is a lookup to User.
if I replace line
'WHERE Medical_Resource_Assignment__r.Username = \''+UserInfo.getUserName()+'\'' +
with
'WHERE Medical_Resource_Assignment__r.Username = \'John.Doe@organization.com\'' +
then I get the correct result for John Doe Tickets. I wonder what am I doing wrong?
Thanks for any Help!
-Carlos
public class MACTicketsByDoctorController {
private String sortOrder = 'Bug_Open_days__c';
private String doctorName = 'Greg Thompson';
public List<SFDC_Ticket__c> getTickets() {
List<SFDC_Ticket__c> results = Database.query(
'SELECT Id, Title__c, MCS_Assignment__c, CreatedDate, Medical_Resource_Assignment__r.UserName, Total_Business_Days_Open__c, Status__c ' +
'FROM SFDC_Ticket__c ' +
'WHERE Medical_Resource_Assignment__r.Username = \''+UserInfo.getUserName()+'\'' +
'AND Status__c = \'Closed\' ' +
'ORDER BY ' + sortOrder + ' DESC ' +
'LIMIT 10'
);
return results;
}
}
This controller is being used in the following Visualforce page:
<apex:page controller="MACTicketsByDoctorController">
<apex:form >
<apex:pageBlock title="Open MAC Tickets" id="tickets_list">
<!-- Tickets List -->
<apex:pageBlockTable value="{! tickets }" var="tix" rendered="{!tickets.size>0}">
<apex:column headerValue="Ticket">
<apex:outputLink target="_parent" value="/{!tix.Id}">{! tix.Title__c }</apex:outputLink>
</apex:column>
<apex:column headerValue="Assigned to" value="{! tix.MCS_Assignment__c }"/>
<apex:column value="{! tix.CreatedDate }"/>
<apex:column value="{! tix.Medical_Resource_Assignment__r.UserName }"/>
<apex:column headerValue="Days open" value="{! tix.Total_Business_Days_Open__c }"/>
<apex:column value="{! tix.Status__c }"/>
</apex:pageBlockTable>
<apex:pageMessage summary="No Tickets assigned" severity="info" rendered="{!tickets.size=0}" />
</apex:pageBlock>
</apex:form>
</apex:page>
Medical_Resource_Assignment__c is a lookup to User.
if I replace line
'WHERE Medical_Resource_Assignment__r.Username = \''+UserInfo.getUserName()+'\'' +
with
'WHERE Medical_Resource_Assignment__r.Username = \'John.Doe@organization.com\'' +
then I get the correct result for John Doe Tickets. I wonder what am I doing wrong?
Thanks for any Help!
-Carlos
If the custom field Medical_Resource_Assignment is in object SFDC_Ticket__c and the filed is a lookup to user names you need write the query like where Medical_Resource_Assignment__c=:Userinfo.getUserId()
All Answers
If the custom field Medical_Resource_Assignment is in object SFDC_Ticket__c and the filed is a lookup to user names you need write the query like where Medical_Resource_Assignment__c=:Userinfo.getUserId()