Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to edit 'Copy Change' UI Action

bhoomikabisht
Kilo Expert

Hi Experts,

I am working on Change management and want to modify 'Copy Change' UI Action

As per the requirement I want to copy two custom fields which i added added in the form. But I am not getting where to add the code in the UI Action. I checked the script include ChangeUtils. But not getting it.

Please help where i can modify.

Below is the UI Action:

function OnCopyChangeClick() {

  function addParam(form, name, val) {

  var inp = cel('textarea', form);

  inp.name = name;

  inp.value = val;

  }

  var srcSysId = g_form.getUniqueValue();

  var ga = new GlideAjax('ChangeUtils');

  ga.addParam('sysparm_name', 'getChangeQueryParams');

  ga.addParam('sysparm_src_sysid', srcSysId);

  ga.setWantSessionMessages(true);

  ga.getXMLAnswer(function (queryParam) {

  if (queryParam) {

  var gotoUrl = [];

  gotoUrl.push('srcSysID=' + srcSysId);

  gotoUrl.push('newSysID=$sys_id');

  gotoUrl.push('sysparm_returned_action=$action');

  gotoUrl = 'CopyChangeRelatedLists.do?' + gotoUrl.join('&');

  var form = cel('form', document.body);

  hide(form);

  form.method = "POST";

  form.action = g_form.getTableName() + ".do";

  if (typeof g_ck != 'undefined' && g_ck != "")

  addParam(form, 'sysparm_ck', g_ck);

  addParam(form, 'sys_id', '-1');

  addParam(form, 'sysparm_query', queryParam);

  addParam(form, 'sysparm_goto_url', gotoUrl);

  form.submit();

  }

  });

}

Below is the Script Includes:

var ChangeUtils = Class.create();

ChangeUtils.prototype = Object.extendsObject(ChangeUtilsSNC, {

  initialize: function(request, responseXML, gc) {

  ChangeUtilsSNC.prototype.initialize.call(this, request, responseXML, gc);

  },

  /***************************

  *

  *Add customer changes below

  *

  ****************************/

  type: 'ChangeUtils'

});

/********************************************************************

*The function below is written in this way to provide access via the

*signature ChangeUtils.isCopyChangeEnabled() from UI Action -

*'Copy Change' > Condition.

*

*Customers are suggested to override methods inside the body of

*ChangeUtils' object definition.

********************************************************************/

ChangeUtils.isCopyChangeEnabled = function(current) {

  var changeUtils = new ChangeUtils();

  if (changeUtils.isCopyFlagValid() && changeUtils.isCopyRulesValid(current)) {

  return true;

  } else {

  return false;

  }

};

Thanks & Regards,

Bhoomika Bisht

14 REPLIES 14

harishdasari
Tera Guru

Hi Bhoomika,



In Client script side :



var ga = new GlideAjax('ChangeUtils'); // ChangeUtils is your script include name


ga.addParam('sysparm_name', 'getChangeQueryParams'); // getChangeQueryParams is function name


  ga.addParam('sysparm_src_sysid', srcSysId);


---you can add your code here like this ---


ga.addParam('sysparm_newvalue', your field name);


ga.addParam(sysparm_newvalue1', your field name 1);


  ga.setWantSessionMessages(true);


  ga.getXMLAnswer(function (queryParam) {


  if (queryParam) {


  var gotoUrl = [];


  gotoUrl.push('srcSysID=' + srcSysId);


  gotoUrl.push('your field name=' + serversidevalue);


gotoUrl.push('your field name=' + serversidevalue 1);


  gotoUrl.push('newSysID=$sys_id');


  gotoUrl.push('sysparm_returned_action=$action');


  gotoUrl = 'CopyChangeRelatedLists.do?' + gotoUrl.join('&');



  var form = cel('form', document.body);


  hide(form);


  form.method = "POST";


  form.action = g_form.getTableName() + ".do";


  if (typeof g_ck != 'undefined' && g_ck != "")


  addParam(form, 'sysparm_ck', g_ck);


  addParam(form, 'sys_id', '-1');


  addParam(form, 'sysparm_query', queryParam);


  addParam(form, 'sysparm_goto_url', gotoUrl);


  form.submit();


  }


  });


}






Thanks.



Hope this might be helpfull. Please correct me if I am wrong


Already checked that and have given the code above.


Hi Bhoomika,



I needed to add a comment in 'work notes' to say where the copied change is originating from and setting a custom field to a specific value.


Below coding in bold did do it for me 🙂



---------------------------------------------------------------------------------------------------------------


function OnCopyChangeClick() {


    var changeID = g_form.getValue('number');


      var param1 = '^u_retrospective_change=no';


      var param2 = '^work_notes=This Change duplicated from: '+ changeID;


     


      function addParam(form, name, val) {


              var inp = cel('textarea', form);


              inp.name = name;


              inp.value = val;


      }


     


      var srcSysId = g_form.getUniqueValue();


     


      var ga = new GlideAjax('ChangeUtils');


      ga.addParam('sysparm_name', 'getChangeQueryParams');


      ga.addParam('sysparm_src_sysid', srcSysId);


      ga.setWantSessionMessages(true);


      ga.getXMLAnswer(function (queryParam) {


              if (queryParam) {


                      var gotoUrl = [];


                      gotoUrl.push('srcSysID=' + srcSysId);


                      gotoUrl.push('newSysID=$sys_id');


                      gotoUrl.push('sysparm_returned_action=$action');


                      gotoUrl = 'CopyChangeRelatedLists.do?' + gotoUrl.join('&');


                     


                      var form = cel('form', document.body);


                      hide(form);


                      form.method = "POST";


                              form.action = g_form.getTableName() + ".do";


                              if (typeof g_ck != 'undefined' && g_ck != "")


                              addParam(form, 'sysparm_ck', g_ck);


                      addParam(form, 'sys_id', '-1');


                    addParam(form, 'sysparm_query', queryParam + param1 + param2);


                      addParam(form, 'sysparm_goto_url', gotoUrl);


                      form.submit();


              }


      });


}


---------------------------------------------------------------------------------------------------------------


Absolutely brilliant.  My brain was bleeding after spending hours on this.  I was in the right area and had the right idea but could not implement it.  Thank you so much for this.