- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 09:09 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 10:29 PM - edited 12-16-2024 10:30 PM
In functional programming, we should use functions as much as possible. Functions are the most important part of functional programming (especially in JavaScript). Functions are the single source that helps developers to perform functional programming. Generally abbreviated as Functional Parameters which revolves around functions and it is how we use functions that makes our code functional.
key component:
- A function always take input(in FP)
- A function always returns the output.
function function_name(fp_name){
gs.print(fp_name);
}
function_name('hello You!');
Input: In general programming, we interchangeably use arguments with parameters but they are not the same ones. Arguments are data that we pass in via function call and parameters are data we receive in a function definition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 11:21 PM
Functional programming (FP) is a programming paradigm where the primary building blocks are pure functions—functions that always produce the same output for the same input and have no side effects. It emphasizes:
- Immutability: Data should not be changed once created. Instead, create new data structures.
- Declarative Code: Focus on "what to do" rather than "how to do it."
- Higher-Order Functions: Functions that take other functions as arguments or return them as results.
- Avoiding Side Effects: Functions shouldn’t affect the program's state or depend on global variables.
Functional Programming in ServiceNow
While ServiceNow primarily uses JavaScript for scripting (which supports functional programming constructs), the platform isn't inherently functional. However, you can apply FP principles in areas like:
- Script Includes: For reusable utilities and data processing.
- Transform Maps: When working with integrations.
- Client Scripts & Catalog Client Scripts: To process data on the client-side.
- REST API Responses: To filter and transform large JSON datasets.
- Background Scripts: For server-side data manipulation.
Core JavaScript Functional Methods
You can use JavaScript's functional programming methods in ServiceNow scripting. These include:
- map(): Transform each element in an array.
- filter(): Filter elements based on a condition.
- reduce(): Reduce an array to a single value (e.g., sum).
- forEach(): Iterate through elements for side effects.
- Example :-
-
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('active', true);
incidentGR.query();var incidents = [];
while (incidentGR.next()) {
incidents.push({
number: incidentGR.getValue('number'),
priority: incidentGR.getValue('priority')
});
}// Transform priorities to human-readable labels
var transformed = incidents.map(function (incident) {
return {
number: incident.number,
priorityLabel: incident.priority === '1' ? 'Critical' : 'Non-Critical'
};
});gs.info(JSON.stringify(transformed, null, 2));
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 11:33 PM
Thanks @Community Alums and @Ravi Gaurav this helps .. and thanks for suggstion @Dr Atul G- LNG and @Ravi Chandra_K
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 11:39 PM
Hi @SandeepKSingh ,
This is the below i have seen which explains functional programming in a netshell and with easy diagrams... i hope it helps.. 🙂
https://www.turing.com/kb/introduction-to-functional-programming
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....