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
JBabuJBabu 

to query on custom settings field

Hi,

 

I have made opportunity stage name as custom settings field (hierarchy type). (name:Opp_Stg_Mappings__c)

 

I need to query on open opportunities and I did the below way.

 

[select Id, ownerId from Opportunity where  stageName  != Opp_Stg_Mappings__c.getOrgDefaults().X100_Stage_Name__c];

 

I am getting error message

"Error: Compile Error: unexpected token: 'Opp_Stg_Mappings__c.getOrgDefaults' ".

 

Please let me know how do I resolve this issue.

 

Thanks,

JBabu.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

You are making a call (getorgdefaults) in your SOQL query.   As far as I know, you can only use bind variables in your SOQL and those require a colon appear before the bind variable.

 

That is why you are getting the unexpected token.

 

I would recommend that prior to your SOQL, that you read your default into a var named "CurrentStageName" and then use this as your SOQL:

 

[select Id, ownerId from Opportunity where  stageName  != :CurrentStageName];

All Answers

colemabcolemab

You are making a call (getorgdefaults) in your SOQL query.   As far as I know, you can only use bind variables in your SOQL and those require a colon appear before the bind variable.

 

That is why you are getting the unexpected token.

 

I would recommend that prior to your SOQL, that you read your default into a var named "CurrentStageName" and then use this as your SOQL:

 

[select Id, ownerId from Opportunity where  stageName  != :CurrentStageName];

This was selected as the best answer
JBabuJBabu

Thanks a lot colemab it worked.

sfdcfoxsfdcfox

You can bind to functions as well:

 

[select Id, ownerId from Opportunity where  stageName  != :Opp_Stg_Mappings__c.getOrgDefaults().X100_Stage_Name__c];

Note the ":" before the reference. The extra variable shouldn't be necessary. I'm including this comment only for completion. colemab's answer is also perfectly acceptable. Note that function binding is not supported in dynamic SOQL (so you can't use it with Database.query(string, boolean)).

 

Other common examples:

 

Map<Id, Account> accounts = ...
...

accounts.putAll([SELECT ... FROM Account WHERE Id IN :accounts.keySet()]);

 

... [SELECT ... FROM Account WHERE Name IN :accountList()];
// accountList is a function returning a list of accounts.

 

... [SELECT ... FROM Contact WHERE AccountId IN :Trigger.newMap.keySet()];
// inside a contact trigger.

 

colemabcolemab
SFDCFox, I never thought about binding to a function - thanks for sharing!