Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Getting variables out of a function

Avatar

Level 10

Hi,

In order to reduce the use of the calculation event in a repeating subform, I have set up a function (riskRating) in a script object (calculateRisk), which the exit event of a dropdown (RA_severity) calls.

//This is the Javascript in the exit event...

var vLikelihood = RA_likelihood.rawValue; //dropdown list 1
var vSeverity = RA_severity.rawValue; //dropdown list 2
var vRisk;

calculateRisk.riskRating(vLikelihood, vSeverity);

console.println("Risk after function = " + vRisk);

RA_risk_rating.rawValue = vRisk;  //this line is meant to assign the value of vRisk from the function to another field (but it doesn't)

//This is the function within calculateRisk script object...

function riskRating(vLikelihood, vSeverity)
{  

    var vRisk;

    if (vLikelihood == null && vSeverity == null)
    {
        vRisk = null;
    }

    else
    {
        vRisk = vLikelihood * vSeverity;
    }
   
    console.println("Risk inside function = " + vRisk);
   
    return vRisk;
   
}

I have tried various approaches to get the answer back out of the function and to be used for the remainder of the script in the exit event script.

The console show the correct calculation in the function, but it is not updating the variable outside of the function.

Any ideas?

Thanks,

Niall

1 Accepted Solution

Avatar

Correct answer by
Level 10

Niall,

Take a look at the attached. The form contains four numeric fields: 'a', 'b', 'total', and 'other'. It does a calculate on fields 'a' and 'b'. On the exit event of 'b' it calls 'addNumbers()' in 'MyScriptObject'. The function updates 'total' and returns the value of 'total' to be bound to 'other'.

// form1.page1.subform1.b::exit - (JavaScript, client)

other.rawValue = MyScriptObject.addNumbers(a,b,total);

// form1.#variables[0].MyScriptObject - (JavaScript, client)

function addNumbers(a_,b_,total_) {
    return(total_.rawValue = a_.rawValue + b_.rawValue);
}

Is that what you are looking for?

Steve

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Niall,

Take a look at the attached. The form contains four numeric fields: 'a', 'b', 'total', and 'other'. It does a calculate on fields 'a' and 'b'. On the exit event of 'b' it calls 'addNumbers()' in 'MyScriptObject'. The function updates 'total' and returns the value of 'total' to be bound to 'other'.

// form1.page1.subform1.b::exit - (JavaScript, client)

other.rawValue = MyScriptObject.addNumbers(a,b,total);

// form1.#variables[0].MyScriptObject - (JavaScript, client)

function addNumbers(a_,b_,total_) {
    return(total_.rawValue = a_.rawValue + b_.rawValue);
}

Is that what you are looking for?

Steve

Avatar

Level 10

Thanks Steve,

Worked perfectly!!!

I was making it too complicated:

- dropped the vRisk variable

- followed your script for calling the function on the same line as the ".rawValue ="

- put the return statement together with the calculation in brackets

Got it working as intended.

I have a much better understanding of it now - thanks!

Niall