Populate Template values on change record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:04 AM
Hi,
Any chance when changing template can erase the old template value and update the new template values and if template b is not having short description value then change form should erase the old value and keep it blank .
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:31 AM
Hi @Nandini DM
yes you can, create OnChange client script and applied when template is changed. This script will check if the field values in the new template are blank and clear the existing values if they are.
Table: Incident/task (where you are applying the template)
Field : template (name of the template field)
(function executeRule(current, previous /*null when async*/) {
// Assuming the template field name is 'template'
var templateSysID = g_form.getValue('template');
if (templateSysID) {
// Load the template record to access its fields
var templateGR = new GlideRecord('sys_template');
if (templateGR.get(templateSysID)) {
// Check if the new template has a blank short description
if (!templateGR.short_description) {
// Clear the current short description if the template's short description is blank
g_form.setValue('short_description', '');
}
// Repeat for other fields you want to manage
}
}
})(current, previous);
i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:47 AM
Hi @Rajesh Chopade1
Thank you for the response , GlideRecord usage is not a good practice to use on client script I believe.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:55 AM
and the above code is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 05:09 AM - edited 09-03-2024 05:10 AM
Hi @Nandini DM ,
This can be achieved using a onchange client script and in that you need to use GlideAjax to call a script include.
Below is the sample code to achieve the same-
Client Script-
(function executeRule(current, previous /*null when async*/) {
var templateSysID = g_form.getValue('template');
// If the template field is not empty, proceed
if (templateSysID) {
// Fetch the template fields via the GlideAjax method to execute on the server-side
var ga = new GlideAjax('GetTemplateFields');
ga.addParam('sys_id', templateSysID);
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
// Iterate over the fields and clear them if necessary
for (var field in result) {
if (result.hasOwnProperty(field)) {
var value = result[field];
if (value === '') {
g_form.setValue(field, '');
}
}
}
});
}
})(current, previous);
Server side script include- (to fetch the template fields)
var GetTemplateFields = Class.create();
GetTemplateFields.prototype = {
initialize: function() {},
getFields: function(templateSysID) {
var result = {};
var templateGR = new GlideRecord('sys_template');
if (templateGR.get(templateSysID)) {
// Loop through the template fields and collect them
var fields = templateGR.field_values.split(',');
fields.forEach(function(field) {
result[field] = templateGR.getValue(field) || '';
});
}
return JSON.stringify(result);
},
// This is required to expose the method to client-side calls
getXMLAnswer: function() {
return this.getFields(this.getParameter('sys_id'));
}
};
Note- Script include should be client callable- ensure to check the box