Skip to main content
Inspiring
November 8, 2021
Answered

Determine if field exists

  • November 8, 2021
  • 1 reply
  • 1204 views

Hi all,

 

Have  asituation where I have icons associated with 'parent' text fields. Trying to build a function to hide all (part of a reset process but built to hide 1 set at a time). The problem is, I get errors if the icon doesn't exist for a given parent.

 

function clearIcons(buildingName, buildingNameOrig){

    buildingName = buildingNameOrig+"_Elec_af_image";                   // Convert field name to icon name from safe var
    if(typeof(buildingName) != "undefined"){
        this.getField(buildingName).display = 1; }                      // Hide the icon

    buildingName = buildingNameOrig+"_Water_af_image";                  // Convert field name to icon name from safe var
    if(typeof(buildingName) != "undefined"){
        this.getField(buildingName).display = 1; }                      // Hide the icon

 In this case, it's fine if all parent buildings have electrical and water connections but fails if a building only has 1. I think I just need the correct method of detecting a fields absence for the 'if' statement but if there's a better way to do, I'm open.

 

Thanks,

This topic has been closed for replies.
Correct answer Bernd Alheit

You can test it with:

 

if (this.getField(buildingName)) {

 

}

 

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
November 8, 2021

You can test it with:

 

if (this.getField(buildingName)) {

 

}

 

Jimmy5E2FAuthor
Inspiring
November 8, 2021

Welp; seems I was overcomplicating! I could have sworn I tried that but I must have been forcing comparisons into the 'if'.

 

Anyway, that works perfectly, thank you!

Bernd Alheit
Community Expert
Community Expert
November 8, 2021

In your script typeof(buildingName) is always a string.