Development in servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 03:26 AM
I have two fields effective and end date , the end date should be defaulted to one year upon selecting the effective date in client script in servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 03:31 AM
Have a look here, change the fields and try your hands
https://www.servicenow.com/community/itsm-forum/add-day-to-a-date-field/m-p/620520
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 04:16 AM
Something like this should work in a server side code, like BR
var gdt = new GlideDateTime(current.<effictive date field>);
current.end_date = gdt.addDays(365);;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 04:28 AM
Create a Script Include
and client script as below
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading) {
var effectiveDateField = 'effective_date';
var endDateField = 'end_date';
// Get the values of the effective date field
var effectiveDate = g_form.getValue(effectiveDateField);
// Check if the effective date is not empty
if (effectiveDate) {
// Call the server-side script to calculate one year later
var ga = new GlideAjax('DateUtils');
ga.addParam('sysparm_name', 'calculateOneYearLater');
ga.addParam('sysparm_start_date', effectiveDate);
ga.getXML(answerCallback);
} else {
g_form.setValue(endDateField, '');
}
}
}
function answerCallback(response) {
if (response && response.responseText) {
var endDate = response.responseText;
var endDateField = 'end_date';
// Set the calculated end date in the end date field
g_form.setValue(endDateField, endDate);
}
}
Script Include
var DateUtils = Class.create();
DateUtils.prototype = {
initialize: function() {},
// Function to calculate one year later from the given date
calculateOneYearLater: function(startDate) {
var gdtStartDate = new GlideDateTime(startDate);
gdtStartDate.addYears(1);
return gdtStartDate.getDisplayValue();
},
type: 'DateUtils'
};
Please Mark it correct if your issue is resolved.
Thank You,
Naveen G N
ServiceNow Developers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 06:23 AM
Sure, you can achieve this by using a client script in ServiceNow. Here's a sample code:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Get the effective date
var effectiveDate = g_form.getValue('effective_date');
if(effectiveDate != ''){
//Create a date object
var date = new GlideDateTime(effectiveDate);
//Add one year to the date
date.addYears(1);
//Set the end date
g_form.setValue('end_date', date.getDisplayValue());
}
}
Here are the steps to implement this:
1. Navigate to "Client Scripts" in ServiceNow.
2. Click on "New" to create a new client script.
3. Fill in the necessary fields:
- Name: Give a name to your client script.
- Table: Select the table where your fields are located.
- Type: Select "onChange".
- Field name: Select your "effective_date" field.
4. Paste the above code in the "Script" field.
5. Click on "Submit" to save your client script.