Expand my Community achievements bar.

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
Former Community Member

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
Former Community Member

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

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----