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

WOAH !  THIS IS STRANGE 

CAN YOU TRY PUTTING ALERT INSTEAD OF ADD INFO ?

tried! alert is showing null.

As Aman mentioned i think we can't pass it to client script.

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

Hi Aman,

Script include function:

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

    },

 

i accessed the "current" in script include as you and Mohith 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);
    }

In client scripts you can't, you will need to pass parameters from your client script to script include, then get that back in your client script:

Few things to keep in mind here, you will need to have Client callable(field on script include as True) script include.

var GetDetails= Class.create();
GetDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getdetails: function() {
        
       var usr = new GlideRecord("sys_user");
       usr.get(this.getParameter("sysparm_caller_id"));
       return usr.getValue("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("GetDetails");
    ga.addParam("sysparm_name","getdetails");
    ga.addParam("sysparm_caller_id", g_form.getValue("caller_id"));
    ga.getXML(res);
    function res(response){
        var answer=response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(answer);
    }

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

 

Best Regards
Aman Kumar