Thursday, March 10, 2022

Important points when working with Javascript funtions in OIC

 We often write simple javascript functions in OIC for transformations such as  formatting a dates, amount etc.

We will  understand what are the consequences of incorrect data type definition while registering the function and we will also see the usage of result from JS functions.

We will take two examples here in this post: 

1).  Using returned value from js as while loop termination condition

2).  Sending a value to js function(example adding days to a given date)


1).  Using returned value  from js as while loop termination condition

We defined a JS function in libraries like below:

This function will take month and year as input and return number of days for that month.

function getNoOfDaysInMonth (month, year) {
var no_of_days =new Date(year, month, 0).getDate();
    return no_of_days;
}


 

 We have defined the returned value(no_of_days) as Number instead of string in  OIC while registering.

 

Now let us see its use in an integration.



We are using the returned value from this function as our while loop upper limit:


Now let us save and run the integration to see how many iterations it gets.

We will pass march as month:

 

To see the return value from js Function we have assigned it to as assign variable and we can see that the fuction has returned the value as 31:

Now let us see how many iterations while loop had, we had directly mapped the response from js as upper limit of loop and it was being compared with a counter variable which is incremented by one.

We can see in the below screenshot that we had only 4 iterations, which is not correct. it should have had 31 iterations.


Also if we observe , we see a logger message for counter after loop and it is 4.

So what went wrong here is , the JS function by default returned a string value  even though we had defined the return no_of_days as number while registering the function in libraries.

One of the solution to this will be to convert this returned value to number when we are comparing in while loop.

Lets try that and see:

 

 

Now lets save and run again to test.


We can see in the above screenshot that it ran perfectly fin this time.

We got the 31 iterations  since the month was march and also the counter value after termination of loop as 31.

 

2).  Sending a value to js function(example adding days to a given date)

We need to be cautious if we are using assign variable to send a n umber value to JS  function.

The assign variable by default sends value as string to JS.

Let us see in detail.

We have defined a function which accepts a date and number of days to be added this date .

The simple function looks like below:

function addDayToGivenDate(input,num) {
var date1=new Date(input);
date1.setDate(date1.getDate() + num);
 var year = date1.getFullYear();
//console.log(year);

var month = String(date1.getMonth() + 1).padStart(2, '0');
//console.log(month);

var day = String(date1.getDate()).padStart(2, '0');
//console.log(day);

var joinedDate = [ month,day, year].join('/');
//console.log(joinedDate);
return joinedDate

}

 

 

We are using counter variable, as number of days to be added, this is a assign variable.

If we observe we can see the type of num ( which is the number of days to be added to date)  is string instead of number.

Lets us see what will be the consequence of this.

Below is the function call from integrations:

We will pass CopyStartDate as 2022-03-01 which is first march and initially the counter variable starts from 0. So our returned date should start from 2022-03-01 to 2022-03-31 since this is inside the while loop which we discussed above and which will have 31 iterations.

 Now lets save and run the integration.


 

We have assigned the result in currdatevar variable.



If we observe we can see that instead of 03/01/2022 it is returning 03/10/2020.

This is because we have not defined the number of days to be added as number while registering the function in library. So it is giving this result.


We can verify this from below screenshots from jsfiddle:


 We had passed 0 as string.

 Now lets pass it as number.

 The result came correctly this time.


Now in OIC we need to change the type to number while registering  the function in libraries.

 

 

In the integration :

Save and run the integration.


We have assigned the result of function to currdatevar variable to verify the value:

We can see now that the  result came as expected:

Note:- When we migrate the integration then this JS function also gets migrated. We need to verify if the  signature got changed.

 In that case we need to update the signature again (for eg from string to double). Otherwise it will throw error saying signature doesn't match the value passed when we try to activate the integration.

No comments:

Post a Comment