How to assign a javascript value to an Outsystems widget?
Question

Hi

I have a Javascript code being executed by a "Run Javascript" action. 

Basically, it's a code to do an IRR calculation and in the end, I'm trying to assign the result to an Outsystems Widget named IRR2 (the part of the code where I'm trying to do that, it's in bold).

"function IRR(values, guess) {
  // Credits: algorithm inspired by Apache OpenOffice
  
  // Calculates the resulting amount
  var irrResult = function(values, dates, rate) {
    var r = rate + 1;
    var result = values[0];
    for (var i = 1; i < values.length; i++) {
      result += values[i] / Math.pow(r, (dates[i] - dates[0]) / 365);
    }
    return result;
  }

  // Calculates the first derivation
  var irrResultDeriv = function(values, dates, rate) {.

    var r = rate + 1;
    var result = 0;
    for (var i = 1; i < values.length; i++) {
      var frac = (dates[i] - dates[0]) / 365;
      result -= frac * values[i] / Math.pow(r, frac + 1);
    }
    return result;
  }

  // Initialize dates and check that values contains at least one positive value and one negative value
  var dates = [];
  var positive = false;
  var negative = false;
  for (var i = 0; i < values.length; i++) {
    dates[i] = (i === 0) ? 0 : dates[i - 1] + 365;
    if (values[i] > 0) positive = true;
    if (values[i] < 0) negative = true;
  }
  
  // Return error if values does not contain at least one positive value and one negative value
  if (!positive || !negative) return 0;

  // Initialize guess and resultRate
  var guess = (typeof guess === 'undefined') ? 0.1 : guess;
  var resultRate = guess;
  
  // Set maximum epsilon for end of iteration
  var epsMax = 1e-10;
  
  // Set maximum number of iterations
  var iterMax = 50;

  // Implement Newton's method
  var newRate, epsRate, resultValue;
  var iteration = 0;
  var contLoop = true;
  do {
    resultValue = irrResult(values, dates, resultRate);
    newRate = resultRate - resultValue / irrResultDeriv(values, dates, resultRate);
    epsRate = Math.abs(newRate - resultRate);
    resultRate = newRate;
    contLoop = (epsRate > epsMax) && (Math.abs(resultValue) > epsMax);
  } while(contLoop && (++iteration < iterMax));

  if(contLoop) return 0;

  // Return internal rate of return
  return resultRate;
}

var values = ["+CRFormCapa.Record.CapexFY1+","+CRFormCapa.Record.CashFlowFY1+","+CRFormCapa.Record.CashFlowFY2+","+CRFormCapa.Record.CashFlowFY3+","+CRFormCapa.Record.CashFlowFY4+","+CRFormCapa.Record.CashFlowFY5+","+CRFormCapa.Record.CashFlowFY6+","+CRFormCapa.Record.CashFlowFY7+","+CRFormCapa.Record.CashFlowFY8+","+CRFormCapa.Record.CashFlowFY9+","+CRFormCapa.Record.CashFlowFY10+","+CRFormCapa.Record.CashFlowFY11+","+CRFormCapa.Record.CashFlowFY12+","+CRFormCapa.Record.CashFlowFY13+"];

document.getElementById('"+IRR2.Id+"').value =  IRR(values,"+(TargetIRR/100)+");
"

The function is running ok, because I was putting the result in a js var and using "console.log" and everything worked fine.

But I can't put the result in the Outsystems text box :(

After the Javascript is running, I have a Ajax refrex in the container where is the text box, and that's the text box attributes,


What could be wrong?

Thanks!

mvp_badge
MVP
Solution

Hi Julia,

I tried the same without any much code change and it's working fine for me.

  • You don't need to Ajax refresh the input control
  • Set the button method type to Ajax Submit
  • In my case, instead of defining the local variable type to CRFormCapa structure record type, I defined the local variable as CRFormCapa structure type

see this sample screen JS_IRR_Calculation

Observation:

Observed a small additional dot character after the opening curly braces as highlighted below, I guess this is a typo mistake!

// Calculates the first derivation
  var irrResultDeriv = function(values, dates, rate) {.


Please refer to the attached sample .oml file


Hope this helps you!


Kind regards,

Benjith Sam

TWALabIRR.oml

WOW it was the dot hahaha. Now it's working. Thank u so much for saving my life.

mvp_badge
MVP

You're most welcome, Julia. Glad to help you :)


Kind regards,

Benjith Sam

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.