If I have a custom setting with some fields and I want to query those fields, in my controller I'm doing the following:

public My_Custom_Setting__c getMyCustomSetting() {
    return = [select URL__c, Title__c from My_Custom_Setting__c];

If these fields are empty, I want to assign default values to them.

Something like the following:

public My_Custom_Setting__c getMyCustomSetting() {
    String title;
    String url;

    My_Custom_Setting__c cs = [select URL__c, Title__c from My_Custom_Setting__c];

    title = cs.Title__C || 'Title default';
    url = cs.URL__c || 'URL default';

    cs.Title__C = title;
    cs.URL__c = url;

    return cs;

I'm getting the following error when attempting to save:

Save error: OR operator can only be applied to Boolean expressions

So I can't use || in this way.

How do I assign my default values to my returned object?

Am I approaching this the right way?


You have 2 ways to do that :

if (!String.isBlank(cs.Title__c)){
    title = cs.Title__c ;
} else {
    title = 'Title default';


title = (!String.isBlank(cs.Title__c)) ? cs.Title__c : 'Title default';

This is one seems silimar to what you tried to do

  • Ah, I didn't know about isBlank. Cheers! – Daft Feb 13 '15 at 15:20

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.