Need advice on editing this script

Hi all,

I have an adobe form which has a fair bit of javascript coding behind it. Basically based on how various fields in the form are answered it will determine an Ultimate Approver calculation.

What we are trying to do?
From the existing code below, we need to add a conditional logic whereby if there is a change to both Date (changeInContractDuration) and Value (changeInSpend) , the Ultimate Approval should = changeInSpendTier.

The code is below. Section D is what we’ve just created to try and get this result but it is not working so something must be logically wrong.

I’m hoping someone can read this script and simply advise what is wrong with Section D coding or the logic behind the coding to get this result.

Really appreciate any feedback.

* Determines the ultimate approver for amendment agreement types
function calculateUltimateApproverForAmendment() {
	var result = "";
	var isPSDDRequired = true;
	var changeInContractDuration = parseInt(PSDD.formExecutiveSummary.subformAmendment.tfContractDurationChange.rawValue);
	var changeInSpend = parseFloat(PSDD.formExecutiveSummary.subformAmendment.tfChangeInSpend.rawValue);
	var changeInSpendTier = getApproverTier(changeInSpend);
	var highestApproverToDateIndex = PSDD.formExecutiveSummary.subformAmendment.ddHighestApproverToDate.selectedIndex;
	var newCumulativeValueTier = getApproverTier(parseFloat(PSDD.formExecutiveSummary.subformAmendment.tfCumulativeValueNew.rawValue));
	if(isContactCentreBackOfHouseProject()) {

		// A. If Contact Centre Back of Hose is checked, use that approval flow.
		result = jsData.APPROVERS_CC[jsData.APPROVERS_CC.length - 1];
	} else if(PSDD.formExecutiveSummary.subformAmendment.ddMaterialChangeContract.rawValue == 1 && changeInSpend == 0) {
		// B. If material change is yes and change in spend and == 0, UltApprover is from 0-2 tier.
		result = jsData.ULTIMATE_APPROVERS[0];
	} else if(PSDD.formExecutiveSummary.ddCOGSDVC.rawValue == 1 && PSDD.formExecutiveSummary.ddAgreementType.rawValue == 2) {
		// C. If COGS == yes, is an amendment form, and no change in contract duration, UltApprover is "No PSDD required".
		if(changeInContractDuration == 0) {
			result = "No PSDD required";
			isPSDDRequired = false;
		} else {
			result = jsData.ULTIMATE_APPROVERS[0];
	} else {
		// D. Else, Check that the date and value field is changing
		if (changeInContractDuration == 1) {
		isPSDDRequired = true;
			// If the contract date and value is changing, use the change in spend.
			result = jsData.ULTIMATE_APPROVERS[changeInSpendTier];
	} else {
			// If Date is not changing, use the new cumulative value.
			result = jsData.ULTIMATE_APPROVERS[newCumulativeValueTier];

		// E. Else, Compare the highest approver to date and new cumulative value fields.
		if(highestApproverToDateIndex >= newCumulativeValueTier && highestApproverToDateIndex != 8) {
			// If the contract is staying in the same tier, or was approved in a higher tier, use the change in spend.
			result = jsData.ULTIMATE_APPROVERS[changeInSpendTier];
		} else {
			// Highest approver tier is less than the new cumulative value tier, use the new cumulative value.
			result = jsData.ULTIMATE_APPROVERS[newCumulativeValueTier];

Let’s start with a test.

Your needs are:
"if there is a change to both Date (changeInContractDuration) and Value (changeInSpend) , the Approval should = changeInSpendTier."

The date is stored at PSDD.formExecutiveSummary.subformAmendment.tfContractDurationChange.rawValue
The value is stored at PSDD.formExecutiveSummary.subformAmendment.tfChangeInSpend.rawValue
The approval should be getApproverTier(changeInSpend)

You haven’t shown it in your code, but I presume that the function returns the result value.

A suitable test could be:

function setChangeInContractDuration(value) {
  PSDD.formExecutiveSummary.subformAmendment.tfContractDurationChange.rawValue = value;
function setChangeInSpend(value) {
  PSDD.formExecutiveSummary.subformAmendment.tfChangeInSpend.rawValue = value;
const expected = getApproverTier(changeInSpend);
const actual = calculateUltimateApproverForAmendment();
console.assert(actual === expected, "Expected", expected, "but got", actual);

That sort of test helps us ensure that the code continues to work as expected when other changes occur, and makes it much easier for us to know when our changes have a beneficial effect.

From there, I would do a simple if statement before the first one in the function, and return the appropriate value. When the tests successfully passes (along with hopefully other tests in place too) that’s when we would refactor the code so that improvements are made to the design of the code without breaking any of the tests.

Thanks for your reply. Your understanding of the code sounds correct.

I’m really looking for advice though on how I could possibly change section D in the above code. Something is wrong here and not producing the result I’m after.

Essentially I want it to be that if there is a an input into the ChangeInSpend field and a change to the changeInContractDuration field, the result in Ultimate Approver value should reference the Change in Spend Tier.

What I have in section D in the above code is not working, Any ideas? The below is another logic I was thinking of using but it doesn’t seem to work either

		// D. If change in spend and change in contract duration, use change in spend
          if (changeInContractDuration == 1 || changeInSpend == 1) {
          } else {
          		result = jsData.ULTIMATE_APPROVERS[changeInSpendTier];

I should warn you that making changes to complex code, when you have no reliable way to ensure that the code still works exactly as was originally desired, is a very bad idea.

First use tests that properly exercise the code, so that you can use those tests when making further changes to ensure that things keep working as needed.

Edit: Right now it is fear of breaking something that’s holding you back. Tests remove that fear.

1 Like