- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2015 12:08 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2015 04:14 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2015 04:14 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 06:35 AM
Hi Frank
I really appreciate your assistance. We may be getting closer, but so far no luck.
Unfortunately, even with the new related lists added to the catalog task and change request forms, they are not being populated.
The other minor item is that we would like to be directed to the new change request record, so all of the mandatory fields can be completed for submission. Currently when clicking the Create Normal Change UI Action, I'm returned to the catalog task list from which I initially open the catalog task. I'm also not seeing the info message with the change number in it, even though the change request record is successfully being created.
Ideally we would like the capability to link multiple catalog tasks to multiple change requests and what we were hoping the m2m definition would provide.
I've triple checked that I have the correct name for the m2m table and fields. Cleared both browser cache and SN cache. Added the parent field to the change request form to see what it contains (it is always blank now).
Here is the current script for the UI Action:
//current.update();
var catalogTaskSysID = current.sys_id;
var change = new GlideRecord("change_request");
change.type = 'Normal';
change.short_description = current.short_description;
var chgReqSysId = change.insert();
//populate m2m table
var grCatTaskChgReq = new GlideRecord("u_m2m_catalog_task_change_reque");
grcatTaskChgReq.initialize();
grCatTaskChgReq.u_catalog_task = catalogTaskSysID;
grCatTaskChgReq.u_change_request = chgReqSysId;
grCatTaskChgReq.insert();
current.update();
gs.addInfoMessage("Change " + change.number + " created");
action.setRedirectURL(change);
action.setReturnURL(current);
Here is a screen capture of the m2m definition:
According to the article I quoted in my previous post, it should not matter which way the m2m definition is created (change request>catalog task or catalog task>change request).
What might I still be missing?
Many thanks
Ron
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 07:03 AM
Hi Ron,
you can pretty much forget about the "Parent" field - in your use case it is actually completely irrelevant as your data model says there is an M2M relation between those tables. Reference fields are always used for 1-n relationships which are being established by one of the tables (e.g. "Change Request" or "Caused by Change" on the Incident table") - on the Change Request table you will find two related lists "Incident -> Change Request" and "Incident -> Caused by Change" for those relationships.
When having an M2M relation you can use the "Edit" button on the related list on both sides. A potential use case could be to create a change from the catalog task and then use the "Edit" button on the Change Request to link more Catalog Tasks to this Change Request. On the other side you could then create another Change Request from the same Catalog Task or even link an already existing Change Request to the Catalog Task - et voila, many-to-many. 🙂
The script seems to be fine - can you attach a screenshot of your Change Request form as well of your Catalog Task form (both showing the related list that you're using)?
Are you sure you added the new M2M related lists to both of the forms and not of the 1-n related lists? They should be named "Catalog Tasks" if you modify your Change Request related lists and "Change Requests" on the Catalog Task related lists.
As a side note: what is the action name of your UI Action and does the UI Action reside on Catalog Task?
Cheers
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 07:33 AM
Change request form:
Catalog task related list configuration on the change request:
Catalog task form:
Change request related list configuration on the catalog task:
Create Normal Change UI Action on catalog task table:
The other question is "will the 2 related lists" keep in sync and update". If I used the Edit button on the catalog tasks related list on the change request to add another related catalog task and I went to that catalog task, would I see the change request appear in the change requests related list? Conversely, if I used the edit button on the change requests related list of a catalog task to add multiple change requests, would the catalog tasks related list on those change requests be automatically populated? If not, then I'm guessing we'd need a business rule to perform the sync.
Again, many thanks for your assistance.
Cheers
Ron