• Carlos Labastida
  • NEWBIE
  • 10 Points
  • Member since 2015


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
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
 
Hello experts.. Starting on Apex and runnign into some testing trouble.. I wrote a trigger for updating the Last Update time for a Case when a related Task is created: 
trigger updateCaseByTask on Task (before insert) {
    list<id> caseIds = new list<id>();   
    
// Find if this task is related to a Case
    for(Task t : Trigger.new){
       if(string.valueOf(t.whatId).startsWith('500')){
          caseIds.add(t.whatId);
       }
    }
    
// Update Case to refresh Last Update 
    if (caseIds.size() > 0) {
      List<Case> caseList = [SELECT Id FROM Case WHERE Id IN :caseIds];
      update caseList;
    }
}
The trigger is working great. I then wrote a test for it, using an existing test Case I had previously created specificaly for this:
@isTest
public class updateCaseByTaskTest {

    @isTest static void testTaskCreation(){
        Case testCase = [SELECT Id, Subject, Status FROM Case WHERE Subject = 'Case to test updateCaseByTask trigger - DO NOT DELETE' Limit 1];
						

        String caseNumber = testCase.CaseNumber;
        DateTime caseLastModified = testCase.LastModifiedDate;
        Task testTask = new Task();
        // Fill out required Task fields
        testTask.WhatId = testCase.Id;
        testTask.Subject = 'Call';
        testTask.Priority = 'Normal';
        testTask.Status = 'In Progress';
        insert testTask;

        System.assertNotEquals(caseLastModified, testTask.LastModifiedDate);      
    }
}
When I run the query in the Developer's Console Query Editor (
SELECT Id, Subject, Status FROM Case WHERE Subject = 'Case to test updateCaseByTask trigger - DO NOT DELETE' Limit 1), it returns the record, but when the test runs it, it is not finding any result, and giving the error:

System.QueryException: List has no rows for assignment to SObject
Class.updateCaseByTaskTest.testTaskCreation: line 4, column 1


Am I missing anything obvious? Is there a certain format I should write the SOQL on?

Thanks for your help!



Hello. Starting on Apex and runnign into some testing trouble.. I wrote a trigger for updating the Last Update time for a Case when a related Task is created: 
trigger updateCaseByTask on Task (before insert) {
    list<id> caseIds = new list<id>();   
    
// Find if this task is related to a Case
    for(Task t : Trigger.new){
       if(string.valueOf(t.whatId).startsWith('500')){
          caseIds.add(t.whatId);
       }
    }
    
// Update Case to refresh Last Update 
    if (caseIds.size() > 0) {
      List<Case> caseList = [SELECT Id FROM Case WHERE Id IN :caseIds];
      update caseList;
    }
}

The trigger is working great. I then wrote a test for it, using an existing Test case I created specificaly for this:
 
@isTest
public class updateCaseByTaskTest {

    @isTest static void testTaskCreation(){
        Case testCase = [SELECT Id, Subject, Status FROM Case WHERE Subject = 'Case to test updateCaseByTask trigger - DO NOT DELETE' Limit 1];
						

        String caseNumber = testCase.CaseNumber;
        DateTime caseLastModified = testCase.LastModifiedDate;
        Task testTask = new Task();
        // Fill out required Task fields
        testTask.WhatId = testCase.Id;
        testTask.Subject = 'Call';
        testTask.Priority = 'Normal';
        testTask.Status = 'In Progress';
        insert testTask;

        System.assertNotEquals(caseLastModified, testTask.LastModifiedDate);      
    }
}

When I run the query in the Developer's Console Query Editor (
SELECT Id, Subject, Status FROM Case WHERE Subject = 'Case to test updateCaseByTask trigger - DO NOT DELETE' Limit 1), it returns the record, but when the test runs it, it is not finding any result, and giving the error:

System.QueryException: List has no rows for assignment to SObject
Class.updateCaseByTaskTest.testTaskCreation: line 4, column 1


Am I missing anything obvious? Is there a certain format I should write the SOQL on?

Thanks for your help!

 
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