Servicenow catalog item

Reddy39
Tera Expert

I have one catalog item in that 2 variables are there 'Duration' and 'Renewal Date' if we select the duration dropdown value as "1 Year, 6 Months, 5 Days" the Renewal Date should auto populate the date after  "1 Year, 6 Months, 5 Days"

Can any one help me with this

2 REPLIES 2

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Reddy39 

 

You can use on change client script and script include:

 

Onchange client script On duration field:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    var durationvalue = g_form.getValue('duration');
    alert(durationvalue);
    var Rdate = new GlideAjax('GetManagerDetailsScript'); 
    Rdate.addParam('sysparm_name', 'getRenewalDate'); 
	Rdate.addParam('sysparm_duration',durationvalue);
    
    Rdate.getXMLAnswer(function(response) {
        var answer = response;
        alert(answer);
        g_form.setValue('renewal_date', answer);
    });
}

 

Script Include:

var GetManagerDetailsScript = Class.create();
GetManagerDetailsScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRenewalDate: function() {
        var dvalue = this.getParameter('sysparm_duration');
        var currentdatetime = new GlideDateTime();
        var years = 0;
        var months = 0;
        var days = 0;
        var extractvalues = dvalue.split(',');
        extractvalues.forEach(function(eachextractvalue) {
            if (eachextractvalue.includes('Year')) {
                years = parseInt(eachextractvalue);
            }
            if (eachextractvalue.includes('Month')) {
                months = parseInt(eachextractvalue);
            }
            if (eachextractvalue.includes('Day')) {
                days = parseInt(eachextractvalue);
            }



        });
        currentdatetime.addYears(years);
        currentdatetime.addMonths(months);
        currentdatetime.addDays(days);


        return currentdatetime.getLocalDate();
    },



});

 

 

 

Thanks and Regards

Sai Venkatesh

HIROSHI SATOH
Mega Sage

I hope this helps

 

 

Client Script:
The most effective way to achieve this is by using a client script on the catalog item. Here's a basic outline:

  1. Trigger: The script will be triggered when the 'Duration' field is changed.
  2. Get Values: Get the selected value from the 'Duration' dropdown and the current date.
  3. Calculate New Date: Convert the selected duration into a number of days and add it to the current date.
  4. Set Value: Set the calculated date to the 'Renewal Date' field.

Code Example:

 

 

 

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

    var durationField = g_form.getControl('duration'); // Replace 'duration' with your actual field name
    var renewalDateField = g_form.getControl('renewal_date'); // Replace 'renewal_date' with your actual field name

    // Convert duration to days (you'll need a more robust conversion for complex durations)
    var durationInDays = convertDurationToDays(newValue);

    // Calculate the renewal date
    var today = new Date();
    today.setDate(today.getDate() + durationInDays);

    // Set the renewal date
    renewalDateField.setValue(today);
}