JaredHague

Script by name makes callbacks fun, too bad buttons are left out!

Discussion created by JaredHague on Jun 14, 2018

Why wouldn't the perform script by name not be extended to setting up buttons?

 

I have been doing some fun things with script by name.  One thing I am using it for is to have something like an action sheet.  It presents itself and you pick something and it dismisses. 

 

Before I would just have all the buttons on the layout resume script.  This would work well if you were picking a record because the next step would just be setting the id of the record and then you would close the window and your good.  The problem is when there are other buttons on the layout such as a done button.  Your not picking a record number in these cases.  I would just do a close window and halt script so it wouldn't take the id of the record your sitting on.  I have enjoyed this because it allows the layout to be reused in more scripts because it just resumes or halts.  

 

Now I am finding that isn't always ideal because it halts all scripts. This is problematic for having a modal window open and then having the option for another modal window over that one.  If you try to dismiss the second window using halt script it also quits the other paused script from the first modal window.

 

I know I could just script the buttons but that would not allow a layout to be used for several functions. My first thought was to have the script name I want to run in a variable.  I am not a fan of using global vars but if you pause a script all your current scripts local vars are available to the parameter of any further script. I would have loved to have the buttons perform script by name.  So for now I created a script that looks like this that I tied to my buttons on my modular layout.

 

#

Allow User Abort [ Off ]

Set Error Capture [ On ]

Set Variable [ $j ; Value: Get ( ScriptParameter ) ]

Set Variable [ $script ; Value: JSONGetElement ( $j ; "scriptName" ) ]

Set Variable [ $parameter ; Value: JSONGetElement ( $j ; "parameter" ) ]

#

#

Perform Script [ Specified: By name ; $script ; Parameter: $parameter ]

 

Now when I launch the script to bring the layout up I just supply the callback script name and the parameters.  The great thing is I also just have one script to bring up the layout form anywhere.  In that scripts parameters I also pass the scriptName and parameter that should be used when the user makes a choice on the modular layout.  This allows a modal window to call another and another without interfering with each other.

 

There is one tricky thing to consider.  When you are sending your intended callback parameters you may not always know what the values are going to be before start the original script.  Also you can't use a script to evaluate them because they need to be initialized at the moment when the user clicks the button to start the callback script.  So they need to be calculated in the script parameter calculation box for the above script.

 

Here is an example of the parameter I am using

 

JSONSetElement ( $j ;

   ["tableName" ; "Contact" ; JSONString];

   ["currentGroup" ; Session::g_contactsId ; JSONString];

   ["targetLayout" ; Get ( LayoutName) ; JSONString];

   ["utilityLayout" ; "Raw_Contact" ; JSONString];

   ["callBack" ; "PickGroup(JSON)" ; JSONString];

   ["callBackParameter" ;

      "JSONSetElement ( \"\" ;

           [\"cellType\" ; CustomGroups::cellType  ; JSONString];

           [\"keyField\" ; CustomGroups::keyField  ; JSONString];

           [\"queryVar\" ;  CustomGroups::queryVar  ; JSONString ];

           [\"label\" ;  CustomGroups::label  ; JSONString];

           [\"groupId\" ;  CustomGroups::id  ; JSONString];

           [\"targetLayout\" ;  $targetLayout  ; JSONString];

           [\"utilityLayout\" ;  $utilityLayout  ; JSONString];

           [\"searchFieldName\" ;  GetFieldName ( Session::SEARCH_CONTACTS )  ; JSONString ];

           [\"labelFieldName\" ;  GetFieldName ( Session::g_contactsLabel )  ; JSONString ];

           [\"groupFieldName\" ;   GetFieldName ( Session::g_contactsId ) ; JSONString ]

       );

       ; JSONString ]

)

 

So what is going on here is the script to call the modular page is getting 4 parameters.  It gets the layout open and pauses.  Then any button that is pressed will run the callBack script using the callBackParameter as its parameter.  The reason the call back parameter is all commented out is so its just a big string.  I need it this way to basically freeze it for transport.  All the values are going to vary depending on the button or record that is current when they are evaluated.  The script parameter for the buttons are..

 

JSONSetElement ( "" ;

["scriptName" ; $callBack ; JSONString ];

["parameter" ; Evaluate ( $callBackParameter ) ; JSONObject]

)

 

Some of the callback parameters are repeated from the first script.  Such as targetLayout.  Because its already established from the last script its available as a variable.  Vars that are known just need to have escaped quotes on them.  Everything else is defined when clicked.

 

I would like a better solution for packing the callback so it doesn't need to be one string.  I thought of sending it as JSON where the values would be the calculations.  Then have a recursive function evaluate all the values and return JSON with the same keys and the new values.  It proved to be more simple just to do this but it would be cool.  My thought is to possibly divide the callBackParamter into 3 possible objects.  1st for prepossessing. 2nd for processing in script and the 3rd for processing when clicked.

 

Well I'm just pumped that script by name is here! I have found it useful. I hope you all find this interesting as I did when I was figuring it out.

 

Jared

Outcomes