---
original_url: "https://jace.pro/blog/debugging-with-fix-scripts/"
format: markdown
ai_optimized: true
---

Debugging with fix scripts# Debugging with fix scripts

May 14, 2018 [servicenow](/tags/servicenow/)

  Enable AI AnimationDebugging for me always starts with isolating the parts of the failing code.

The quickest way for me to find the issue varies but it almost always includes some variation of order of some of these;

QuestionDebugging ToolDescriptionAny errors or logs?System LogNavigate to System Logs > System Log. You can place alert statements in your business rule which can write information to the log.Any errors or logs?Debug Business Rule (Details)Navigate to System Diagnostics > Debug Business Rule (Details). This debugging module displays the results business rules. Use this module to see if conditions are being met and values are being set as expected.Did anything about this change?`sys_update_xml` record listCan you reproduce the problem?Background Scripts / Fix ScriptsSee [Simulating the Script](https://jace.pro/post/2018-05-13-debugging/#simulating-the-script)## [Simulating the script](#simulating-the-script)

Different area’s have different variables available to them;

I’d copy the appropriate script, and then paste your code in the run function.

## [Business Rules](#business-rules)

`var table = 'incident';//pick a table here
var sysId = '';//pick a record with a sysid here

var current = new GlideRecord(table);
current.get(sysId);
var previous = new GlideRecord(table);
previous.get(sysId);

try {
    // paste your code below here

    // paste your code above here
} catch (error) {
    gs.info(error.message, "fix script " + gs.getUserName());
}`## [Inbound Emails](#inbound-emails)

```
`var emailSysId = '';//pick a sys_email record's sysid here

var email = new GlideRecord('sys_email');
if(email.get(emailSysId)){
    var current = new GlideRecord(email.getValue('target_table'));
    current.get(email.getValue('instance'));
    email.getValue('body_text').split('\n').map(function(line){
        var lineArray = line.split(':');
        email[lineArray[0]] = lineArray.pop().join(':');
    });
}
var event = {};
try {
    // paste your code below here

    // paste your code above here
} catch (error) {
    gs.info(error.message, "fix script " + gs.getUserName());
}`
```

## [Script Actions](#script-actions)

```
`var eventSysId = '';//pick a sysevent record's sysid here

var event = new GlideRecord('sysevent');
if(event.get(eventSysId)){
    var current = new GlideRecord(event.getValue('table'));
    current.get(event.getValue('instance'));
}

try {
    // paste your code below here

    // paste your code above here
} catch (error) {
    gs.info(error.message, "fix script " + gs.getUserName());
}`
```

## [Workflow Activity](#workflow-activity)

```
`var workflowContextSysId = '';
var workflowContext = new GlideRecord('wf_context');
if(workflowContext.get(workflowContextSysId)){
    var current = new GlideRecord(workflowContext.getValue('table'));
    current.get(workflowConext.getValue('id'));
    var workflow = {
        scratchpad: JSON.parse(workflowContext.getValue('scratchpad'));
    };
}

try {
    // paste your code below here

    // paste your code above here
} catch (error) {
    gs.info(error.message, "fix script " + gs.getUserName());
}`
```

## [Transform Scripts](#transform-scripts)

```
`var importSetRowSysId = '';//sysid from your import table
var targetTable = '';// table to set the field on
var targetSysId = null; // doesn't need to be set but if you know the record, you can set this sysid

var importTable = new GlideRecord('sys_import_set_row');
if(importTable.get(importSetRowSysId)){
    var source = new GlideRecord(importTable.getValue('sys_class_name'));
    source.get(importTable.getValue('sys_id'));
    var target = new GlideRecord(targetTable);
    if(targetSysId != null){
        target.get(targetSysId);
    } else {
        target.newRecord();
    }
}

try {
    // paste your code below here (source, and target are set up from above)

    // paste your code above here
} catch (error) {
    gs.info(error.message, "fix script " + gs.getUserName());
}`
```

## [Relationship Scripts](#relationship-scripts)

```
`/* global GlideAggregate, GlideRecord, gs */
var parent = new GlideRecord('');//applies to table
parent.get('ad6ce161db560740d9ca72ec0f9619f5');//specific record to test.
var current = new GlideRecord('task');//queries from table
//set the above gr's to have access like you would on the form.

(function refineQuery (current, parent) {
    try {
        // paste your code below here

        // paste your code above here
    } catch (error) {
        gs.info(error.message, "relationship " + gs.getUserName());
    }
})(current, parent);
current.query();`
```

## [Mail Scripts](#mail-scripts)

```
`var table = '';//set the current table name
var sysid = '';//set the current sysid
var eventsysid = '';//set to the event sysid on a past sysevent
//current, template, email, email_action, event
var event;
if(eventsysid.length>1){
  event = new GlideRecord('sysevent');
  event.get(eventsysid);
}
var current = new GlideRecord(table);
current.get(sysid);
var template = {
  print: function(text){
    gs.info(text);
  },
  space: function(num){
    var returnStr = '';
    for(var x=0;x<num;x++){
      returnStr += ' ';
    }
    return returnStr;
  }
}
var email = {
  addAddress: function(){},
  setBody: function(){},
  setFrom: function(){},
  setReplyTo: function(reply){
    gs.info('Set ReplyTo: ' + reply);
  },
  setSubject: function(subj){
    gs.info('Set Subject to: ' + subj);
  },
  addAddress: function(a,b,c){
  //doing nothing this is a placeholder
  },
  setBody(html){
    gs.info(html);
  }
};
try {
    // paste your code below here

    // paste your code above here
} catch (error) {
    gs.info(error.message, "relationship " + gs.getUserName());
}`
```

## [Debugging Tools](#debugging-tools)

Debugging ToolDescriptionSystem DictionaryNavigate to System Definition > Dictionary. The dictionary provides a list of all tables within your instance and can be invaluable when trying to locate information. One example is verifying the column exists on the table or a parent table of your record.System LogNavigate to System Logs > System Log. You can place alert statements in your business rule which can write information to the log.Debug Business Rule (Details)Navigate to System Diagnostics > Debug Business Rule (Details). This debugging module displays the results business rules. Use this module to see if conditions are being met and values are being set as expected.Alert MessagesThere are system functions that allow you to print messages to the page, the field or the log file. See Scripting alert, info, and error messages.Business Rule ExamplesSometimes you can find what you’re looking for in scripts others have written, including business rule error messages, or by building an OR query.GlideRecord InformationThis is the basic syntax used to query the database for information. See Using GlideRecord to query tables. GlideRecord also includes aggregation support.
---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2018/2018-05-13-debugging-with-fix-scripts.md).

[Debugging with fix scripts](https://jace.pro/blog/debugging-with-fix-scripts/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/debugging-with-fix-scripts/*
