- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 01:02 PM
Hello all,
I have added a field on incident form to make the field visible and mandatory when SLA is breached.Trying to achieve this with Business rules providing some triggers , and also through onChange or onLoad client script, unable to achieve it. Can someone help me on this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 03:43 PM
Hi @now developer,
I see that you are trying .setDisplay() and .setMandatory() in your BR, unfortunately these don't work on server side.
Here you need to pass data from server to client side. First on server side you'll need to check if any sla associated with INC has breached and then you'll need to pass that data to client script to make the field visible and mandatory. There are a couple of ways you can achieve this; script include and client script or display business rule and client script.
I would recommend using a Display BR and onLoad client script in your case. You can also try script include if you want.
Display BR on Incident table:
This script checks related SLAs for the INC and if any of them has breached it populates true in g_scratchpad.has_breached variable.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var sla = new GlideRecord("task_sla");
sla.addQuery('task', current.sys_id);
sla.addQuery('has_breached', true);
sla.query();
if(sla.next()){
g_scratchpad.has_breached = true;
}
})(current, previous);
OnLoad Client Script: Here we check the scratchpad variable and make our field visible and mandatory.
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.has_breached == true){
g_form.setVisible('u_reason_for_sla_breach', true);
g_form.setMandatory('u_reason_for_sla_breach', true);
}
}
I am assuming this custom field is not visible by default, so I am just making it visible and mandatory. Feel free to modify according to your requirements.
Please mark this response helpful or correct, if it helped you.
Regards,
Rishi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 03:42 PM
Hi @now developer ,
Via BR its not possible bcoz BRs are server side scripts and wont manipulate the client-side behavior of form fields such as visibility or mandatory status without a page reload,
You should use Client Scripts to achieve this, which run on the client side and can interact with the form as it's being used.
1. onLoad Client Script
This script will check the status of the SLA when the incident form is loaded and adjust the field properties accordingly.
function onLoad() {
// Function to check SLA status and adjust field properties
function adjustFieldProperties() {
var slaStatus = g_form.getValue('stage'); // Assuming 'stage' holds the SLA status
if (slaStatus === 'breached') {
g_form.setDisplay('u_reason_for_sla_breach', true); // Make field visible
g_form.setMandatory('u_reason_for_sla_breach', true); // Make field mandatory
} else {
g_form.setDisplay('u_reason_for_sla_breach', false); // Hide the field
g_form.setMandatory('u_reason_for_sla_breach', false); // Field is not mandatory
}
}
// Call the function to adjust fields as per SLA status
adjustFieldProperties();
}
2. onChange Client Script
This script will be triggered when the SLA status changes on the form, enabling real-time updates without reloading the form.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (control == 'stage') { // Assuming 'stage' is the field that reflects SLA status
if (newValue === 'breached') {
g_form.setDisplay('u_reason_for_sla_breach', true); // Make field visible
g_form.setMandatory('u_reason_for_sla_breach', true); // Make field mandatory
} else {
g_form.setDisplay('u_reason_for_sla_breach', false); // Hide the field
g_form.setMandatory('u_reason_for_sla_breach', false); // Field is not mandatory
}
}
}
This client-side scripting approach ensures that the field visibility and mandatory status are dynamically updated on the incident form based on the SLA status.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 04:11 PM
Thank you for prompt response. But onChange which field I should give? I tried giving State field changes but didn't work. I am still trying to fix, but not working.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 03:43 PM
Hi @now developer,
I see that you are trying .setDisplay() and .setMandatory() in your BR, unfortunately these don't work on server side.
Here you need to pass data from server to client side. First on server side you'll need to check if any sla associated with INC has breached and then you'll need to pass that data to client script to make the field visible and mandatory. There are a couple of ways you can achieve this; script include and client script or display business rule and client script.
I would recommend using a Display BR and onLoad client script in your case. You can also try script include if you want.
Display BR on Incident table:
This script checks related SLAs for the INC and if any of them has breached it populates true in g_scratchpad.has_breached variable.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var sla = new GlideRecord("task_sla");
sla.addQuery('task', current.sys_id);
sla.addQuery('has_breached', true);
sla.query();
if(sla.next()){
g_scratchpad.has_breached = true;
}
})(current, previous);
OnLoad Client Script: Here we check the scratchpad variable and make our field visible and mandatory.
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.has_breached == true){
g_form.setVisible('u_reason_for_sla_breach', true);
g_form.setMandatory('u_reason_for_sla_breach', true);
}
}
I am assuming this custom field is not visible by default, so I am just making it visible and mandatory. Feel free to modify according to your requirements.
Please mark this response helpful or correct, if it helped you.
Regards,
Rishi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 04:14 PM
No luck!
I am writing the BR on incident table and onLoad also on incident table. But still it is not mandatory even though I have tickets breached and when trying to close, its not mandate.