The CreatorCon Call for Content is officially open! Get started here.

Can we use "current" in Script include

Sachin G K1
Kilo Sage

As we know, to access server side value we use make use object "current" in Business Rule. Similarly can we use object "current" in "Script Include". If not what are other ways to access server side value in script include except "GlideRecord".

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

As Mohith suggested above, to gain deeper understanding you can refer to below article:

Ref link:
Can Script Includes Use the "current" Variable?

The correct way to access the “current” object in a Script Include, is to pass it into the function you’re calling (and preferably, to use a variable name within the method other than “current”).

Note: Keep in mind that when handling objects in JavaScript, you’re passing by reference, not by value. This basically means that anything that you do to the object in the Script Include, will also happen to that object in the Business Rule - even if your SI method doesn’t return anything!
Since this is the “short” solution, I’ll spare you the explanation of
pass-by-reference (PBR). If you want to know more about it - and you should - you can read more in my article on the subject, here.

 

Best Regards
Aman Kumar

View solution in original post

13 REPLIES 13

Mohith Devatte
Tera Sage
Tera Sage

Hello @Sachin G K ,

yes you can use current object in a script include 

try calling a script include from a BR and lets say if your script is on incident table 

and you write current.number you will get the incident number 

HERE IS ONE EXAMPLE 

Refer to this below link for detailed explanation 

https://snprotips.com/blog/2019/12/18/can-script-includes-use-the-current-variable

find_real_file.png

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

Hi Mohith,

Script include function:

getdetails: function() {
        
       return gs.addInfoMessage(current.caller_id.email);
        

    },

 

i accessed the "current" in script include as you suggested and it worked. But suppose we want to pass this to client script how to do this?

I tried access it in client script like below but didn't work:

Script Include:

getdetails: function() {
        
       return current.caller_id.email;
        

    }

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
    var ga=new GlideAjax("AbstractAjaxProcessor");
    ga.addParam("sysparm_name","getdetails");
    ga.getXML(res);
    function res(response){
        var answer=response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(answer);
    }
    
   

Hello @Sachin G K YOU CAN TRY THIS 

Make sure our script include is client callable 

and i noticed that there was typo error in "return" keyword so i corrected it .

it should work i guess now give a try with below scripts

script include :

getdetails: function() {
        
       return current.caller_id.email.toString();
        

    }

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
    var ga=new GlideAjax("AbstractAjaxProcessor");
    ga.addParam("sysparm_name","getdetails");
    ga.getXML(res);
}
    function res(response){
        var value=response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(value);
    }

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

Hi Mohith, getting addinfomessage like this:

find_real_file.png

CLIENT SCRIPT:

find_real_file.png

 

BR:

find_real_file.png

 

SCRIPT INCLUDE:

find_real_file.png