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
Victor PVictor P 

Function in an apex trigger that looks for null value and returns an empty space

I'm trying to create a quick function that I can reuse throughout the code that replaces a null value with an empty space.

Here's how I do it at the moment:
String.isBlank(opportunity.details__c)? '' : opportunity.details__c)
I need to reuse this a lot throughout the code so something simple like
CheckForNull(opportunity.details__c) would be easier.

Here's my poor attempt:
 
function CheckForNull (search) {
var checked = String.isBlank(search)? '' : search)
return checked;
}


 
Best Answer chosen by Victor P
Maharajan CMaharajan C
Hi Victor,

1. You can use String.isEmpty(empty) , String.isBlank(blank) methods for this:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

So you have to use like below:

public static String isEmptyString(String inputString)

String str = String.isBlank(inputString)?'': inputString;
return str;
}



=================================


So in Class you have to use like below:

Public class Sample
{

Public static void sampleMethod()
{
     String testString = isEmptyString( 'Not a Empty String');
}

Public static Boolean sampleMethod1()
{
     String testString = isEmptyString( '');
     return testString ;

}

public static String isEmptyString(String inputString)

String str = String.isBlank(inputString)?'': inputString;
return str;
}

}



Thanks,
Maharajan.C
 

All Answers

Maharajan CMaharajan C
Hi Victor,

1. You can use String.isEmpty(empty) , String.isBlank(blank) methods for this:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

So you have to use like below:

public static String isEmptyString(String inputString)

String str = String.isBlank(inputString)?'': inputString;
return str;
}



=================================


So in Class you have to use like below:

Public class Sample
{

Public static void sampleMethod()
{
     String testString = isEmptyString( 'Not a Empty String');
}

Public static Boolean sampleMethod1()
{
     String testString = isEmptyString( '');
     return testString ;

}

public static String isEmptyString(String inputString)

String str = String.isBlank(inputString)?'': inputString;
return str;
}

}



Thanks,
Maharajan.C
 
This was selected as the best answer
Victor PVictor P
public static String isEmptyString(String inputString)

String str = String.isBlank(inputString)?'': inputString;
return str;
}

THIS.

I was pretty close :). Thank you Maharajan.C!