You need to sign in to do that
Don't have an account?
Rishabh Patel 1
Test Class for Dynamic SOQL
I am doing a SOQL query and then updating records. The trick here is . I am grabbing my query from two custom fields which are in the Custom Settings . My code works fine records are getting queried and updated. I am having big trouble in making test class for this . Help is appriciated . Here is my Apex Class -
public with sharing class textInputsConsecond implements Schedulable { public list<Lead> quo{get;set;} public void execute(SchedulableContext ctx){ quo= Database.query(Lead__c.getInstance().Database_Query__c + Lead__c.getInstance().Database_Query2__c); for(Lead ldt :quo){ //Update those leads with new owner ID ldt.OwnerId = '00G3B000001hp6VUAQ'; } update quo; } }
Here is the screenshot of my Query in the Field in custom settings -
So my query is basically Database Query field + Database Query2 Field
The only reason to use two fields is salesforce is not allowing text box more that 255 characters
Remove the hardcoded Id from the Code and update the code as below
And use the test class like this
All Answers
Remove the hardcoded Id from the Code and update the code as below
And use the test class like this
Yes, hardcoded Ids are bad, but if we look at the OP post, the query is for Groups '00Gxxxxxx' therefore
The OP code is doing a query of a certain group owner and converting to a new group, so we need the new group Owner
Asserts - without them, test code is rubbish. How do you know the code actually worked and how do you know it won't be broken when more code / other processes are updated.?
@Rishabh
Hope the additional notes above help.
Regards
Andrew
Hello Raj and Andrew, Really appriciate you guys answering the question . So I used Rajs code and queried the Group as andrew said. The code is working fine
Raj , When I rum the test class. I am getting this error. If you can tell what Exatly am I missing
Time Started2/19/2019 10:40 AM
ClasstextInputsConsecondTest
Method NametestDailyLeadProcessorScheduledJob
Pass/FailFail
Error MessageSystem.QueryException: unexpected token: nullnull
Stack TraceClass.textInputsConsecond.execute: line 7, column 1
Can you post the code and test class you eventually built? I am assuming you blended the two lots of code; so it would be easier to understand the error if we see the code.
Regards
Andrew
Hey Andrew, Thanks for the reply . Sorry I forgot to add the test class.
Here you go
I am adding task to the leads too . Cause in my main query . I am getting the leads with last activity date within 7 days
Based on line 7 and the message: Error MessageSystem.QueryException: unexpected token: nullnull
I would stick some debugs around the creation of the query; perhaps HTH
Andrew
Hey Andrew, Thanks a lot for the reply. I finally understood what the problem is. Apex is not able to grab the Data from Custom Settings and shows Null . When I moved everything to Custom Metadata. That worked .
Thanks again for you help on this. Really appriciate it