cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if a field is empty on a Unity form?

David_Chatterto
Star Collaborator
Star Collaborator

The below works, it outputs an empty value to the diagnostic console. However if I try to check if the siteManager field is empty, I get an error.

 

// Returns the current FormInstance
CustomActionForm thisForm = args.FormInstance;

// Initialize a new form modifier for changing a field.
FormModifier formModifier = thisForm.CreateUnityFormModifier();

// Return the name of the form
string formName = thisForm.FormTemplate.Name;

if (formName == "Trainee Development Review")
{
ValueField fieldValue = thisForm.AllFields.ValueFields.Find("sitemanager");

app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Verbose,
String.Format("SiteManager is empty? = {0}", fieldValue));

}

10 REPLIES 10

@George Sialmas it doesn't like this line:

 

if (string.IsNullOrWhiteSpace(fieldValue.AlphaNumericValue) || (fieldValue.IsEmpty))

 

"Object reference not set as an instance of an object"

 

I had to add the 'AlphaNumericValue' to fieldValue, otherwise I get a 'cannot convert from ValueField to string.

 

Also, this works fine:

 

ValueField fieldValue = thisForm.AllFields.ValueFields.Find("sitemanager");

app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Verbose,
String.Format("SiteManager is empty? = {0}", fieldValue));

 

I get a blank entry in the diagnostic console. However if I try fieldValue.isEmpty or fieldValue.AlphaNumericValue, I get the error.

 

I don't know what's happening here.

In what context is the code being executed? Can you share what the script looks like?

@George Sialmas I have expanded the code in the original post.

 

I'm very much scratching my head at this point.

I agree with @George Sialmas that it sounds like your fieldValue parameter is null, and you are getting the error because you are dereferencing a property off the null object.

 

You can check this way if (fieldValue?.IsEmpty ?? true)

@David Chatterton I have questions:

 

1. In what context is the code being executed? Workflow, Scheduler, CustomActionEvent? I'm trying to understand why you are checking fieldValue is null.

2. How were you checking if the property (fieldValue) was null? Can you tell me what you had in your IF statement? The reason I'm asking is because whenever I've wanted to check if a fieldValue is null I would use the following:

 

If ((fieldValue ==null) || (fieldValue.IsEmpty))

{

    app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Verbose, "{0}: fieldValue {1} is empty.", SCRIPT_NAME, fieldValue);

}

 

and it worked for me.