You need to sign in to do that
Don't have an account?
Dave Berenato
system.QueryException: invalid ID field: null
I have an Apex Class that pulls Contacts that are assigned using a custom field to a specific Door Knocker:
And I'm using the below Test Class:
The test fails and says "System.QueryException: invalid ID field: null" but shouldn't it pull the UserID into the myParam?
Also my system.debug didn't show up in the log.
I got the test class to pass when I took out
Any suggestions on how I can get this to work?
public with sharing class DoorKnockerAssigned { public DoorKnockerAssigned(){ if(ApexPages.currentpage().getParameters().get('leadgenerator') == null){myParam = UserInfo.getUserId();} else{myParam = ApexPages.currentpage().getParameters().get('leadgenerator');} } public String myParam { get; set; } public String sortOrder = ' Door_Knocker__r.Name '; public String ascendingOrDescending = ' ASC '; public void sortByDoorKnocker() {this.sortOrder = ' Door_Knocker__r.Name '; if(ascendingOrDescending ==' ASC ') {ascendingorDescending = ' DESC ';} else{ascendingorDescending = ' ASC ';}} String myRequestYesterday = ' SELECT id, Owner.Id,Owner.Name,Name, FirstName, LastName, Door_Knocker__r.Id,Assigned_Knock_Date__c'+ ' FROM Contact '+ ' WHERE Assigned_Knock_Date__c = YESTERDAY AND Door_Knocker__r.Id = \'' + myParam + '\' ORDER BY '; List<Contact> DKYesterday; public List<Contact> getDKYesterday() {DKYesterday = new List<Contact>();DKYesterday = Database.query(myRequestYesterday + sortOrder + ascendingOrDescending);return DKYesterday;} public void saveDKYesterday() {update DKYesterday;}
And I'm using the below Test Class:
@isTest public class DoorKnockerAssignedTest { static testMethod void TestDKAssignedYesterday(){ Account testaccount = new Account(); testaccount.name = '123 Test Drive'; testaccount.RecordTypeId = '0126A0000004Qo4'; testaccount.APN__c = '123'; testaccount.Foreclosure_Status__c = 'Auction'; testaccount.Property_Zip_Code__c = '90001'; insert testaccount; Contact testcontact = new Contact(); testcontact.Contact_Stage__c = 'Dialing'; testcontact.Phone_1__c = '6106754043'; testcontact.Accountid = testaccount.id; testcontact.LastName = 'Dave'; testcontact.OwnerId = '0056A000000HocdQAC'; testcontact.Door_Knocker__c = UserInfo.getUserId(); testcontact.RecordTypeId = '0126A0000004QlU'; testcontact.Assigned_Knock_Date__c = date.valueOf('2018-02-26 11:00:00z'); insert testcontact; system.debug(UserInfo.getUserId()); system.debug(testcontact.OwnerId); system.debug(testcontact.Door_Knocker__c); ApexPages.currentPage().getParameters().put('leadgenerator',UserInfo.getUserId()); DoorKnockerAssigned td = new DoorKnockerAssigned(); system.debug(td.myParam); List<Contact> list1 = td.getDKYesterday(); td.saveDKYesterday(); system.assertEquals(1, list1.size()); }
The test fails and says "System.QueryException: invalid ID field: null" but shouldn't it pull the UserID into the myParam?
Also my system.debug didn't show up in the log.
I got the test class to pass when I took out
AND Door_Knocker__r.Id = \'' + myParam + '\'from String myRequestYesterday so I know it's coming from that.
Any suggestions on how I can get this to work?
All Answers
The "ORDER BY" is followed by the variable "sortOrder" which is set through a public String.This allows me to build an Apex Table with sortable column headers.
When I added "Name" to the end of the SOQL Query, the error becomes "Unexpected token: Door_Knocker__r.Name" because that's the default sortOrder.