I have below piece code where I want to assign 'No Value' to the string (Oldvalue) if it is Null. I have tried it by several approaches like (String.isblank etc), but not getting desired output. Can someone please help?

Class code snippet:

    oldvalue='No Value';


aht.Alert_Text_vod__c = alts.Alert_Text_abv__c + ' ' + oldvalue +' to ' + newValue;
  • 1
    What is the context that you're getting oldValue from? Might be better to have a little more of the code.
    – pchittum
    Sep 3 '15 at 10:54

Not sure what you could be doing wrong, but if I run the following code in an execute anonymous window it appears to do what you're after.

It could be that your string has been initialized which means it's a blank string rather than null. This is why it's important to use functions like isBlank() rather than checking for nulls.

String oldValue;

if(String.isBlank(oldValue)) {
    oldValue = 'No Value';

System.debug('oldValue: ' + oldValue);

Or you can do the assignment with a ternary operator:

String oldValue;

oldValue = String.isBlank(oldValue) ? 'No Value' : oldValue;

System.debug('oldValue: ' + oldValue);

Does this help?

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.