Auto Populating a field based on user's closed incidents
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2023 09:48 AM
Hello All,
I have a requirement below:
- Notes field should only be displayed when Sentiment is Unhappy and should be mandatory.
- This field should be auto populated with the following text if User has closed incidents:
- {Last closed Incident Number – Last closed Incident hyperlink} was the latest closed incident for this user.
- This field should be auto populated with the following text if User has no closed incidents:
- This user has no closed incidents.
- User can add to above texts once field is populated.
- This field should be auto populated with the following text if User has closed incidents:
Though i am done with displaying notes field when Sentient is unhappy but I am really really really stuck on the later part.
After we visit any user record servicenow should look for that particular user incident records
Any solution please ? What to use how to use ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2023 01:13 AM
So the user selects unhappy and it should load on client the Notes field as mandatory and populate information on it?
You'll need to create a GlideAjax script include and a onChange client script.
Shawn has made a great example of how to use GlideAjax and client scripts together, so check his post :
GlideAjax Example Cheat Sheet - ServiceNow Community
In your client script you'll be calling the script include and a function in it.
var closedInc = new GlideRecord('incident');
closedInc.addQuery("closed_by=gs.getUserID()");
closedInc.orderByDesc("closed_at");
closedInc.query();
if(closedInc.next()){
return "X";
}else{
return "Y";
}
In your client script you'll then parse the answer and show it on the text field.
For example the function in script include could automatically generate the hyperlink and text and you'd just need to use g_form.setValue("your_field", answer) to set it there. This of course depends a lot on how you build the return response and the client side parsing.
Also, do note that you're currently using a string field as the text field variable. If you want to enable hyperlinks, then you need to change the field to an HTML field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2023 02:13 AM
Hello @spimple ,
you can achive this using client script and Script include but i dont think in Text field you can store hyperlink, either create new field of URL type and store URL there.
Client Script : onChange Sentiment
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('Dateutils'); // Client Callable Script Inlcude Name
ga.addParam('sysparm_name', 'getINC'); // Script include Function name
ga.addParam('start_value', newValue); // Passing Start Date
ga.getXML(getResponse);
}
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Answer" +answer);
g_form.setValue('close_notes', answer);
}
Script Include : (Client callable true)
var Dateutils = Class.create();
Dateutils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getINC: function() {
gs.info("Chetan-8 inside Script Include");
// Get the Start Date
var value = this.getParameter('start_value');
var userID = gs.getUserID();
var notesText;
var instanceUrl = gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=";
var incidentURL;
var incGR = new GlideRecord('incident');
incGR.addQuery('caller_id', userID); // Assuming the User field on the Incident table is 'caller_id'
incGR.addQuery('active', false);
incGR.orderByDesc('sys_created_on');
incGR.setLimit(1);
incGR.query();
if (incGR.next()) {
incidentURL = (instanceUrl + incGR.getLink(true));
// If the user has closed incidents, populate the Notes field with the latest closed incident information
notesText = incGR.number + ' - ' + incidentURL + ' was the latest closed incident for this user.';
} else {
// If the user has no closed incidents, populate the Notes field accordingly
notesText = 'This user has no closed incidents.';
}
return notesText;
},
type: 'Dateutils'
});
Result :
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 01:16 AM
How r u getting the closed incidents for a specific user ?
I am trying to get the closed incidents for a particular user after clicking on choice: Unhappy it should check if that user record has any closed incidents
I am not able to get the logic actually i tried querying but it did not work:
This was the Script Include: