User Tools

Site Tools


background_scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
background_scripts [2024/12/12 07:53] johnsonjohnbackground_scripts [2024/12/12 12:22] (current) johnsonjohn
Line 121: Line 121:
 =====Metric Incident Category===== =====Metric Incident Category=====
 <code> <code>
 +//Incident Category = 1bf7c3561bce8d5099e265b1604bcb43
 +//Incident Subcategory = e02847961bce8d5099e265b1604bcba9
  
 +var definition = new GlideRecord("metric_definition");
 +definition.get("name", "Incident Category")
 +gs.print(definition.name);
 +
 +var gr = new GlideRecord("incident");
 +    gr.addEncodedQuery("opened_at<=javascript:gs.endOfYesterday()");
 +    gr.setLimit(3);
 +    gr.query();
 +
 +    gs.print('Found ' + gr.getRowCount());
 +
 +// Update fieldname below for category or subcategory
 +while(gr.next()) {
 +    gs.print(gr.sys_id);
 +    history = new GlideRecord("sys_audit");
 +    history.addQuery("documentkey", gr.sys_id);
 +    history.addQuery("fieldname", "category");
 +    history.orderBy("sys_created_on");
 +    history.query();
 +
 +    while(history.next()) {
 +        var grMetric = new GlideRecord('metric_instance');
 +        grMetric.addQuery('id', gr.getValue('sys_id'));
 +        grMetric.addQuery('definition', definition.getValue('sys_id'));
 +        grMetric.addQuery('value', gr.getDisplayValue('category'));
 +        // For original value, use the line below instead of the line above
 +        //grMetric.addQuery('value', history.getValue('oldvalue'));
 +        grMetric.query();
 +
 +        var gdtStart = new GlideDateTime(history.sys_created_on);
 +        // For original time, use the line below instead of the line above
 +        //var gdtStart = new GlideDateTime(gr.opened_at);
 +        gs.print("Start "+gdtStart);
 +        if (!grMetric.hasNext()) {
 +                var now = new GlideDateTime();
 +                var instant = new GlideDuration(0);
 +                //var gdurCalendar = GlideDateTime.subtract(gdtStart, gdtEnd);
 +
 +                grMetric = new GlideRecord('metric_instance');
 +                grMetric.initialize();
 +                grMetric.setValue('table', gr.getRecordClassName());
 +                grMetric.setValue('id', gr.getValue('sys_id'));
 +                grMetric.setValue('definition', definition.getValue('sys_id'));
 +                grMetric.setValue('field', definition.getValue('field'));
 +                // Update value below for category or subcategory
 +                grMetric.setValue('value', history.getValue('oldvalue'));
 +                grMetric.setValue('duration', instant);
 +                grMetric.setValue('calculation_complete', true);
 +                grMetric.setValue('start', gdtStart);
 +                grMetric.setValue('end', now);
 +                grMetric.insert();
 +        }
 +    }
 +}
 </code> </code>
  
Line 128: Line 184:
 =====Trigger Business Rule===== =====Trigger Business Rule=====
 <code> <code>
 +ClearUserLog();
 +//Log Time Started
 +UserLog("Script Started at " + gs.nowDateTime());
  
 +//Query Table
 +var gr = new GlideRecord('sys_user');
 +gr.addEncodedQuery('u_business_unit!=NULL^u_business_unit_ref=NULL');
 +gr.query();
 +
 +//Update Records
 +while (gr.next()) {
 +    // gr.setWorkflow(true);
 +    gr.setForceUpdate(true);
 +}
 +
 +//Log Time Ended
 +UserLog("Script Ended at " + gs.nowDateTime());
 </code> </code>
  
 ---- ----
  
-=====Unlock PROD=====+=====Unlock USER PROD=====
 <code> <code>
 +ClearUserLog();
 +//Log Time Started
 +UserLog("Script Started at" + gs.nowDateTime());
  
 +//Query Table
 +var gr = new GlideRecord("sys_user");
 +gr.addQuery('active', 'true');
 +gr.query();
 +    while (gr.next()) {
 +        gr.locked_out = false;
 +        gr.setWorkflow(false);
 +        gr.update();
 +    }
 +
 +//Log Time Ended
 +UserLog("Script Ended at" + gs.nowDateTime());
 </code> </code>
  
Line 142: Line 229:
 =====Update Time Zone===== =====Update Time Zone=====
 <code> <code>
 +ClearUserLog();
 +//Log Time Started
 +UserLog("Set TimeZone Started at" + gs.nowDateTime());
 +
 +//Query Table
 +var gr = new GlideRecord('sys_user');
 +gr.addNotNullQuery('location');
 +gr.addNullQuery('time_zone');
 +gr.query();
 +
 +//Update Records
 +while (gr.next()) {
 +    gr.time_zone = gr.location.time_zone;
 +    gr.setWorkflow(false);
 +    gr.update();
 +}
  
 +//Log Time Ended
 +UserLog("Set TimeZone Ended at" + gs.nowDateTime());
 </code> </code>
  
 ---- ----
background_scripts.1734018783.txt.gz · Last modified: 2024/12/12 07:53 by johnsonjohn