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
  • 3958 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";

  }

}

MHWinter
Level 4
February 8, 2022

Not sure it saves you a lot but here is an option. 

1. create a script object to set the rating fields as required or optional based on 3 parameters:

   - the page number

   - the object number

   - the status of KPI (empty or not)

2 include a script in the exit event of each KPI field to test if KPI is empty or not and call for the script object.

 

I tried this and it worked:

Script Object:

pmp.#variables[0].RatingMandatorySetting - (JavaScript, client)
function CheckMandatory(PageNumber,ObjectNumber,Status){
// sets the rating criteria as mandatory or optional
// Call for RatingMandatorySetting.CheckMandatory([PageNumber: number between 1 and 6],[ObjectNumber: number between 1 and 13],[Status: 1: required; 0: optional]);
for (var i = 1; i<=5; i++){
var sTargetRatingBox = "pmp.Page"+PageNumber+".object"+ObjectNumber+".Table1.Row1.ratingbox"+ObjectNumber+"_"+i;
if (Status == "1"){
xfa.resolveNode(sTargetRatingBox).mandatory = "error";
} else {
xfa.resolveNode(sTargetRatingBox).mandatory = "disabled";
}
}
}

 

KPI field script of object 3 on Page2:

pmp.Page2.object3.kpi3::exit - (JavaScript, client)
if ((this.rawValue == null) || (this.rawValue == "")) {
RatingMandatorySetting.CheckMandatory(2,3,0); //CheckMandatory(Page2,object3,optional)
} else {
RatingMandatorySetting.CheckMandatory(2,3,1); //CheckMandatory(Page2,object3,required)
}


Here is the form with the script in KPI 1 thru 3

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