- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2023 12:22 PM
Hi,
I'm new to ServiceNow and Java is my least fav. I'm struggling to create a simple logic how I want the form to behave.
I created a simple form
I would like the Milestone date field to subtract 56 days from the KickOffDate selected based on the Idea Type field equal to Process Improvement.
If Idea Type = Process Improvement and the selected KickOff Date then -56 days
Milestone output should be in date format MM/DD/YYYYI do not know what is the best approach nor how can I write a formula to populate the correct milestone date.
I tried in the calculated field but is keep showing blank
(function calculatedFieldValue(current) {
// Add your code here
var gdt = new current.milestone;
gdt.addDays(-56); // adds 14 days to the current date
gs.print(gdt);
//return ''; // return the calculated value
})(current);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2023 05:11 PM
Dates and date/times are manipulated server side using objects GlideDate and GlideDateTime. To fetch the value of a field into such an object, the GlideElement that "represents" the field should be used as parameter to the constructors.
E.g:
var gdt = new GlideDateTime(current.u_kickoffdate);Such an object can be set as value for a field; e.g:
current.u_milestone = gdt;Several notes about your code:
- If you want to add some days to a field and than set the result into another field, you should start by fetching the value of the field that holds the date you want to increment, not by fetching the value in field you want to set. This is programming 101, not Java, not JavaScript.
- Fields (which are GlideElement instances), e.g. current.milestone don't need and can't be instantiated; thus
new <GlideRecord>.<field name>is not a valid instruction.
Where available current is already an instance of GlideRecord, that creates instances of GlideElement as properties of itself for each field - no need to create anything.
- Don't force data or date/time formats; you would be showing the middle finger to your users. ServiceNow has a functionality where Dates and Date/Times are shown to each user in the Time Zone and format configured by the user for itself. All you need to make sure is to make your fields the proper type - e.g. Milestone should be a field of type Date or Date/Time. The system takes care of the rest.
- Scripting in ServiceNow is not Java but JavaScript. It couldn't even be Java, cause Java is not a scripting language.
Your final script could look something like:
(function calculatedFieldValue (current) {
// Only do the computation it the conditions are met
if (canComputeMilestone(current))
// If the conditions are met, return the computed date
return get56DaysBeforeKickOffDate(current);
else
// If the conditions are not met, return null
return null;
function canComputeMilestone (current) {
// Return the result of verifying that the Idea type has the required value and
return current.u_idea_type == '<whatever the value of choice "Process Improvement" is>' &&
// the field that needs to be offset actually has a value - is not empty/nil
!current.u_kickoffdate.nil();
}
function get56DaysBeforeKickOffDate (current) {
// Fetch the value in KickOffDate
var kick_off_date = new GlideDate('' + current.u_kickoffdate);
// Offset that value by subtrackting 56 days
kick_off_date.addDays(-56);
// Return the subtracted date
return kick_off_date;
}
})(current);Note that the script contains guessed field names and values - just copy+pasting it will 100% not work. E.g field having label "KickOffDate" might actually not be named u_kickoffdate. Or field having label "Idea type" might actually not be named u_idea_type. Generally speaking a lot of stuff has both an actual value/name that one uses in scripting and another (often called display value) that is used to display the info to users. E.g. reference fields where the actual value (what one uses in scripts) is a sys_id (unique key) and the display value (what is displayed to users) is the user's name. Or choice fields where one thing is the actual value and another thing is the text displayed to the user; e.g. when field State on Incident displays "Work in Progress" the actual value to be used in scripting and is saved into the DB is "2".
I hope you know how to check for choice values and field names; let us know if you don't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2023 05:11 PM
Dates and date/times are manipulated server side using objects GlideDate and GlideDateTime. To fetch the value of a field into such an object, the GlideElement that "represents" the field should be used as parameter to the constructors.
E.g:
var gdt = new GlideDateTime(current.u_kickoffdate);Such an object can be set as value for a field; e.g:
current.u_milestone = gdt;Several notes about your code:
- If you want to add some days to a field and than set the result into another field, you should start by fetching the value of the field that holds the date you want to increment, not by fetching the value in field you want to set. This is programming 101, not Java, not JavaScript.
- Fields (which are GlideElement instances), e.g. current.milestone don't need and can't be instantiated; thus
new <GlideRecord>.<field name>is not a valid instruction.
Where available current is already an instance of GlideRecord, that creates instances of GlideElement as properties of itself for each field - no need to create anything.
- Don't force data or date/time formats; you would be showing the middle finger to your users. ServiceNow has a functionality where Dates and Date/Times are shown to each user in the Time Zone and format configured by the user for itself. All you need to make sure is to make your fields the proper type - e.g. Milestone should be a field of type Date or Date/Time. The system takes care of the rest.
- Scripting in ServiceNow is not Java but JavaScript. It couldn't even be Java, cause Java is not a scripting language.
Your final script could look something like:
(function calculatedFieldValue (current) {
// Only do the computation it the conditions are met
if (canComputeMilestone(current))
// If the conditions are met, return the computed date
return get56DaysBeforeKickOffDate(current);
else
// If the conditions are not met, return null
return null;
function canComputeMilestone (current) {
// Return the result of verifying that the Idea type has the required value and
return current.u_idea_type == '<whatever the value of choice "Process Improvement" is>' &&
// the field that needs to be offset actually has a value - is not empty/nil
!current.u_kickoffdate.nil();
}
function get56DaysBeforeKickOffDate (current) {
// Fetch the value in KickOffDate
var kick_off_date = new GlideDate('' + current.u_kickoffdate);
// Offset that value by subtrackting 56 days
kick_off_date.addDays(-56);
// Return the subtracted date
return kick_off_date;
}
})(current);Note that the script contains guessed field names and values - just copy+pasting it will 100% not work. E.g field having label "KickOffDate" might actually not be named u_kickoffdate. Or field having label "Idea type" might actually not be named u_idea_type. Generally speaking a lot of stuff has both an actual value/name that one uses in scripting and another (often called display value) that is used to display the info to users. E.g. reference fields where the actual value (what one uses in scripts) is a sys_id (unique key) and the display value (what is displayed to users) is the user's name. Or choice fields where one thing is the actual value and another thing is the text displayed to the user; e.g. when field State on Incident displays "Work in Progress" the actual value to be used in scripting and is saved into the DB is "2".
I hope you know how to check for choice values and field names; let us know if you don't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:37 AM
Thank you
