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
Kristen LundKristen Lund 

Add profile to string query

Hello Developers - - I have a string query that I use to identify users, but am struggling adding the unique profile to the current string:
 
String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND User__r.IsActive = true';
I've tried to add the profile via User__r.Profile = 18digit  User__r.ProfileId = 18digit, User__r.Profile.Id = 18 digit, User__r.ProfileName = Community Name, but nothing has worked. I don't feel like it should be this difficult to add in a profile to the string. Any suggestions would be greatly appreciated! Thank you!

 
Best Answer chosen by Kristen Lund
Alain CabonAlain Cabon
Hi Kristen,

You have to escape the simple quotes with "\" (antislash) here.

String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId =\'18Digit\'';
 
String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId =\'18Digit\'';

It is a common problem with queries in apex.

Alain

All Answers

Alain CabonAlain Cabon
Hello Kristen,

User__r.Profile.Id or User__r.ProfileId = '18 digit'  : should work for sure.

What is the error?
Kristen LundKristen Lund
Hello Alain - - that's what I thought too! No error that I'm seeing, its just not running successfully. It's a job to deactivate users, so I schedule the job hourly and, while it's noted as running, users are not being deactivated. All versions as noted above save without any issues.
Alain CabonAlain Cabon
Your query returns no result (query editor of the developer console)? 
Kristen LundKristen Lund
Sorry about that - I missed that error initially! Not that its super helpful: "Uknown error parsing query". I tried again with those listed above. I've even now tried with the contact record type, but still trowing that same error.
SELECT Id, User__c FROM Contact WHERE User__c != null AND User__r.IsActive = true AND RecordType = 18Digit

 
Kristen LundKristen Lund
Hi Alain - yes, I'd prefer the profile, but I'm just trying to get anythign to work. The record type Id of the contacts is unique as well, so I figured I'd try using that instead. Unfortunately, using RecordTypeId doesn't work either. Same response. As soon as I remove the AND RecordTypeId = 18Digit - - it works fine.
Alain CabonAlain Cabon
There is not this record type for your contacts or it is inactive. We always query: AND RecordTypeId = '(18digit)'    (simple quotes).
Strange.  ( Setup > Contact : Record Types)

select id, name,sobjecttype from recordtype where sobjecttype  = 'Contact'
 
Kristen LundKristen Lund
Hi Alain - - so this does work in the query editor:
SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId = '18Digit'
But as soon as I put it in my code, the punctuation needs to be updated and it no longer works. Ugh, so frustrating!
String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId = 18Digit';
Since I can't put quotes within quotes, any idea how I should be punctuating this so the query continues to work in the class?

 
Alain CabonAlain Cabon
Hi Kristen,

You have to escape the simple quotes with "\" (antislash) here.

String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId =\'18Digit\'';
 
String query = 'SELECT Id, User__c FROM Contact WHERE User__c != null AND RecordTypeId =\'18Digit\'';

It is a common problem with queries in apex.

Alain
This was selected as the best answer
Alain CabonAlain Cabon
And that should be the same problem for ProfileId in your query.

 User__r.ProfileId = \' 18 digit  \'   inside a query delimited itself with simple quotes.
 
Kristen LundKristen Lund
Success! Woohoo - I was just able to make those changes and run the job successfully. Obviously I'm new to some of this, so I very much appreciate the guidance of these boards. Thank you so much for your help, Alain!