How to make a script that can work universally across any page and any object | Community
Skip to main content
Level 3
February 8, 2022

How to make a script that can work universally across any page and any object

  • February 8, 2022
  • 2 replies
  • 3942 views

I have a script, that due to how they wanted the form to look and all of these alerts that are necessary, there are 13 scripts which span across multiple pages, and multiple objects, and was trying to find a way that all I would have to do is change the field names, so i was wondering if someone could help me out, as I was trying to search for it, and I could not find it.

Here is the script

if(pmp.Page1.object1.kpi1.rawValue == null) {// Field is empty, other fields not required pmp.Page1.object1.Table1.Row1.ratingbox1_1.mandatory = ""; pmp.Page1.object1.Table1.Row1.ratingbox1_2.mandatory = ""; pmp.Page1.object1.Table1.Row1.ratingbox1_3.mandatory = ""; pmp.Page1.object1.Table1.Row1.ratingbox1_4.mandatory = ""; pmp.Page1.object1.Table1.Row1.ratingbox1_5.mandatory = ""; } else if(pmp.Page1.object1.kpi1.rawValue != null) {// Field has data, other feilds required pmp.Page1.object1.Table1.Row1.ratingbox1_1.mandatory = "error"; pmp.Page1.object1.Table1.Row1.ratingbox1_2.mandatory = "error"; pmp.Page1.object1.Table1.Row1.ratingbox1_3.mandatory = "error"; pmp.Page1.object1.Table1.Row1.ratingbox1_4.mandatory = "error"; pmp.Page1.object1.Table1.Row1.ratingbox1_5.mandatory = "error"; }

 

So the pages this code need to be on are pages 1 to 6, and the objects go from 1 to 13, then the ratingbox field changes from ratingbox1_ to ratingbox13_

 

I was going through and using the search and replace function, and saving the form every change, but i just realized, after going back to multiple fields, the code reverted back to the above code, so I was trying not to have to do that again.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

MHWinter
Level 4
February 8, 2022

I guess you could create a loop running through each object, i.e. from 1 thru 13, create a string variable to specify the targeted object and set the mandatory status using the resolveNode property. Something like:

for (var i=1; i<=13;i++){

  var sTarget = "object"+i;

  if(this.resolveNode(sTarget).kpi1.rawValue == null){   // Field is empty, other fields not required
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_1.mandatory = "disabled";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_2.mandatory = "disabled";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_3.mandatory = "disabled";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_4.mandatory = "disabled";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_5.mandatory = "disabled";

  } else  {  // Field has data, other fields required
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_1.mandatory = "error";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_2.mandatory = "error";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_3.mandatory = "error";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_4.mandatory = "error";
    this.resolveNode(sTarget).Table1.Row1.ratingbox1_5.mandatory = "error";

  }

}

 

[I learned that recently - thanks to this community - that while somefield.mandatory = ""; works, the preferred statement would be somefield.mandatory = "disabled";]

 

MHWinter
Level 4
February 8, 2022

And now I realize the rating box fields also change numbers,... so here is a revised script

for (var i=1; i<=13;i++){

  var sTarget = "object"+i;

  var sTargetRatingBox_1 = sTarget+".Table1.Row1.ratingbox"+i+"_1";

  var sTargetRatingBox_2 = sTarget+".Table1.Row1.ratingbox"+i+"_2";

  var sTargetRatingBox_3 = sTarget+".Table1.Row1.ratingbox"+i+"_3";

  var sTargetRatingBox_4 = sTarget+".Table1.Row1.ratingbox"+i+"_4";

  var sTargetRatingBox_5 = sTarget+".Table1.Row1.ratingbox"+i+"_5";

  if(this.resolveNode(sTarget).kpi1.rawValue == null){   // Field is empty, other fields not required
    this.resolveNode(sTargetRatingBox_1 ).mandatory = "disabled";
    this.resolveNode(sTargetRatingBox_2 ).mandatory = "disabled";
    this.resolveNode(sTargetRatingBox_3 ).mandatory = "disabled";
    this.resolveNode(sTargetRatingBox_4 ).mandatory = "disabled";
    this.resolveNode(sTargetRatingBox_5 ).mandatory = "disabled";

  } else  {  // Field has data, other fields required
    this.resolveNode(sTargetRatingBox_1 ).mandatory = "error";
    this.resolveNode(sTargetRatingBox_2 ).mandatory = "error";
    this.resolveNode(sTargetRatingBox_3 ).mandatory = "error";
    this.resolveNode(sTargetRatingBox_4 ).mandatory = "error";
    this.resolveNode(sTargetRatingBox_5 ).mandatory = "error";

  }

}

Level 3
February 8, 2022

There are exceptions to that I believe: if the script is included in a script object or attached to an object of the master page, I believe the complete SOM would be needed… but I’m not 100% sure. 


ok, well when i get to that point, I will have to take a better look.  I tested the script out and it works perfectly on the first object because the field it is in is "kpi1" and I forgot to mention that the "kpi" field is from "kpi1" to "kpi13" as well, so I have been experimenting with different combinations to this line of code:

 if(this.resolveNode(sTarget).kpi1.rawValue == null)

I have tried doing this

 if(this.resolveNode(sTarget).kpi"+i+".rawValue == null)

as well as take the kpi out like so

 if(this.resolveNode(sTarget).rawValue == null)

but of course neither of those work as well.  Would i just have to add another var loop?

all of the "kpi" fields are located in each of the pages in the following hiearchy

pmp.Page.object.kpi
radzmar
Level 10
February 8, 2022

You can do this the same way a the average calculations.

var oNodes = pmp.resolveNodes("#subform[*].#subform[*].Table1.Row1.#field.[Exists($.ui.#choiceList) eq 1]");
for (var i = 0; i < oNodes.length; i += 1)  {
	oNodes.item(i).mandatory = pmp.Page1.object1.kpi1.isNull ? "error" : "disabled";
}