Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Date & time value will automatically set based on in one variable based on other variable

SrinivasuSagi
Tera Contributor

Hi Community,

 

Requirement: 

 

Once we select a value for "When is this needed" (Date & Time variable), the "When can the snapshot be deleted" (Date & Time variable) will automatically be set to 72 hours after the selected date and time

 

 

SrinivasuSagi_1-1733812343840.png

For example, if we select 10/12/2024 at 1 PM in "When is this needed," the "When can the snapshot be deleted?" will automatically be set to 13/12/2024 at 1 PM

 

Please advise how can we achieve this?

 

 

Thanks & Regards,

Srinivasu Sagiraju

 

3 REPLIES 3

AshishKM
Kilo Patron

Hi @SrinivasuSagi 

Write onChange catalog client script and set the 72 hour after date & time to second variable.

 

you can replace the firstDate and secondDate as per varibale name in the catalog.

 

function onChange(control, oldValue, newValue, isLoading) 
{ 
     if (isLoading || newValue == '') { 
            return; 
        } 
var firstDate = new GlideDateTime(g_form.getValue('first_date')); 
var secondDate = firstDate.addHours(72); 
g_form.setValue('second_date', secondDate); 
}

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Rajesh Chopade1
Mega Sage

hi @SrinivasuSagi 

 

You need to create 'onChange' client script and add bellow script:

// Get the date and time from the "When is this needed" variable
    var whenIsNeeded = g_form.getValue('when_is_this_needed'); // Replace with the actual variable name for "When is this needed"

    // If the "When is this needed" value is not empty
    if (whenIsNeeded) {
        // Convert the "When is this needed" value to a Date object
        var dateNeeded = new GlideDateTime(whenIsNeeded);
        
        // Add 72 hours to the date
        dateNeeded.addHours(72); 
        
        // Set the calculated date to the "When can the snapshot be deleted" variable
        g_form.setValue('when_can_the_snapshot_be_deleted', dateNeeded.getDisplayValue()); // Replace with actual variable name for "When can the snapshot be deleted"
    }

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

Ashish Parab
Mega Sage

Hello @SrinivasuSagi ,

Please try below onChange Client script that calls script include. Because addHours is not a valid method.

Script Include:

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

    addHours: function() {
        var date = this.getParameter('sysparm_dateTimeString');
		
		if(date){
		var gdt = new GlideDateTime(date);
		gdt.addSeconds(72*60*60);
		return gdt;
		}

    },

    type: 'dateValidation'
});

 

OnChange Client Script - 

UI Type - All

Variable Namewhen_is_this_needed

Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Call the Script Include to calculate the new date/time
    var ga = new GlideAjax('dateValidation');
    ga.addParam('sysparm_name', 'addHours'); // Specify the method in the Script Include
    ga.addParam('sysparm_dateTimeString', newValue); // Pass the selected date/time

    ga.getXMLAnswer(function(response) {
        if (response) {
            g_form.setValue('when_can_the_snapshot_be_deleted', response);
        }else{
			g_form.addErrorMessage("Error calculating new date/time from Script Include");
		}
        
    });

}

 

Output - 

ashish_parab_0-1733816125259.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish