Functional Programming !! ( How to use it in ServiceNow)

SandeepKSingh
Kilo Sage
 
2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @SandeepKSingh 

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.

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

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:

  1. Immutability: Data should not be changed once created. Instead, create new data structures.
  2. Declarative Code: Focus on "what to do" rather than "how to do it."
  3. Higher-Order Functions: Functions that take other functions as arguments or return them as results.
  4. 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:

  1. Script Includes: For reusable utilities and data processing.
  2. Transform Maps: When working with integrations.
  3. Client Scripts & Catalog Client Scripts: To process data on the client-side.
  4. REST API Responses: To filter and transform large JSON datasets.
  5. 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.
  6. Example :-
  7. 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/

View solution in original post

6 REPLIES 6

Ravi Chandra_K
Kilo Patron
Kilo Patron

I have attended Pune Meetup where this is discussed....

Please reach out to Amit Gujarathi he might have presentation deck....

 

https://www.linkedin.com/in/amit-gujarathi-98632a175?utm_source=share&utm_campaign=share_via&utm_con...

Please mark the answer as helpful and correct if helped. 

Kind Regards, Ravi Chandra  

Community Alums
Not applicable

Hi @SandeepKSingh 

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.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

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:

  1. Immutability: Data should not be changed once created. Instead, create new data structures.
  2. Declarative Code: Focus on "what to do" rather than "how to do it."
  3. Higher-Order Functions: Functions that take other functions as arguments or return them as results.
  4. 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:

  1. Script Includes: For reusable utilities and data processing.
  2. Transform Maps: When working with integrations.
  3. Client Scripts & Catalog Client Scripts: To process data on the client-side.
  4. REST API Responses: To filter and transform large JSON datasets.
  5. 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.
  6. Example :-
  7. 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/

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @SandeepKSingh 

 

Latest @Amit Gujarathi  delivered session in Pune meetup. You can connect with him if required.

*************************************************************************************************************
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]

****************************************************************************************************************