How to populate date field variable of catalog item from choice selected from another variable

akshay parekar1
Tera Contributor

Hello Guys, please help me for this

I have created 2 variables for my catalog item , one is of multiple choice type with choices 1 month, 2 months, 6 months and 1 year. The second variable is of date type

Now, if i select any choice out of these 4 mentioned above, my date type variable should populate the date with

"Today's date + choice selected above"

i.e. if i select 1 month then date type variable should populate the date which is today's date + 1 month later

Thanks in advance

 

1 ACCEPTED SOLUTION

Vaibhav Dane
Tera Expert
Tera Expert

Hi Akshay,

 

You can use below client script and Script include to achieve the functionality: 

 

Client Script: 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    alert('running duration change');
    var ga = new GlideAjax('testScript');
      ga.addParam('sysparm_name', 'getDate');
      ga.addParam('sysparm_duration', newValue);
      ga.getXML(updateDate);


     

function updateDate(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
    alert('return date '+answer);
    g_form.setValue('date',answer);
   //Type appropriate comment here, and begin script below
    
}
   
}

 

Script Include: 

var testScript = Class.create();
testScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getDate : function(){
    var duration = this.getParameter('sysparm_duration');
        var currentDate = new GlideDateTime(gs.now());
        currentDate.addMonths(duration);
        return currentDate;
    
},

    type: 'testScript'
});

 

Fields on the form: 

 

Type- Select Box : Question- Duration : Name- duration

Type- Date : Question- Date : Name- date

 

 

Please mark correct if helpful.

 

 

Regards,

Vaibhav Dane

 

 

 

 

View solution in original post

4 REPLIES 4

akshay parekar1
Tera Contributor

Anyone, any idea for this?

Hi you can do like this

Script Include:

Client Script:

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

    var getChoiceValue = g_form.getValue('type'); // choice value field
    alert(getChoiceValue); 

    var ga = new GlideAjax('dateValidation'); // SI name
    ga.addParam('sysparm_name', 'compareDates'); //SI function
    ga.addParam('sysparm_needed_by', getChoiceValue);
    ga.getXML(callBack);

    function callBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("answer" + answer);
        g_form.setValue('Joining_date',answer);
    }

}

compareDates: function() {
        
        var getDateType = this.getParameter('sysparm_needed_by'); // type from date choice
        var start = new GlideDateTime(gs.now());
        if(getDateType == '1 month')
            {
        start.addMonths(1);
        return start;        
            }
        if(getDateType == '6 months')
            {
        start.addMonths(6);
        return start;        
            }
        if(getDateType == '1 year')
            {
        start.addYear(1);
        return start;        
            }

    },

Regards
Harish

Vaibhav Dane
Tera Expert
Tera Expert

Hi Akshay,

 

You can use below client script and Script include to achieve the functionality: 

 

Client Script: 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    alert('running duration change');
    var ga = new GlideAjax('testScript');
      ga.addParam('sysparm_name', 'getDate');
      ga.addParam('sysparm_duration', newValue);
      ga.getXML(updateDate);


     

function updateDate(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
    alert('return date '+answer);
    g_form.setValue('date',answer);
   //Type appropriate comment here, and begin script below
    
}
   
}

 

Script Include: 

var testScript = Class.create();
testScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getDate : function(){
    var duration = this.getParameter('sysparm_duration');
        var currentDate = new GlideDateTime(gs.now());
        currentDate.addMonths(duration);
        return currentDate;
    
},

    type: 'testScript'
});

 

Fields on the form: 

 

Type- Select Box : Question- Duration : Name- duration

Type- Date : Question- Date : Name- date

 

 

Please mark correct if helpful.

 

 

Regards,

Vaibhav Dane

 

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@akshay parekar 

what are the choice values for those choices? is it 1, 2, 6 and 12?

you need not use script include and can directly handle it using client script only

Can you share what script did you start with and where are you stuck?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader