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

  • Do you not want null to go through the soql? or do you want to pass null in the test class? – Mahmood Nov 21 '17 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. – Suraj Tripathi Nov 21 '17 at 6:18
  • thanks guys :) yeah i want to pass null ID in test class, yeah validated before passing – SFDC Nov 24 '17 at 6:30
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
}
  • thanks buddy it helped :) error occurs only in test class – SFDC Nov 24 '17 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.