tommeh_1337

ServiceNow config exceptions business rule

Feb 14th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function executeRule(current, previous /*null when async*/) {
  2.    
  3.     var maProperty = 'cmdb.managed_attributes.' + current.sys_class_name.toString(); //get managed attributes property
  4.     var fieldsArray = gs.getProperty(maProperty).split(','); //convert comma separated attributes string into an array
  5.     var oldValues = ''; //used for populating previous (old) values
  6.     var newValues = ''; //used for populating current (new) values
  7.     var configExc = false;
  8.    
  9.     for(var i = 0; i < fieldsArray.length; i++) {
  10.         if(previous[fieldsArray[i]] != current[fieldsArray[i]]){ //check if the value of current element is changed
  11.             configExc = true; //one of the managed attributes is changed, so we need a config exception record
  12.             oldValues += current[fieldsArray[i]].getLabel() + ': '+ previous[fieldsArray[i]].getDisplayValue() + '\n';
  13.             newValues += current[fieldsArray[i]].getLabel() + ': '+ current[fieldsArray[i]].getDisplayValue() + '\n';
  14.         }
  15.     }
  16.    
  17.     if(configExc){ //only create a config exception record if one of the managed attributes is changed
  18.         var type = previous.install_status == "" ? "new_ci" : "ci";
  19.         var gr = new GlideRecord('u_config_exceptions');
  20.         gr.initialize();
  21.         gr.u_ci_name = current.sys_id;
  22.         gr.u_type = type;
  23.         gr.u_old_values = oldValues;
  24.         gr.u_new_values = newValues;
  25.         gr.insert();
  26.     }
  27.    
  28. })(current, previous);
Add Comment
Please, Sign In to add comment