Correct my code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi
I have a requirement where in i have to create a field named compliant in problem task and that field should be read only (this is done) and that should display Yes if the task is closed date is less than due date, and No the other way round.
I tried to achieve this via BR, and i am not getting the proper results.
Can you please correct me on what i did wrong.
Thanks
Code:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @Abdul ,
Check below code
(function executeRule(current, previous) {
current.u_compliant = '';
var closedVal = current.getValue('closed_at');
var dueVal = current.getValue('due_date');
if (closedVal && dueVal) {
var gdtClosed = new GlideDateTime(closedVal);
var gdtDue = new GlideDateTime(dueVal);
var closedTs = gdtClosed.getNumericValue();
var dueTs = gdtDue.getNumericValue();
current.setValue('u_compliant', (closedTs <= dueTs) ? 'Yes' : 'No');
}
})(current, previous);
Please make sure the Business Rule will run for before and Insert & Update checked. Set Condition to trigger when closed_at or due_date changes (or when state becomes Closed).
Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
The value is not getting populated in the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
The problem is most likely in when/how the Business Rule runs rather than the code itself. Try checking business rule table, when condition, insert ans update, trigger condition etc..
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @Abdul ,
try this
(function executeRule(current, previous /*null when async*/ ) {
var closed_date_str = current.getValue('closed_at');
var due_date_str = current.getValue('due_date');
if (closed_date_str && due_date_str) {
GlideDateTime.subtract(current.closed_at.getGlideObject(), current.due_date.getGlideObject()) > 0 ? current.setValue('u_compliant', "Yes") : current.setValue('u_compliant', "No");
}
})(current, previous);
if not working please share more details
is your BR a before update BR? if not make i before update if it's after use current.update() else make it before update
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya