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
Carlos LabastidaCarlos 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
 
Best Answer chosen by Carlos Labastida
DeveloperSudDeveloperSud
HI ,

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

DeveloperSudDeveloperSud
HI ,

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()
This was selected as the best answer
Carlos LabastidaCarlos Labastida
That worked great. Thank you!