- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 11:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 08:36 AM
Hello ,
use the below sample code:-
SI:-
var SampleScriptInclude = Class.create();
SampleScriptInclude.prototype = {
initialize: function() {
},
// This method can be called via GlideAjax
getGreeting: function(userName) {
// Return a greeting message
return 'Hello, ' + userName + '! Welcome to ServiceNow.';
},
type: 'SampleScriptInclude' // Name of the Script Include
};
CS:-
function onLoad() {
// Instantiate the GlideAjax object
var ga = new GlideAjax('SampleScriptInclude');
// Add a parameter that calls the method in the Script Include
ga.addParam('sysparm_name', 'getGreeting');
// Add any parameters that need to be passed to the Script Include method
ga.addParam('sysparm_userName', g_user.getFullName());
// Make the asynchronous call
ga.getXMLAnswer(function(response) {
// Handle the response from the Script Include
var greeting = response.responseXML.documentElement.getAttribute("answer");
alert(greeting); // Display the greeting message in an alert
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:16 PM
One Very Simple Example I can help you out is with :-
// Script Include: MyGlideAjaxScriptInclude
// Accessible from Client-Side Scripts: Yes
// Client Callable: Yes
var MyGlideAjaxScriptInclude = Class.create();
MyGlideAjaxScriptInclude.prototype = {
initialize: function() {
},
// Method to be called from the client-side
getServerData: function(param1, param2) {
// Example logic
var result = "Hello " + param1 + " and " + param2;
return result;
},
// Required method
type: 'MyGlideAjaxScriptInclude'
};
CS :-
// Client Script: onLoad or onChange, depending on your use case
function onLoad() {
var ga = new GlideAjax('MyGlideAjaxScriptInclude');
// Adding parameters (can be any value)
ga.addParam('sysparm_name', 'getServerData'); // sysparm_name must match the function name in the Script Include
ga.addParam('param1', 'World');
ga.addParam('param2', 'Universe');
// Sending the request and processing the response
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response from server: " + answer); // Display the response
});
}
try it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:19 PM
One more best example :-
Scenario:
When a user selects a department from a dropdown, the manager's name associated with that department should automatically populate in a separate field.
1. Script Include
This Script Include retrieves the manager's name based on the department selected.
// Script Include: GetDepartmentManager
// Accessible from Client-Side Scripts: Yes
// Client Callable: Yes
var GetDepartmentManager = Class.create();
GetDepartmentManager.prototype = {
initialize: function() {
},
getManagerByDepartment: function(departmentSysId) {
var managerName = '';
var deptGR = new GlideRecord('cmn_department');
if (deptGR.get(departmentSysId)) {
var managerSysId = deptGR.manager; // Assuming 'manager' is a reference field to 'sys_user'
if (managerSysId) {
var userGR = new GlideRecord('sys_user');
if (userGR.get(managerSysId)) {
managerName = userGR.getValue('name');
}
}
}
return managerName; // Return manager's name
},
// Required method
type: 'GetDepartmentManager'
};
2. Client Script
This Client Script runs when the department field is changed and auto-populates the manager field.
// Client Script: onChange
// Applies to the Department field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetDepartmentManager');
ga.addParam('sysparm_name', 'getManagerByDepartment'); // Calls the function in the Script Include
ga.addParam('departmentSysId', newValue); // Passes the selected department's sys_id
ga.getXMLAnswer(function(response) {
var managerName = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('manager_field', managerName); // Auto-populates the manager's name field
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 08:28 AM
@SandeepKSingh Here is the sample code for Script Include.
var GetCurrentDateTime = Class.create();
GetCurrentDateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCurrentDateTime: function() {
var currentDateTime = new GlideDateTime(); // Get the current date and time
return currentDateTime.toString(); // Convert it to a string and return
},
type: 'GetCurrentDateTime'
});
Here is the sample code for onLoad script having GlideAjax call to the above script include.
function onLoad() {
// Create a GlideAjax object and pass the Script Include name
var ga = new GlideAjax('GetCurrentDateTime');
// Specify the method name in the Script Include that should be called
ga.addParam('sysparm_name', 'getCurrentDateTime');
// Send the request and process the response with the provided callback function
ga.getXMLAnswer(processResponse);
}
// Callback function to process the response
function processResponse(response) {
var currentDateTime = response; // The response is the current date and time as a string
// Display the current date and time in an alert (or use it as needed)
alert("Current Date and Time: " + currentDateTime);
// Alternatively, set the value in a field
// g_form.setValue('your_field_name', currentDateTime);
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 08:36 AM
Hello ,
use the below sample code:-
SI:-
var SampleScriptInclude = Class.create();
SampleScriptInclude.prototype = {
initialize: function() {
},
// This method can be called via GlideAjax
getGreeting: function(userName) {
// Return a greeting message
return 'Hello, ' + userName + '! Welcome to ServiceNow.';
},
type: 'SampleScriptInclude' // Name of the Script Include
};
CS:-
function onLoad() {
// Instantiate the GlideAjax object
var ga = new GlideAjax('SampleScriptInclude');
// Add a parameter that calls the method in the Script Include
ga.addParam('sysparm_name', 'getGreeting');
// Add any parameters that need to be passed to the Script Include method
ga.addParam('sysparm_userName', g_user.getFullName());
// Make the asynchronous call
ga.getXMLAnswer(function(response) {
// Handle the response from the Script Include
var greeting = response.responseXML.documentElement.getAttribute("answer");
alert(greeting); // Display the greeting message in an alert
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:16 PM
One Very Simple Example I can help you out is with :-
// Script Include: MyGlideAjaxScriptInclude
// Accessible from Client-Side Scripts: Yes
// Client Callable: Yes
var MyGlideAjaxScriptInclude = Class.create();
MyGlideAjaxScriptInclude.prototype = {
initialize: function() {
},
// Method to be called from the client-side
getServerData: function(param1, param2) {
// Example logic
var result = "Hello " + param1 + " and " + param2;
return result;
},
// Required method
type: 'MyGlideAjaxScriptInclude'
};
CS :-
// Client Script: onLoad or onChange, depending on your use case
function onLoad() {
var ga = new GlideAjax('MyGlideAjaxScriptInclude');
// Adding parameters (can be any value)
ga.addParam('sysparm_name', 'getServerData'); // sysparm_name must match the function name in the Script Include
ga.addParam('param1', 'World');
ga.addParam('param2', 'Universe');
// Sending the request and processing the response
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response from server: " + answer); // Display the response
});
}
try it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 11:19 PM
One more best example :-
Scenario:
When a user selects a department from a dropdown, the manager's name associated with that department should automatically populate in a separate field.
1. Script Include
This Script Include retrieves the manager's name based on the department selected.
// Script Include: GetDepartmentManager
// Accessible from Client-Side Scripts: Yes
// Client Callable: Yes
var GetDepartmentManager = Class.create();
GetDepartmentManager.prototype = {
initialize: function() {
},
getManagerByDepartment: function(departmentSysId) {
var managerName = '';
var deptGR = new GlideRecord('cmn_department');
if (deptGR.get(departmentSysId)) {
var managerSysId = deptGR.manager; // Assuming 'manager' is a reference field to 'sys_user'
if (managerSysId) {
var userGR = new GlideRecord('sys_user');
if (userGR.get(managerSysId)) {
managerName = userGR.getValue('name');
}
}
}
return managerName; // Return manager's name
},
// Required method
type: 'GetDepartmentManager'
};
2. Client Script
This Client Script runs when the department field is changed and auto-populates the manager field.
// Client Script: onChange
// Applies to the Department field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetDepartmentManager');
ga.addParam('sysparm_name', 'getManagerByDepartment'); // Calls the function in the Script Include
ga.addParam('departmentSysId', newValue); // Passes the selected department's sys_id
ga.getXMLAnswer(function(response) {
var managerName = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('manager_field', managerName); // Auto-populates the manager's name field
});
}