script

Karthik_Yallam1
Tera Contributor

How many ways we will call the Script include to the client script

1 ACCEPTED SOLUTION

Aditya02
Tera Guru

Hi @Karthik_Yallam1 ,


There are actually two ways to achieve this:

 

1. Using GlideAjax: This is the most common approach.

  • You create a GlideAjax object, specify the script include name, and call a function on the script include.
  • The script include returns a result, which you can then process in the client script.

2. Using the g_form object:

  • If the script include is specifically designed to work with forms, you can directly call its functions using the g_form object.
  • This method is more efficient and can be used for simple interactions with form-related script includes.

 

Here's an example of each method:

Using GlideAjax:

var ga = new GlideAjax('MyScriptIncludeName');
ga.addParam('param1', 'value1');
ga.addParam('param2', 'value2');
ga.get();
ga.addCallback(function() {
var result = ga.getAnswer();
// Process the result
});

Using the g_form object:

var result = g_form.callServer('MyScriptIncludeName', 'myFunction', 'param1=value1&param2=value2');
// Process the result

 

Choosing the right method:

  • GlideAjax: Use this method for general-purpose script include calls and when you need to pass parameters or handle asynchronous responses.
  • g_form object: Use this method for script includes that are specifically designed to work with forms and when you need to call functions directly from the client script.

 

===================================***************=========================================

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Aditya

=====================================***********==========================================

 

 

View solution in original post

7 REPLIES 7

debendudas
Mega Sage

There is only one way that you can call script include in client script by using GlideAjax.

Please refer the below post: In how many methods we can call script include in ... - ServiceNow Community

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍

Aditya02
Tera Guru

Hi @Karthik_Yallam1 ,


There are actually two ways to achieve this:

 

1. Using GlideAjax: This is the most common approach.

  • You create a GlideAjax object, specify the script include name, and call a function on the script include.
  • The script include returns a result, which you can then process in the client script.

2. Using the g_form object:

  • If the script include is specifically designed to work with forms, you can directly call its functions using the g_form object.
  • This method is more efficient and can be used for simple interactions with form-related script includes.

 

Here's an example of each method:

Using GlideAjax:

var ga = new GlideAjax('MyScriptIncludeName');
ga.addParam('param1', 'value1');
ga.addParam('param2', 'value2');
ga.get();
ga.addCallback(function() {
var result = ga.getAnswer();
// Process the result
});

Using the g_form object:

var result = g_form.callServer('MyScriptIncludeName', 'myFunction', 'param1=value1&param2=value2');
// Process the result

 

Choosing the right method:

  • GlideAjax: Use this method for general-purpose script include calls and when you need to pass parameters or handle asynchronous responses.
  • g_form object: Use this method for script includes that are specifically designed to work with forms and when you need to call functions directly from the client script.

 

===================================***************=========================================

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Aditya

=====================================***********==========================================

 

 

Amit Verma
Kilo Patron
Kilo Patron

Hi @Karthik_Yallam1 

 

From a client script, the recommended way is to make use of GlideAjax. You can refer below posts to check how you can use it :

https://www.servicenow.com/community/developer-articles/how-to-call-script-include-from-client-scrip...

https://www.servicenow.com/community/developer-forum/calling-a-script-include-from-a-client-script/m...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Sandeep Rajput
Tera Patron
Tera Patron

@Karthik_Yallam1 The most comman way to call a script include from client script is using GlideAjax. For more information on GlideAjax please refer to https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideAjaxAPI