Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client script

Gangaiah Deepu
Tera Contributor

If the variable is "Please select the start date from which the change should take effect (only full months from the 1st of a calendar month):" and type is "Date " then default value should be "1st of current month"
How to achieve this using client script

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Gangaiah Deepu ,
I trust you are doing great.
Please find below code :

(function onLoad() {
    // Ensure this runs only on the form view
    if (g_form.isNewRecord()) {
        var today = new Date();
        var firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
        
        // Format the date to YYYY-MM-DD for setting the date field value
        var formattedDate = firstDayOfMonth.toISOString().split('T')[0];
        
        // Replace 'your_date_field' with the actual field name
        g_form.setValue('your_date_field', formattedDate);
    }
})();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

2 REPLIES 2

Rob Sestito
Mega Sage

Hello,

 

If I am following your request correctly here, the client script may look something like this (be sure to make your changes for your actual field value):

function onLoad() {
    // Get current date
    var currentDate = new Date();
    
    // Set date to first day of current month
    currentDate.setDate(1);

    // Format date to yyyy-mm-dd
    var year = currentDate.getFullYear();
    var month = (currentDate.getMonth() + 1 < 10) ? '0' + (currentDate.getMonth() + 1) : currentDate.getMonth() + 1;
    var day = (currentDate.getDate() < 10) ? '0' + currentDate.getDate() : currentDate.getDate();
    var formattedDate = year + '-' + month + '-' + day;

    // Set default value for date field
    g_form.setValue('date_field_name', formattedDate);
}

 

Hope this helps get you going!

Thanks,

-Rob

 

** Please remember to mark replies Helpful and/ or Correct when appropriate **

 

 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Gangaiah Deepu ,
I trust you are doing great.
Please find below code :

(function onLoad() {
    // Ensure this runs only on the form view
    if (g_form.isNewRecord()) {
        var today = new Date();
        var firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
        
        // Format the date to YYYY-MM-DD for setting the date field value
        var formattedDate = firstDayOfMonth.toISOString().split('T')[0];
        
        // Replace 'your_date_field' with the actual field name
        g_form.setValue('your_date_field', formattedDate);
    }
})();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi