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
Elite Collaborator
Elite Collaborator

@David Chatterton If you use the If statement like you have tried but try this:

 

if (fieldValue.IsEmpty)

 

Good luck,

GS

@George Sialmas thanks for the answer, unfortunately that doesn't work.

 

Even when I write to diagnostics, if the field is not empty, I see the value in the diagnostic console. If it IS empty, I see an error.

@David Chatterton Have you got access to VS (IDE) to test your code? I find VS is much better for troubleshooting than OnBase Studio.

 

In any case, can you try something for me? Instead of the code you mentioned in your initial post 

 

string fieldValue = thisForm.AllFields.ValueFields.Find("fieldname").Value;

 

Can you replace it with the following? Use the ValueField class

 

ValueField fieldValue = args.FormInstance.AllFields.ValueFields.Find("fieldname");

 

Then use the following to evaluate if the property fieldValue is null.

 

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

 

Good luck,

GS

@David Chatterton I forgot to ask you how were you checking if the property (fieldValue) is null? Can you tell me what you had in your if statement?