- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 05:25 AM
In Incident form there is related list name-problem if the related list is empty I can not change the state of incident form new to inprogrees. if it is not empty then I can move the state. how I can achieve this..
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 07:25 AM
Try below onChange Client script and script include -
onChange client script -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
if (newValue == 2) {
// Call the Script Include
var ga = new GlideAjax('CheckRelatedProblems');
ga.addParam('sysparm_name', 'hasRelatedProblems');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
if (response == "false") {
g_form.addErrorMessage("Cannot change the state to In Progress because there are no related Problems.");
g_form.setValue('state', oldValue); // Revert the state to its previous value
}
});
}
}
Script Include -
var CheckRelatedProblems = Class.create();
CheckRelatedProblems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRelatedProblems: function() { // Changed method name to match sysparm_name
var incidentId = this.getParameter('sysparm_sys_id');
var problemGR = new GlideRecord('problem');
problemGR.addQuery('parent', incidentId); // Adjusted field if needed
problemGR.query();
// Return result as "true" or "false" as a string
var result = problemGR.hasNext() ? "true" : "false";
return result;
},
type: 'CheckRelatedProblems'
});
Output -
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 07:00 AM
Hi @keshav77,
Definitely the Problem table has a reference field referencing the incident record.
Write a BR on Incident and do gliderecord query on Problem table using the current.getUniqueValue(). If you find no records from the gliderecord query that means the related list is empty and abort the action of update using current.setAbortAction(true).
If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.
You may mark this helpful as well if it helps you.
Thanks,
Animesh Das
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 07:20 AM
Hi @keshav77 ,
You can write before BR.
Code:
var gr = new GlideRecord('problem');
gr.addQuery('parent', current.sys_id);
gr.query();
if (!gr.next()) {
gs.addErrorMessage('Please attache a problem to move state in-progress');
current.setAbortAction(true);
}
Output:
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 07:25 AM
Try below onChange Client script and script include -
onChange client script -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
if (newValue == 2) {
// Call the Script Include
var ga = new GlideAjax('CheckRelatedProblems');
ga.addParam('sysparm_name', 'hasRelatedProblems');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
if (response == "false") {
g_form.addErrorMessage("Cannot change the state to In Progress because there are no related Problems.");
g_form.setValue('state', oldValue); // Revert the state to its previous value
}
});
}
}
Script Include -
var CheckRelatedProblems = Class.create();
CheckRelatedProblems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRelatedProblems: function() { // Changed method name to match sysparm_name
var incidentId = this.getParameter('sysparm_sys_id');
var problemGR = new GlideRecord('problem');
problemGR.addQuery('parent', incidentId); // Adjusted field if needed
problemGR.query();
// Return result as "true" or "false" as a string
var result = problemGR.hasNext() ? "true" : "false";
return result;
},
type: 'CheckRelatedProblems'
});
Output -
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 03:36 AM
this is not working for me. I am using in the same as you shared but it is not working some how