create change request from catalog task

rlehmann
Kilo Sage

I've been thru a number of articles in the community, the SN guru page and reviewed the wiki for solutions, but cannot find what I am missing.

I created a new m2m definition for catalog task to change request. (Creating a Many-to-Many relationship in ServiceNow - ServiceNow Guru)

Created a UI action on catalog task (sc_task) to create a normal change request (we use normal instead of routine for our change type).

The UI action only appears as a form context menu.

Condition is: current.active == true && (gs.hasRole('itil')) || gs.hasRole('admin')

Script:

current.update(); //Save any updates to the current catalog task before proceeding

var parentid = current.sys_id;

var change = new GlideRecord("change_request");

change.type = 'Normal';

change.short_description = current.short_description;

change.parent = parentid;

var sysID = change.insert();  

var mySysID = current.update();  

gs.addInfoMessage("Change " + change.number + " created");

action.setRedirectURL(change);  

action.setReturnURL(current);

I have the new m2m definition catalog tasks as a related list on the change request form and the change requests related list on the catalog task form.

When utilizing the create change UI action on the catalog task, the change request number appears in the related list on the catalog task, but the catalog task number does not appear in the related list on the change request. The ultimate goal is to have the creation of the change request via the ui action, have the related catalog task(s) appear in the related list on the change request and the change request(s) appear in the related list on the catalog task.

We are currently on Fuji Patch 8.

If anyone has suggestions as to what I may be missing, I would really appreciate some guidance.

Many thanks.

Ron

1 ACCEPTED SOLUTION

fschuster
ServiceNow Employee
ServiceNow Employee

Hi Ron,



there are a couple of things worth mentioning.


You said you have created a new M2M table between Change Request and Catalog Task - but in your script you are not utilizing it so it will be completely empty.


What you do is create a Change Request and fill the "Parent" field of the change Request with the sys_id of the Catalog Task which is the representation of a 1-n relationship (only 1 Catalog Task can be in that Parent field at a time). Therefore you'll only have the related list populated on the Catalog Task   by populating the "Parent" field on the CR level.



I would assume that on your Catalog Task you are displaying the related list "Change Request -> Parent" and not your newly created M2M - as stated before this should be completely empty because you didn't fill it with data. So first thing is to get the M2M table related lists on both tables.



Next thing is to create the Change Request as you already do and also populate your M2M table right away - this could look like this:



//current.update(); //Save any updates to the current catalog task before proceeding


//current.update at the beginning shouldn't be necessary as you're not leaving the record - so one time at the end should be sufficient



var catalogTaskSysID = current.sys_id;



var change = new GlideRecord("change_request");


change.type = 'Normal';


change.short_description = current.short_description;


//change.parent = parentid;   //we don't need this anymore.


var chgReqSysId = change.insert();



//populate m2m table


var grCatTaskChgReq = new GlideRecord("your_m2m_table_name_here");


grCatTaskChgReq.initialize();


grCatTaskChgReq.catTaskRefFieldName = catalogTaskSysID;


grCatTaskChgReq.chgReqRefFieldName = chgReqSysId;


grCatTaskChgReq.insert();



current.update();


gs.addInfoMessage("Change " + change.number + " created");



action.setRedirectURL(change);


action.setReturnURL(current);



Let me know if that helped - if it doesn't I'm happy to jump on a quick call tomorrow if you like.



Regards


Frank


View solution in original post

7 REPLIES 7

fschuster
ServiceNow Employee
ServiceNow Employee

Hi Ron,



both related lists point to the same M2M table - so in short: the answer is yes, this will be in sync.


Just add some records to the M2M table by using the "Edit" button (doesn't matter from which table) and click on the little "information" ( ) symbol on the related list - it will lead you to the record that has been created within the M2M table.



Two things:


1. Correct the GlideRecord variable for populating the M2M table in line 2 where it says initialize (grcatTaskChgReq -> grCatTaskChgReq).


2. Provide an action name for the UI Action (e.g. "create_change")



Let me know if that helped and it works afterwards.


Hi Frank



Thank you so very much for your assistance.


Everything is now working as intended.


Correcting the typo grcatTaskChgReq and adding an action name for the UI Action were the final issues.


I greatly appreciate your efforts.



Cheers


Ron


fschuster
ServiceNow Employee
ServiceNow Employee

Hi Ron,



Happy to hear that!


Glad it works



Frank