2

Using a dynamic soql query in my managed package , In my where condition am passing id value,

string whereCond = NameSpace('fldName') +' = \'' +keyType+ '\'' +' and ' + NameSpace('fldName') +' = \'' +obj.Id+ '\'' +' ORDER BY ' + NameSpace('fldName') +' asc ' 

if my obj.ID is null , its passing as 'Null' (with single quotation) am getting a error that INVALID field ID : NULL ,

NameSpace('fldName') +' = :'+obj.Id when I tried this code error in Dynamic soql query . In my test class i want to pass a null ID

3
  • Do you not want null to go through the soql? or do you want to pass null in the test class?
    – Mahmood
    Nov 21, 2017 at 6:15
  • You are getting 'NULL' with quotes because you are using ' = \'' +obj.Id+ '\'' in your SOQL Query. You need to validate your obj.Id should not be null. then your query will run. Nov 21, 2017 at 6:18
  • thanks guys :) yeah i want to pass null ID in test class, yeah validated before passing
    – SFDC
    Nov 24, 2017 at 6:30

2 Answers 2

1

I would write a method which implements a more generic approach.

public static String buildFilter(String field, String value)
{
    if (value != null)
        value = '\'' + String.escapeSingleQuotes(value) + '\''
    return field + '=' + value; 
}

Then you would use this method instead.

query += 'WHERE ' + builder(namespace('fieldName'), value);
3

As the above said just add this to the beginning of your method

If(obj == null  || obj.Id == null){
     //throw and error or return empty list
}
1
  • thanks buddy it helped :) error occurs only in test class
    – SFDC
    Nov 24, 2017 at 6:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .