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.

How to create client script to auto populate the date field based on current date + 7days

nandhini muthu
Tera Contributor
 
1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @nandhini muthu ,

 

You can use calculated filed option like below.

 

RunjayPatel_0-1736344249066.png

 

Code:

var gdt = new GlideDateTime(); 
	gdt.addDaysUTC(7); 
	gdt.getDate();
	return gdt;  // return the calculated value

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

8 REPLIES 8

nandhini muthu
Tera Contributor

Thanks for the reply to everyone

But ,I need this .Date field should be auto populate with addition of 7 days from current date and exclusive of holidays/weekends .So I have created  schedules and used in the script include and Onload client script .But getting 'javascript browser console error ".Please give your inputs

 

Please find the code below:

 

Please find the code of script Include:

 

var AddingSchedulestodays = Class.create();
AddingSchedulestodays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    calculatefuturedate:function(daystoAdd)
    {
       
        var scheduleID='schedule_sys_id';
        var schedule=new GlideSchedule('scheduleID');
        var startDate=new GlideDateTime();
       

        var futureDate=new GlideDateTime(startDate);
        for(var i=0;i<daystoAdd;i++)
        {
            futureDate=schedule.add(futureDate,1,'day');
            
        }
        var result=futureDate.getLocalDate();
         return result;
        },

    type: 'AddingSchedulestodays'
});
 
Onload client script:
 
function onLoad() {
   //Type appropriate comment here, and begin script below

   
   var ga= new GlideAjax('AddingSchedulestodays');
   ga.addParam('sysparm_name','calculatefuturedate');
   ga.addParam('sysparm_days','7');
   ga.getXMLAnswer(function(response)
   {
   var futureDate=response.responseXML.documentElement.getAttribute('answer');
    if(futureDate)
    {g_form.setValue('date_field',futureDate);
    }
   });
   
}

Hi @nandhini muthu ,

 

Update the script include code with below.

var days = 7;

var opened = new GlideDateTime();

var duration = new GlideDuration(60*60*24*1000*days);

var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // Use the Existing 8-5 weekday schedules

var updatedDays = schedule.add(opened, duration);

return updatedDays ;

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

I have tried as you mentioned,but still getting same error

script include:

 

var Effectivedays = Class.create();
Effectivedays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    calculatedate:function()
    {
var days=7;
var startdate =new GlideDateTime();
var duration =new GlideDuration('60*60*24*1000*days');
var schedule=new GlideSchedule('scheduleID');
var newday =schedule.add(startdate,duration);
return newday;
    },

    type: 'Effectivedays'
});
Onload client script:
 
function onLoad() {
   //Type appropriate comment here, and begin script below

   var ga= new GlideAjax('Effectivedays');
   ga.addParam('sysparm_name','calculatedate');
   ga.addParam('sysparm_days','7');
   ga.getXMLAnswer(function(response)
   {
    alert("glideAjax responsed");
    var futureDate=response.responseXML.documentElement.getAttribute('answer');
    alert(futureDate);
   
    if(futureDate)
    {g_form.setValue('eeffective_date',futureDate);
    }
   
   });
   
}

Please help me on this.