0% found this document useful (0 votes)
91 views

SNOW - Glide Aggregate

Uploaded by

sowmiDarshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
91 views

SNOW - Glide Aggregate

Uploaded by

sowmiDarshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 25
servicenow Voncouver API Reference GlideAggregate - Global Glide aggregate enables creating datahare aggregation queries. ‘The GideAggregate classis an extension of GlideRecord and provides database aggregation (AVG, COUNT, GROUP_CONCAT, GROUP_CONCAT DISTINCT, MAX, MIN, STDDEV, SUM) queries. This functionaity con be helpful when creating customized reports or in ‘cakuiations for calculated fields. When you use GideAggregate on currency or price fields, you ore ‘working with the reference currency value. Be sure to convert the ‘aggregate values to the user's session currency for display. Because the conversion rate between the currency or price value (displayed value) andits reference currency valve (aggregation value) might change, the result may not be what the user exoects. Note: when using cn on-premise system, the database server time zone must be set fo GMI/UTC for this class to work properly. GlideAggregate - addAggregate(string agg, String name) Adds an aggregate to a database query. Name of an ‘aggregate 10 use. Valid valves: © AVG: Average value of the expression. + COUNT: Count of the number of non-null values. * GROUP_CONCAT: ‘Concatenates all servicenow Vancouver API Reference ra oa 3 ‘nor-null values of the group in ‘ascending order. joins them with o ‘comma ('"), and rotums tho result os 0 String, GROUP_CONCAT_D! STINCT: Concatenaies all nor-null values of the group in ‘ascending order, removes dupicates. joins them with a. ‘comma ('"), and retums the result oso String. + MAX: Largest, or ‘maximum, value. + MIN: Minimum valve. « SIDDEY: Population slondard ceviation. © SUM: Sum of al values. ‘Optional. Nome of ‘he field to group ‘ne results of the fame Sting aggregation by. Default: Null servicenow. Vancouver API Reference Returns Typ void Example The following example shows how to use GlideAagregate functions on the Incident [incident] table. (var incidentGA - new ClideAggregate(‘incident") incidentcn.groupBy ['category! incidentGa.orderByAggregate('COUNT", ‘sys_ned_count'): incidentca.addaggregate("AVG", ‘sys_nod_count'); incidentca.addaggregate("COUNT', ‘eys_ned_count! AncidentGA.adaggregate("GROUP_CONCAT?, 'sys_mod_count") incidentGA.addAggregate("GROUP_CONCAT_DISTINCT*, sys_mod_ lcount") 5 incidentGAa.addAggregate("MAX', ‘sys_nod_count'); incidentcA.addaggregate(*MIN', ‘sys_mod_count'); AncidentGR addAggregate("STDDEV', 'Sys_Rod_count'); incidentGa.addAggregate("SUM', ‘sys_nod_count'): [email protected](); Jsnile (incidentGa.next()) { igs info(!CATEGORY: ' + incidentGR.gatValue ("category"): (as.inro(‘AVG: * + incidentGA.getAgaregate (‘AVG', 'sys_med_ (couat')) (go into { 'Couw: ‘mod_count")) (98. Tnfo('GROUP_CONCRT: ' + incidentGA.getAggregate | 'GROUP_ |concar’, ‘sys_moa_count')); igs. info ("GROUP _colicaT DISTINCT: * + incidentGA.getaggregat le(*GROUP_CONCAT DISTINCT", 'sys_mod_count')); ige-info(TMAX: ' + incidentcA.getaggregate ("MAX", 'eys_nod_ lcount')) (gs. info('MIN: ' + incidentGA.getAggregate('MIN', ‘sys_mcd_ (count ')) \go- info { *STDDEV: 's_sod_count") | 4 dncidentea.getaggregate ("COUNT!, ‘oyo_ * + dncidentGa.getaggregate (‘STDDEV', ‘sy, servicenow. Vancouver API Reference las-inro( ‘sum: * + incidentGA.getagaregate (‘SUM', ‘sys_mod_ (couat')) \go-info(" * 2 Output. (CATEGORY: inquiry lave: 8.42424242424242424242424242424242424242E00 (cour: 33 (GROUP_CONCAT: 0, 0, 0,.0/2,2,2)4y 4, 4,5, 5,51 67616, 6, 8, 6, 6, 67 9,15, 15,19, 52,33, 36 + 0,1, 2,4,5,6,7,B, 9,15,19, 32, 33,36 IMnn: 0 leropey: 9.15843294125113629932710147135171494439200 ‘sum: 278 (CATEGORY: software lave: 22 \couur: 13 (GROUP_CONCAT: 3, 4,5,5,6,7,9,10,10,11,14, 94,95 \GROUP_CONCAT DISTINCT: 3,4,5,6,7,9,10,11,14,94, 95 wax: 95 lMnN: 3 \sropev: 3.27693962918655755532970326989852794087501 ‘sum: 273 (cArEGORY: Hardware lave: 6.8 (cour: 10 (GROUP_CONCAT: 2, 5,5, 6,6,6,7,7,9,15 (GROUP_CONCAT_DISTINCT: 2,5, 6,7,8,15 \vax: Ts Man: 2 ‘sropev: 3.39280283999985933622620108982884699755200 ‘sum: 68 (CATEGORY: network lave: 16 (cour: 5 (GROUP_CONCAT: 3, 12,17,21, 37 (GROUP_CONCAT DISTINCT: 3,12,17,21,37 servicenow. Vancouver API Reference (Group _coNcaT: 8, 8, 20,12 \GRoUPLcoNcaT_prstinct: 8,10,12 1, 91495421551267621995020382273964310607200 (CATEGORY: database lave: 29 2,50 IceouP_concan orsemucr: @, 50 bax: 30 an: 8 lsroDev: 2.96984e4e0se3499c024835463209403¢5965501 Isom: se Scoped equivalent To use the addagaregate|) method in a scoped application, use the corresponding scoped method: . GlideAggregate - addEncodedQuery(String query) ‘Adds an encoded avery tothe other queries thal may have been set for ‘his aggregate. Parameters Encoded query sting auery sting to.add to the aggregate, servicenow. Vancouver API Reference (var agg = new GlideAggregate( incident’); lag. addaogregate( ‘count’, "category") : lagg.orderByAggregate ("count', ‘category’ lag. orderBy ("category") 'agg.addQvery(*opened_at', '>=', ‘Javascript:gs.monthsAgost larcez)"); lagg-addgvery(*opened at", "<= lac2)) lagg.query(): Wwhile (agg.next (I) var category = aqg.categozy: var count = agg.getAggregate (*count', ‘cetegory") ; var query ~ agg.getQuery() var agg2 = naw Glidedggregate ("incident '); agg? .addAggregate ("count", 'category") agg2 orcersyaggrecate(*count', *category'): agg? .oxcerBy ("category"): agg? adcQuery ("opened at', '>=', 'Javascript:gs.nonthehg loseare(3)") ‘agg? .adcQvery ('opened_at', ‘< lozna(s)* ‘agg? -adcEncodeaguery (query) + agg? query ( var last while (agg2-next (0) ¢ last = agg2.getaggregate( ‘count’, ‘category') “Javascript:gs.moathsAgcEn d gs.log(categozy + ": Last month: lonth:" + last! + count +" Previous M servicenow Voncouver API Reference Scoped equivalent To use the addEncodedQuery() method in a scoped application, use the corresponding scoped method: . GlideAggregate - addHaving(String name, String operator, String value) ‘Adds a "having" element to the aggregate, such as select category, count{*) from incident group by calegary HAVING count|(*| > 5. Parameters co coy ee Aggegate to titeron name ‘Sting For example, COUNT. , Operate symbal, For operctor Sting oxemale <>, vetve sting vate query on. For Returns. void Example \var trend = new GlideAggregate ("incident"); trond.addfzend ("opened _at', 'Month') + (trond. adddggregate ("COUNT"): //addiaving Limits the results returned to those in which [the aggregate COUNT is greater than 2 lerend.addHiaving("COUNT', ">", 12") \erond. sotGroup(Faise) (Erend. query 0) lwnite(trend.next()) ( servicenow Voncouver API Reference gs.print (("Incidents by month * + trend.getvalue(‘timere f) +" where count is more than 2 count is: * + trend.get [Aggregate (*COUND') output Tactdenta By worth 5/2018 where count Ta wave than 7 coun actuesealoyieeet sys 07zor heceseleonaed afeeeat een Tome as tans ale peemt Ni as Tevenaleearet at eeet eet Tomen GlideAggregate - addTrend(String fieldName, String timeinterval, Number numUnits) Ades 0 tend forte Parameters Name othe field for felattome Sting wrth Fencing shoul Tire iniervalfor ibe fon VS vetes: + cote + dayolwook fimotnonal Sting + howe + minute + querer + vale + week servicenow. Vancouver API Reference Nome a ‘Optional. Only vad when timelnierval = minuto, Number of minutes to include in umnunits Number royale Dofoult:1 Returns oa void Example lvar trend = new Giiceagaregate (‘incident’); ltrend.adétvend (‘opened at', 'nonth’); lerend.addPggregate {*COUNE" |; lerond. setGroup (aise) lerend. query (0 Wwntte(trend.next()) ( g5-print (trend.getValue('tineret") +‘: * + trend.getag \gregate ‘couNr") Scoped equivalent To use the addirena|string fleldName, String timeintervai) method in a scoped application, use the comesponding scoped metnod: scoped servicenow Voncouver API Reference Glide Aggregate - addtrend(string fieldName, String timeinterval. Number aumnUnits) GlideAggregate - getAggregate(String agg, String name) Gels the value of an aggregate from the current record. Parameters Type of the aggregate, Vos vaues: + AVG: Average value of the oxpression. + COUNT: Count of the number of nor-null values. © GROUP_CONCAT: Concafenates cll ronal values of the group in escending order, 9g sting joins therm with o Somma (and returns no rosuitas a sting. GROUP_CONCAT_D| STINCT, ‘Conectenaies all non-null values of the group in ‘ascending order. removes duplicates, joins them with o ‘comma ('"), and retums the result oso String. servicenow Vancouver API Reference ca a Descripfion * MAL Largest, or maximum, value. + MIN: Minimum vaive. + SIDDEY: Population slonderd deviation. * SUM: Sum of all values. Name of the field to nome string get the agareacte from. ra Ley Value of the aggregation. If the values being aggregcted ore FX Currency values, the returned value ish tne format , such as: USD;134.980000, ‘sting Note: Ifthe specitiod field contains FX Curroncy values of mixed curancy types, the metnod is not able to cagregate the volues and retumse semicolon Example ‘This example shows how to obtain the COUNT aggregete. Fonction doMyBusinessRuis(assigned to, number) { var agg = new GlideaAggregate ("incident ') agg. adcquery("assiqned_to', assigned to) servicenow. Vancouver API Reference agg. aacquery(*category’, number); agg. addaggregate ("COUNT") ; agg-query () var ansuer = 'faice! if (agg-next()) { answer = agg. getAggregate ("COUNT") ; Af (answer > 0) eise ‘answer = ‘false’: , 1 Example ‘This example shows the aggregation of an Fx Currency field. (var ga - new GlideAggregate(' laptop tracke:") iga-addagoregate('SUM', ‘cost! ‘ga-groupBy ("name') : (ga. query () lwnile (ga.next()) { go.info('Aggregate results ' + ga.getValue("name') 4 ' > 1 + ga.gethggregate('SUM', ‘cost')) + d ‘Output: A= Script: Aggregate results Apple MacBook Air 1. 784260000000 *** Seript: Aggregate reoulte Apple MacBock Pro -> USD/165 1. 724280000000 w** Script: Aggregate results Dell XPS => UsD;470.85267200 ‘0000 ss* Script: Aggregate results 16 => s#* Seript: Aggregate results Samsung Galaxy => USD;225.32 USI 65 ‘000000000 ne* Script: Aggregate results Surtace3 => USD;2895. 3603695, \20000 s** Script: Aggregate results Toshiba => Usb; 9385.20287580 (0000 servicenow. Vancouver API Reference Scoped equivalent To use the gelAggregaie(| inethod in a scoped application, use the: corresponding scoped method: . GlideAggregate - getQuery() Retrieves the query necessary fo retuin the current aggregate. Parameters ro aa None Returns ad Sting The query. Example \var agg = new Glidexggregate('incident'"); lagg.addaccregate( ‘count’, "category"); lagg-ordexByAggregate ("count', ‘category’ lagg-ordexby ("category") lagg_addQuery("opened_at’, '>=', "javascript :gs.monthskgost lare(2)") 2 lagg.adaquery(" opened at’, * lacy; lagg- query ()7 janie (agg.next()) { var category = agg.category: var count = agg.getaggregate | "count", "category") ; var query = agg-getQuery() i var agg? ~ new GlideAggregate "incident! agg2.addAggragate ("count', 'category") + ag92.orderByAggregate("count', ‘category' aggz .orcersy ("category") agg2.adcQuery ("opened _at', '>=', ‘javascript:gs.montheag loceaze (3)")7 agg2-adcQuery('opened_at', "<=", 'javascript:gs.monthsay *, “Javascript:gs.monthsagcen servicenow Voncouver API Reference jozna3) "yz agg2.addEncodedguery (query) + agg2.query () var last =" while (agg2-next()) { last = agg2.getaggregate ‘count’, ‘category"); eel oe (ester ic freee erets tak eerste Ac eeertenet lonthi" + last): D GlideAggregate - getTotal(Striing agg, String name) Retums the number of recoras by summing an aggregate. Parameters De Nam: Name of an aggregate to use. Valid values: + AVG: Average value of the expression. + COUNT: Count of the number of nor-null values. * GROUP_CONCAT: egg ‘String Concatenates all non-null values of the group in ‘ascending order, joins them with o ‘comma ('"), and retums the result oso String. GROUP_CONCAT_D| STINCT: servicenow. Vancouver API Reference ‘Concatonates all, ‘non-null values of the group in ‘ascending order, removes duplicates, joins thom with o ‘comma ('!), and retums tne result as a Siting. MAX: Largest, or ‘maximum, value. MIN: Minimum value. = SIDDEY: Population sionderd deviation. ‘SUM: Sum of all values. Name of the field to aggregate. Number of records. (var incidentGa = new Ciidelggregate ("incident"); incidentGA.addQuery (‘category', ‘software’ incidentGA.addAggregate ("COUNT"); incident¢A.addfrend(‘opened_at',"year'}; // Counting nube le of incidents for software category per year incidentGn.setGroup (false) incident¢a-query(); \wniLe (incidentGA.next ()) ( gs.info(*Incidents opened on year ~ ‘rincidentca.getValu servicenow. Vancouver API Reference le(*timerer')+" - ‘+inciaentGa.cetaggregate ("COUNT") ); d \go-info ("Total Aggregate Value >> '+ incidentGa.getTotal (" lcounr')) Output: Incidents opened on year - 2015 - 1 Incidents opened on year - 2018 - 5 Incidents opened on year - 2020 - 10 Total Aggregate Valve >> 16 GlideAggregate - getvalue(String name) Retums the value of a field. name string Name of the field. Returns oa sting Value of the specttied field. Retums nul if invaiid (not part of result set). Example \var trend - new GlideAggregate |"incident') (trond. addzend ("opened _at', 'Month') + (trond. adddggregate ("COUNT"): //addaving limits the results returned to those in which (the aggregate COUNT is greater than 2 lerend.addHaving("COUNT', '>', 12") [trend. setGroup (false) (Erend. query () \wntte(trend.next()) ( go.print (("Incidents by month ' + trend.getValue('tinere £1) 4" where count ie more than 2 count ie: ' + trend.get servicenow. Vancouver API Reference lagaregate (*COuNT")); z Output: Incidents by month 9/2018 where count ie more than 2 coun lr is: 3 Incidents by month 10/2018 where count is more than 2 coun eis: 8 Incidents by month 11/2018 where count is more than 2 coun le io: 34 Scoped equivalent To use the getvelue() metnod in a scoped application, use the comresponding scoped method: . GlideAggregate - groupBy(String name) Provides the name of a field fo use in grouping the aggregates. May be called numerous times fo set multiple group fields. nome String Name of the field. a void Example [var count = new GlideAggregate ("incident") \count .addAggregate|'MIN', 'sys_nod_count') \count.addaggregate|*MAX", ' sys_mod_count') \count.adaaggregate|‘AVG", ‘sys mod_count'); ‘count.groupsy(* category" servicenow Voncouver API Reference (count query (0 lwnite {count-next()) { var nin ~ count .getAggregate (!MIN' var ‘count .getAggregate | ‘MAX! var avg = count .getAggregate | "AVG var category gs. tog (category + " Update counts: MIN =" + min +" MA k=" + max +" AVG =" + avg); 2 Scoped equivalent To use the group8y() method in a scoped appication, use the ‘comresponding scoped method: . GlideAggregate - orderBy(String name) Orders the aggregates using the value of the specified field. The field is also added fo the group-by list. Name of he field used fo o1cer Ihe ‘aggregates. Aitematively, you can Provide a. glidefunciion to order the aggregates, such glidefunction: eng tn(short_descripti nome String on). For more informeticn about glidefunctions, see glidefunction operations. servicenow. Vancouver API Reference lvar agg = new Glidexagregate("incident'"); lagg-addaggregate( ‘count’, "category") lagg.ordezByAggregate ("count', ‘category’ lagg-ordexBy ("category") 'agg-addQuery("opened_at', '>=', 'Javascript:gs.monthsAgost larcczi 9s lagg-addQuery(*opened_at", "<=", "Javascriptigs.moathsAgcen lava") lagg.query(): Wwhile (agg.next (I) var category = aqg.categozy: var count = agg.getAggregate (*count', ‘cetegory") ; var query ~ agg.getQuery() var agg2 = naw Glidedggregate ("incident '); agg? .addAggregate ("count", 'category") agg2 orcersyaggrecate(*count', *category'): agg? .oxcerBy ("category"): agg? adcQuery ("opened at', '>=', 'Javascript:gs.nonthehg loseare(3)") ‘agg? .adcQvery ('opened_at', ‘< lozna(s)* ‘agg? -adcEncodeaguery (query) + agg? query ( var last while (agg2-next (0) ¢ last = agg2.getaggregate( ‘count’, ‘category') d gs.log(categozy + ": Last month: lonth:" + last) d ‘Scoped equivalent ‘To use the orderfy() method in a scoped application, use the comrosponding scoped method: . + count +" Previous M servicenow Voncouver API Reference GlideAggregate - orderBy Aggregate(String agg, String name) Orders the aggregates based on he spectied aggregate and feld. Parameters on ro " Type of aggregation. Valid vaves: + AVG: Average value of the oxpr + COUNT: Count of the number of non-null values, + GROUP_CONCAT: Concefenates all ‘non-null velues of the group in ‘scending order, joins them with o . ‘comma ('!), and 99 Sting retums the result as a Siting. GROUP_CONCAT_D| STINCT, Concctenates all non-null values of the group in ‘ascending order. removes duplicates, joins them with o ‘comma ('"), and retuns the result aso String. + MAX: Largest, or ‘maximum, valve servicenow. Vancouver API Reference Nome a EB * MIN: Minimum value. + SIDDEY: Population siondrd deviation. + SUM: Sum of all values. Name of the field to fieldName: ‘string ‘cagecate. Returns Type void Example ‘agg = new GlideAggregate (‘incident’) addaggregate('count', "category" sorderByAggregate (‘count', ‘category’ orderzy ("category") lagg.addQuery(*opened_at*, >=" lart(2)"); lagg-addgvery("opened_at', '<=', "Javascript:ge-monthenceen lac2y"): lagg.query() Wwnile (acg.next(]) var category ~ agg.category; var count = agg.getAggregate |"count', 'eategory") ; var query = agq.getQuery( var aggz = new Gliceaggregate ("incident agg? .addAggregate (*count', ‘category") agg? .oxderByAggregate("count', ‘category'); agg? .orderBy(‘ category") agg?.adcQuery("opened_at', ">=! lostart 131"): ‘agg? .adeQuery ("opened_at', lenna(3)* ‘agg? -adcEncodedQuery (query) + “Javascript:gs-moathsagest. ) “Javascript:gs.nonthsag servicenow. Vancouver API Reference agaz.avery( var last ="; white (agg2.next (0) { Last = agg2.getAggragate ‘count, ‘category! d gs.1og (category + ": Last montn:" + count +" Previous loncn:* + last) 1 Scoped equivalent To se the orderByAggregate() method in a scoped application, use the coresporcing scoped method: . GlideAggregate - query() Issues the query and gets the resus Parameters ca a None Returns (var agg = new Glidexggregate (‘incident’); ‘agg. addacaregate( ‘count’, "category" lagg- orderByAggregate ("count', ‘category’); lagg-ordezBy ("category") |agg-addQuery("opened_at’, '>=', "javascript :gs.monthskgost larci2)")s ‘agg. addquery("openeg_at’, ‘<=', ‘Javaseriptigs.montnsacen (aca) lagg. query () jsnile (agg.noxt()) ( servicenow. Vancouver API Reference var category = aqg.categozy: var count = agg.getAggregate (*count', category") ; var query ~ agg.getQuery() var agq2 = naw Glidedggregate ("incident agg? .addAggregate ("count", 'category") agg2 .orcersyaggrecate(*count', *category'): agg? .oxderBy ("category") agg? adcQuery ("opened at’, '>-', ‘Javascript js.nonthsAg loseaze (3)") agg2.adQuery('opened_at', "<=", 'javascript:gs.monthsag lofnd(3)* agg? .aduEncodedQuery (query) ; agg? query ( var let = while (agg2-next()) { last = agg2.getAggregate ‘count’, ‘category’ d go.1og (category + ": Last month:" + count +" Previous M Scoped equivalent To use the query!) method in c scoped application, use the comrespercing scoped method: . GlideAggregate - setAggregateWindow(Number firsiRow, Number lastrow) Limits the number of rows from the lable fo include in the aggregate query. Parameters Na Typ cee Tero-based index of the fist row to include in fistkow Number the aggregate query, inclusive. Tero-based index of the last row fo include in lasfRow — Number the aggragate query, exclusiva, servicenow. Vancouver API Reference Typ None Example Prints the count of each categary for the fist ten records in the Incident lincideni] table. (var ga - new GlideAggregate ("incident"); \ga-addaggregate('COUNT', ‘category') + (ga. setAggregateWindaw(0, 10 \ga-query () Wwnile (ga.next()) { var incidentCount — ga.getAggregate('COUNT', ‘category’ gs.info("{0) count: (1}', [ga.getValue('category'), inc igentcount} 1 output: (database coun Hardware coun: inquiry count: 'software count: Scoped equivalent To use the setAggregateWindow!) method in a scoped application, use ‘the corresponding scoped method: . GlideAggregate - setGroup(Boolean b) Sets whether to group the results. servicenow Vancouver API Reference Parameters Flag that indicates ‘whether to group the results. Valid values: b Boolean + true: Group the results. + fae: Do not group the results. Returns Type void Example (var ga = new Glidekggregate (' incident’) \ga.addagoregate("COUNI", ‘ category"): \ga-setGroup (true) i Scoped equivalent Jo use the setGroup() method in a scoped application. use the comresponcing scoped method: .

You might also like