PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 04/12/2017 11:18 AM by  TimC
Call javascript function call in Assign node
 9 Replies
Sort:
You are not authorized to post a reply.
Author Messages
TimC
Systems Analyst
Private
Veteran Member
(220 points)
Veteran Member
Posts:84


Send Message:

--
03/24/2017 12:20 PM

    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
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    03/24/2017 1:30 PM
    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
    Lawson Developer
    Private
    Basic Member
    (43 points)
    Basic Member
    Posts:15


    Send Message:

    --
    03/25/2017 1:18 PM

    Hi Tim.

    Try this on the Start Node:

    TimC
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    03/27/2017 9:21 AM
    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
    Private
    Private
    Veteran Member
    (3031 points)
    Veteran Member
    Posts:1021


    Send Message:

    --
    03/27/2017 9:32 AM
    Tim - try datecalc=freq(variable)

    function freq(variable)
    {
    David Williams
    alexrs
    Lawson Developer
    Private
    Basic Member
    (43 points)
    Basic Member
    Posts:15


    Send Message:

    --
    03/27/2017 9:39 AM

    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
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    03/27/2017 10:13 AM
    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
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    03/27/2017 10:42 AM
    getting "undefined"
    TimC
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    03/27/2017 10:50 AM
    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
    Systems Analyst
    Private
    Veteran Member
    (220 points)
    Veteran Member
    Posts:84


    Send Message:

    --
    04/12/2017 11:18 AM
    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.
    You are not authorized to post a reply.