Audit date change on service offering.

Atchutaram
Tera Contributor

Hi team,

we have a requirement when business critiality is very high and high then the next _audit _date should be set to 1 year from the audit date we select and when business criticality is medium, low and very low then next audit date should set to 2 years from audit date. I have written a on change client script on audit change.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || isTemplate) {
return;
}

if (!newValue) {
g_form.clearValue('u_next_audit_due');
return;
}

// Parse the audit date
var auditDate = new Date(newValue);
if (isNaN(auditDate.getTime())) {
g_form.clearValue('u_next_audit_due');
return;
}

// Get business criticality
var criticality = g_form.getValue('business_criticality');
if (!criticality) {
g_form.clearValue('u_next_audit_due');
return;
}

criticality = criticality.trim().toLowerCase();
var yearsToAdd = 0;

if (criticality === 'very high' || criticality === 'high') {
yearsToAdd = 1;
} else if (['medium', 'low', 'very low'].includes(criticality)) {
yearsToAdd = 2;
} else {
g_form.clearValue('u_next_audit_due');
return;
}

// Add years to the audit date
auditDate.setFullYear(auditDate.getFullYear() + yearsToAdd);
var formattedDate = auditDate.toISOString().slice(0, 10); // YYYY-MM-DD
g_form.setValue('u_next_audit_due', formattedDate);
}

 

This is not working, what changes should i make?

 

Best Regards

2 REPLIES 2

Rafael Batistot
Kilo Patron

Hi @Atchutaram 

 

I can see why it might not be working as expected. A few points in your current approach could be causing issues:

 

  1. Client script type – You mentioned it’s an onChange script on audit field. Make sure the audit field’s value is actually passed as newValue. If the field is a date type, sometimes the newValue comes as a string in the format YYYY-MM-DD which is fine, but timezone issues can happen when using new Date(newValue).
  2. Field names – Ensure your field names are correct: business_criticality and u_next_audit_due. Any typo will make g_form.getValue or g_form.setValue fail silently.
  3. Date formatting – toISOString().slice(0,10) gives the date in UTC. ServiceNow stores dates in the system timezone, so sometimes the day might be off by one depending on your timezone. Using g_form.setValue with a Date object works if the field is date only, but you’re converting to string—so it’s okay, just be aware.

Here’s a cleaned-up and working version of your script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || isTemplate) {
return;
}

// If no audit date, clear next audit due
if (!newValue) {
g_form.clearValue('u_next_audit_due');
return;
}

// Parse audit date
var auditDate = new Date(newValue);
if (isNaN(auditDate.getTime())) {
g_form.clearValue('u_next_audit_due');
return;
}

// Get business criticality
var criticality = g_form.getValue('business_criticality');
if (!criticality) {
g_form.clearValue('u_next_audit_due');
return;
}

criticality = criticality.trim().toLowerCase();
var yearsToAdd = 0;

if (criticality === 'very high' || criticality === 'high') {
yearsToAdd = 1;
} else if (['medium', 'low', 'very low'].includes(criticality)) {
yearsToAdd = 2;
} else {
g_form.clearValue('u_next_audit_due');
return;
}

// Add years
auditDate.setFullYear(auditDate.getFullYear() + yearsToAdd);

// Format date as YYYY-MM-DD (works for date fields)
var day = ('0' + auditDate.getDate()).slice(-2);
var month = ('0' + (auditDate.getMonth() + 1)).slice(-2);
var year = auditDate.getFullYear();
var formattedDate = year + '-' + month + '-' + day;

g_form.setValue('u_next_audit_due', formattedDate);
}

 

Important

 

Manual formatting instead of toISOString() to avoid timezone issues.

• Ensured trim().toLowerCase() for criticality check.

• Safer parsing of audit date.

kaushal_snow
Mega Sage

Hi @Atchutaram ,

 

Use GlideDateTime for Accurate Date Manipulation: Instead of relying on JavaScript's Date object, utilize ServiceNow's GlideDateTime to handle date calculations....Ensure the date is formatted in the yyyy-MM-dd format before setting it on the form....

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || !newValue) {
        return;
    }

    // Parse the audit date
    var auditDate = new GlideDateTime(newValue);
    if (!auditDate.isValid()) {
        g_form.clearValue('u_next_audit_due');
        return;
    }

    // Get business criticality
    var criticality = g_form.getValue('business_criticality');
    if (!criticality) {
        g_form.clearValue('u_next_audit_due');
        return;
    }

    criticality = criticality.trim().toLowerCase();
    var yearsToAdd = 0;

    if (['very high', 'high'].includes(criticality)) {
        yearsToAdd = 1;
    } else if (['medium', 'low', 'very low'].includes(criticality)) {
        yearsToAdd = 2;
    } else {
        g_form.clearValue('u_next_audit_due');
        return;
    }

    // Add years to the audit date
    auditDate.addYears(yearsToAdd);

    // Format the date as yyyy-MM-dd
    var formattedDate = auditDate.getDate().toISOString().split('T')[0];
    g_form.setValue('u_next_audit_due', formattedDate);
}

 

 

Implementing these changes should resolve the issues with setting the u_next_audit_due field based on the selected audit_date and business_criticality. If the problem still there, consider reviewing the field types and ensuring they are appropriately configured to handle date values....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/