Call javascript function call in Assign node

 9 Replies
 3 Subscribed to this topic
 52 Subscribed to this forum
Sort:
Author
Messages
TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member

1. I initialize a variable in start node:

     calcDates (string) = ""

2. Then init the variable in an assign node (script):

            calcDates=function(freq){

                   doDateLogicHere freq

           }    

3. need to call the function from another line in the init (assign) assign node.

      calcDates('Tuesday')

     Trouble is, I get an eval error:

     "Error evaluating expression: calcDates(frequency)"

 

Error evaluating expression: calcDates(frequency)

 

 

TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
Ok... So, here's the actual calcDates function that returns json to a parent flow.
This compiles fine by the interpreter in an Assign script node. Its used to calculate the next run date after the current job was executed. This gets run in a scheduled flow.
calcDates = function( freq ){

freq = freq.toString().toUpperCase();
switch(freq)
{
case "TUESDAY":
paymentDateDayOfWeek=2;
paymentThroughDateDayOfWeek=3;
break;
case "THURSDAY":
paymentDateDayOfWeek=4;
paymentThroughDateDayOfWeek=5;
break;
case "ALL":
dayofWeek = todayDate.getDay();
if(todayDateDay ==5){
//if today is Friday, set to following monday;
paymentDateDayOfWeek=AddDay(todayDate,3).getDay();
paymentThroughDateDayOfWeek=AddDay(todayDate,4).getDay();
} else {
paymentDateDayOfWeek=AddDay(todayDate,1).getDay();
paymentThroughDateDayOfWeek=AddDay(todayDate,2).getDay();
}
break;
default:
dayofWeek = todayDate.getDay();
if(todayDateDay ==5){
//if today is Friday, set to following monday;
paymentDateDayOfWeek=AddDay(todayDate,3).getDay();
paymentThroughDateDayOfWeek=AddDay(todayDate,4).getDay();
} else {
paymentDateDayOfWeek=AddDay(todayDate,1).getDay();
paymentThroughDateDayOfWeek=AddDay(todayDate,2).getDay();
}
break;
}
for(i=1;i<7;i++)
{
tempDate = AddDay(todayDate,i);
dayofWeek = tempDate.getDay();
if(dayofWeek==paymentDateDayOfWeek)
{
submittedDate = getDateDME(tempDate);
paymentDate = DateString(tempDate,"yyyymmdd");
payThroughDate = DateString(( AddDay(tempDate,1)),'yyyymmdd');
break;
}
}

var payDates = {"paymentDate":paymentDate,"payThroughDate":payThroughDate};
return payDates;
}

The next statement in the Assign node is payDates = calcDates(frequency), which receives the error...
Stumped...

2nd part of this is I want to read the JSON struct in the parent flow. So, I suspect the Assign node from the Trigger node would be:
parentPayDates = CalcDatesTrigger_outputData.
myPayDate = parentPayDates.paymentDate;
mypayThroughDate = parentPayDates.payThroughDate.

Then the next Lawson query node would update the multi-step job.
alexrs
Basic Member Send Private Message
Posts: 16
Basic Member

Hi Tim.

Try this on the Start Node:

TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
Hmm...
XML object???
Still getting the error calling the function.
Activity init: Error evaluating expression: calcDates(frequency);
The calcDates function compiled. I debugged it in Visual Studio and it works there.
David Williams
Veteran Member Send Private Message
Posts: 1127
Veteran Member
Tim - try datecalc=freq(variable)

function freq(variable)
{
David Williams
alexrs
Basic Member Send Private Message
Posts: 16
Basic Member

Trust me, it works. 

Step one- Create the variable as xml and  declare the function as you would declare any function in javascript

Step two- Call the function anywhere in the flow as you would call any function, say calcDate('Tuesday'). That's it.


TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
I got it to work. Didn't need the XML object var.
Start Node:
calcDates (string) = ""

declared a variable in a script: (Assign Node):
calcDates = function(frequency)
{
//logic to init paymentDate, payThroughDate.....
var pDates = {"paymentDate":paymentDate,"payThroughDate":payThroughDate};
return pDates;
}

OK. Trying to have the function return a JSON object. Got that [object] returned to the var. So, that looks right?

Assign node:
payDates = calcDates(frequency) evals to:
payDates = [object] //So, that looks right.
Now, trying to read paymentDate = payDates.paymentDate



TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
getting "undefined"
TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
Tried it with either JSON or a function object to no avail.
paymentDate on the read from the call returning "undefined"

var pDates = function(){}
pDates.paymentDate = paymentDate
pDates.payThroughDate = payThroughDate;
//var pDates = {"paymentDate":paymentDate,"payThroughDate":payThroughDate}; JSON object
return pDates

Call:
payDates = eval(pDates)
paymentDate = payDates.paymentDate (returns "underfined")
TimC
Veteran Member Send Private Message
Posts: 84
Veteran Member
Ok. I threw in the towel of having the subflow return back a JSON object vs. a string. It seems you can do it between an Assign node to read the value back from a function from within the same flow. However, it doesn't seem like it can be sent out to a calling flow. So, I modified the logic to return a string "paymentDate:20170101|payThroughDate:20170102". Then parse it in the outer flow.