[4.2] - Including a function (with return value) from a host

Posted by IramK on 24-Mar-2017 05:35

Hello,

I have a function that returns a value in a hosted file.

Hosted File:

function test(param1) {

return param1 + "World";

}

Actual Trigger: (this needs a date return value)

{!#HOSTED_FILE.5-1W4gciTTatl-eE4CTNaQ#text} // Hosted file

var returnValue = test("Hello");

var currentDate = new Date();
return currentDate; // Throws an error when I click on "Validate Formula".

currentDate; // This works fine

Can someone explain why is this throwing an error in the first scenario?

Cheers.

Iram

All Replies

Posted by mpiscoso@gmail.com on 24-Mar-2017 07:40

Hello,

In my experience when creating reusable functions with return statements in server-side script, I use String Tokens and have not encountered your issue yet.

I assume it might be some sort of conversion with the hosted file [tag:text] token.

Try converting your function to a String token (it's also easier to manage since these show up in server-side scripts > my primary purpose of using them).

Hope this helps.

Posted by IramK on 24-Mar-2017 07:45

I didn't know you could add JS functions as a String token. However, as suggested by Rollbase, the correct use of Hosted files would include using JS, CSS, HTML etc. So, I'm thinking it'd be better to fix that isn't it if this were a bug?

Cheers.

Iram

Posted by mpiscoso@gmail.com on 24-Mar-2017 07:49

Hi,

I know using hosted files is suggested for the client-side though and not the server-side (previously [tag:text] suffix for hosted files weren't available). This is where I came up with using string tokens for server-side because [tag:text] wasn't available at my time of implementation.

I don't think there's a bug with [tag:text] on the client-side since I've used that as well.

Let's wait for a progress response I guess.

Meantime, I think you'll be better off using string tokens on the server-side.

Cheers

Posted by Thierry Ciot on 24-Mar-2017 15:22

Here is the Progress answer :).  Yes it is possible to use hosted file with trigger.  I just tried it and it works: I have a trigger (type: Update Field Value) that update a field with the square value entered in another field.  

Here is my example:

1) in Hosted file:

function computeSquare (a) {

return  a * a;

}

2) in my trigger:

{!#HOSTED_FILE.KBbXdmuBRZuSyens6NAZDQ#text}

var x = {!Number_Field};

computeSquare(x);

------

That's it.  Thierry.

PS: Notice there is no need for a return statement in the trigger.

Posted by mpiscoso@gmail.com on 24-Mar-2017 19:20

Given Thierry's example, i think i know where your problem lies iramk. You want your reusable function to be cast to a variable but it is being treated as the return statement of the whole trigger (sorry I didnt notice earlier).

To fix this you'll need an encapsulating function, ex.

//hosted file(s) or string token(s) here

function doThings() {

   var x = test("hello"), y = computeSquare(5);

   return x + " " + String(y);

} doThings();

By encapsulating in a function you'll be able to create local variables that gets their values from reusable functions from any source, be it hosted file, string token, or in-trigger fuctions.

The main problem is the unencapsulated script was being treted as the return and the above should solve that.

Hope this helps you further.

Cheers.

Posted by Thierry Ciot on 26-Mar-2017 15:08

Wrapping into a function is not necessary.

The key thing is that the last statement will be used as return value - but as I mentioned above, you do not need to finish your code with a return statement (I know it feels a bit weird when you look at the code) but do not use return statement.

The output of a function call or the evaluation of a variable will be enough.

Another way to put it is write your code as if there was an invisible return statement.

Thierry

Posted by mpiscoso@gmail.com on 26-Mar-2017 20:50

What if you need to use a reusable function (or multiple ones) many times in your script Thierry?

The first time you use it, your script would stop and this isnt always the case in code.

The example is fairly simple so it doesnt justify the need for a function wrapper but in a real-world scenario I dont think that you would use reusable functions just once (without the wrapper).

And as you said, it feels akward not to have the final return statement.

As for me, for safety purposes and ease of code, i would still wrap everything in a function.

To each his own i guess

Posted by Thierry Ciot on 27-Mar-2017 14:04

Yes sure.  I was responding in the context of the initial question from Iram.  So in summary:

1) yes you can use hosted file

2) Iram's issue was the return statement in the last line of code.

All good :)

Thanks for contributing.

This thread is closed