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 9, 2022

See Designer Scripting Basics guide's chapter on Creating and Reusing JavaScript Functions


thank you, I appreciate it, will definitely review this document

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";
}