How to auto populate date based on choice field

charan_1
Tera Contributor

Scenario is to populate date out based on a choice field and the date out has to get incremented based on choice i select in choice field and the date in value is current date. How to populate date out when i select any choice on a choice field

7 REPLIES 7

newhand
Mega Sage

@charan_1 

[client script - onChange] for the choice list..

Please mark my answer as correct and helpful based on Impact.

Johns Marokky
Tera Guru

Hi @charan_1 ,

I didn't quite understand that incremental part. Can you explain with an example?.

 

Regards,

Johns

Example :date in is current date and assume the choice field has 3 choices it is a,b,c if i select the choice a then date out has to be date in+1, if i select the choice b then it has to be date in+2 if i select c then it has to be date in+3

Hi @charan_1 ,

for this requirement, you need to create a script include and onChange client script.

I had created using the sample data. you can try that with your correct data.

 

Script Include:

Screenshot 2023-02-09 at 2.36.34 PM.png

 

getDateOutField: function() {

        var choiceData = this.getParameter('sysparm_choiceData');
        var dateAdd = 0;
        if (choiceData == 'a') {      //replace the choice with correct values from choice (not label use value of choice)
            dateAdd = 1;
        } else if (choiceData == 'b') {
            dateAdd = 2;
        } else if (choiceData == 'c') {
            dateAdd = 3;
        }

		var currentDateTime = new GlideDateTime(gs.now());
		currentDateTime.addDays(dateAdd);
		var currentDate = currentDateTime.getDate();

		return currentDate;

    },

 

 

Client Script:

Screenshot 2023-02-09 at 2.38.30 PM.png

 

var choiceData = g_form.getValue('u_choice_date');
	var ga = new GlideAjax('PopulateDateOutField');    //script Include name
	ga.addParam('sysparm_name', 'getDateOutField');  //script include function
	ga.addParam('sysparm_choiceData',choiceData);
	ga.getXMLAnswer(getDate);

	function getDate(answer){
		g_form.setValue('u_date_out',answer);   //set the values using the correct fields
	}

 

 

I will attach the results in the attachment. hope you were looking for this solution.

 

Mark helpful and accept the solution if it helps in solving your query.

 

Regards,

Johns