Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How do I access fields in instances and compare value?

Avatar

Former Community Member

I use the instance manager to add subforms to a form. In the subform I have a field “KtNr” and I would like make a warning if the same data has been typed in this field for more than one instance.

I tried this code on the validate event of the field “KtNr”, but it does not work.

var subDetail = subform1.reolveNodes("MyInstance[*]");

for (i=0; i<subDetail.length;i++)
{
if (subDetail.item.(i).reolveNode("KtNr").rawValue == subDetail.item.(i).reolveNode("KtNr").rawValue)
{
xfa.host.messagebox("You have entered the same KtNr for more than one item.");
}
}

Am I on the right track or…?

Kirstine

3 Replies

Avatar

Level 5

Hi Kirstine,

You are close just a couple of comments

you have the code

subDetail.item.(i).reolveNode("KtNr").rawValue

this should be ( removal of the "." before the "(i)" )

subDetail.item(i).reolveNode("KtNr").rawValue

And the other point is that you are only going to compare the exact same value so it is always going to return true, I think you need to change you code to be something more like this (and this is off the top of my head so there may be errors)

var subDetail = subform1.reolveNodes("MyInstance[*]");

var numSubDetail = subDetail.length;

for ( var i = 0; i < numSubDetail; i++)

{

     for ( var j = 0; j < numSubDetail; j++)

     {

          if ( i != j) // only compare different instances of KtNr

          {

               if ( subDetail.item(i).resolveNode("KtNr").rawValue == subDetail.item(j).resolveNode("KtNr").rawValue)

               {

                    xfa.host.messagebox("You have entered the same KtNr for more than one item.");

               }

           } 

     }

}

Hope this helps

Malcolm

Avatar

Former Community Member

Hi Malcolm

Thanks for your answer. I couldn’t make it work, but it let me in the right direction.

This is how I managed to solve my problem:

if (subform1.MyInstance.instanceManager.count>1)//to check if the form has more than one instance

{

if(xfa.resolveNode("subform1.MyInstance ["+(subform1.MyInstance.instanceManager.count-1)+"].KtNr").rawValue == xfa.resolveNode("subform1.MyInstance ["+(subform1.MyInstance.instanceManager.count-2)+"].KtNr").rawValue)

    {

    xfa.host.messageBox("Error");

    }

else{

    xfa.host.messageBox("No errors");

    }

}

Kirstine

Avatar

Former Community Member

This is just for the record.

After some testing I discovered that my code only works if there is the same value for KtNr in instances right after one another, i.e. number 2 and 3 and not number 2 and 4.

I changed my script to be more like the one Malcolm suggested and it looks like it works.

var nEffekt1 = subform1.MyInstance.instanceManager.count

if (nEffekt1>1)

{

for (var i=0; i<nEffekt1; i++)

  {

for (var j=1; j<nEffekt1; j++)

    {

    if (i!=j)

     {

if(xfa.resolveNode("subform1.MyInstance ["+i+"].KtNr").rawValue == xfa.resolveNode("subform1.MyInstance ["+j+"].KtNr").rawValue)

    {

    xfa.host.messageBox("Error");

    }

else{

    xfa.host.messageBox("No errors");

    }

      }

    }

  }

}

Kirstine