The CreatorCon Call for Content is officially open! Get started here.

Populate Field with time retreived from a schedule entry

ICaTonyBalde
Tera Expert

In a form, a time field "start time", needs to be populated according to the start time from a schedule entry chosen by the user. e.g.: The user selects the "Morning" schedule which has 8 am as the start time and this value is displayed in the "start time" field; If the user chooses a different schedule, "Afternoon" that has 1 pm as the start time, it should change accordingly.

I've created a Client Script and a Script Include but still unable to retrieve the start time value.
Here is the code so far:

Script Include (set as Client callable):

 

 

var GetScheduleStartTime = Class.create();
GetScheduleStartTime.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	initialize: function() {
	},
	getStartTime: function() {
		var scheduleId = this.getParameter('sysparm_schedule');
		var startTime = ''; // Initialize the start time variable
		// Fetch the start time from the selected schedule using GlideRecord
		var scheduleGR = new GlideRecord('cmn_schedule');
		scheduleGR.query();
		if (scheduleGR.get(scheduleId)) {
			startTime = scheduleGR.getValue('start_date_time');
		}
		return startTime;
	},
	type: 'GetScheduleStartTime'
});

 

 


Client Script (onChange):

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	var scheduleField = g_form.getValue('schedule');
	if (!scheduleField || newValue === oldValue) {
		return;
	}
	var ga = new GlideAjax('GetScheduleStartTime');
	ga.addParam('sysparm_name', 'getStartTime');
	ga.addParam('sysparm_schedule', scheduleField);
	ga.getXML(function(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');
		if (answer) {
			g_form.setValue('start_time', answer);
		}
	});
}

 

 



Thanks in advance

1 ACCEPTED SOLUTION

Hi @Samaksh Wani 

Thanks for your help, unfortunately, I won't be able to use :

ga.getXMLWait(); 

since I am working on a scoped application, access is not available.
I would need a different approach

 

View solution in original post

3 REPLIES 3

Samaksh Wani
Giga Sage

Hello @ICaTonyBalde 

 

Make this changes in Client Script

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	var scheduleField = g_form.getValue('schedule');
	if (!scheduleField || newValue === oldValue) {
		return;
	}
	var ga = new GlideAjax('GetScheduleStartTime');
	ga.addParam('sysparm_name', 'getStartTime');
	ga.addParam('sysparm_schedule', scheduleField);
	ga.getXMLWait(); 

  
		var answer = ga.getAnswer();
		if (answer) {
			g_form.setValue('start_time', answer);
		}
}

 

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

Hi @Samaksh Wani 

Thanks for your help, unfortunately, I won't be able to use :

ga.getXMLWait(); 

since I am working on a scoped application, access is not available.
I would need a different approach

 

Hello @ICaTonyBalde 

 

Plz Mark my Solution as Accept, if you got any Help from it.

 

Regards,

Samaksh