- 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-23-2024 07:17 AM
Hey @now developer ,
Can you please share screenshots of the BR and Client Script? So I can help further. The above shared script was working fine on my PDI.
Thanks,
Rishi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 07:40 AM
I suspect the issue could be that this field is not added to the form. Can you please confirm that you have added 'u_reason_for_sla_breach' on the form? If not please add it to the form using form designer or form layout. Later please utilize the onLoad client script below along with the previously mentioned Display BR
Client Script: This will make 'u_reason_for_sla_breach' visible and mandatory if any related SLA has breached otherwise it will hide it.
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.has_breached == true){
g_form.setDisplay('u_reason_for_sla_breach', true);
g_form.setMandatory('u_reason_for_sla_breach', true);
}
else{
g_form.setDisplay('u_reason_for_sla_breacht', false);
g_form.setMandatory('u_reason_for_sla_breach', false);
}
}
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-23-2024 07:58 AM
Hello Rishi,
Please find the screenshots below:
I provided all the screenshots, please let me know if I am going wrong somewhere.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 08:00 AM
Custom field is visible always on the form. Can we write a UI policy to hide it initially and then the scripts run?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 12:52 PM
I don't think that will work here because Client Scripts runs before UI policy. So, whatever we do in UI policy (which is going to run later) will only reflect on the form.
If the field is on the form the above-mentioned BR and Client script should work. Or there might be a UI policy which might be affecting this, can you please check that once? You can check this by going to sys_ui_policy_action.LIST and apply filters like table=incident and field= u_reason_for_sla_breach.
If none of these works, please share the BR and client script you have written, so I can help further.
Regards,
Rishi.