function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sangeet kaseraSangeet kasera 

How to do Dynamic SOQL Query using NOT IN operator for Single String

I have one Dyanmic query using Not In operator but it is giving an error when i am trying to run my code i used several ways but still not found the solution.

I am not good in using escape character in where condition of SOQL
Please help me out
Query -
String 
CreatedById = '5006F00002iorNcQAI';
I Tried 
  •    customObjQuery = customObjQuery+' And (Createdby.Id NOT IN:CreatedById)'; 
  •   customObjQuery = customObjQuery+' And Createdby.Id =\''+IN:CreatedById+'\'';
Anyone also please explain the concept of using escape character in SOQL.

Regards,
Sangeet
Best Answer chosen by Sangeet kasera
ANUTEJANUTEJ (Salesforce Developers) 
As mentioned in https://developer.salesforce.com/forums/?id=906F0000000kCx2IAE can you once try below query once:

CreatedById = '5006F00002iorNcQAI';

customObjQuery = customObjQuery+  ' And Createdby.Id != '+ CreatedById;

Also can you once can you add below statement as well:

System.debug(customObjQuery);

Looking forward for your response.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sangeet,

As I see there is a documentation regarding this in the link : https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_quotedstringescapes.htm#:~:text=SOQL%20defines%20several%20escape%20sequences,is%20the%20backslash%20(%5C)%20character.

Does this helps??
Regards,
Anutej
Maharajan CMaharajan C
Hi Sangeet ,

Try the below:

String CreatedById = '5006F00002iorNcQAI';

customObjQuery = customObjQuery+  ' And Createdby.Id != \''+ CreatedById +'\'';

Thanks,
Maharajan.C

 
Sangeet kaseraSangeet kasera
Hi Mahajan,

when i am using querywhatever you have given then code is running fine but AND operator is not accepting in debug

Like I Used - commentQuery = commentQuery+' And Createdby.Id =\''+CreatedById+'\'';

In Debug I Found-
User-added image

And @AnuTej, I have already checked it but it didn't give the clear understanding of using escape character.
@Mahajan, If you have better knowlegde regarding Escape character please take a time and help me out by some example or with any doc.

Regards,
Sangeet

 
ANUTEJANUTEJ (Salesforce Developers) 
As mentioned in https://developer.salesforce.com/forums/?id=906F0000000kCx2IAE can you once try below query once:

CreatedById = '5006F00002iorNcQAI';

customObjQuery = customObjQuery+  ' And Createdby.Id != '+ CreatedById;

Also can you once can you add below statement as well:

System.debug(customObjQuery);

Looking forward for your response.
This was selected as the best answer