Relate Changes List

Sam Ogden
Tera Guru

Hi All,

On our change form we current have a related list for Change Requests.

This however creates a parent child relationship.   On change team do not use parent and child change requests, but they still want the ability to add changes to a related list so you can link from one change to another.   If a change is added to the related list it needs to be visible in both the change records related list (currently the child records only show in the parent records related list).

Do I just need to create a new M2M table between change requests and change request?

Thanks

Sam

1 ACCEPTED SOLUTION

gaynorg
Giga Contributor

Hi Sam,


it is possible to link the same table to itself, you just need to add in two business rules to keep the records in sync (two records need to exist for each entry to enable users to see the many to many link from both records)


Here is an example of a Change M2M



  • Navigate to sys_m2m.list and create a new table Many to Many table, referencing Change <> Change but do not save straight away. On creation
    • From Table:                     Change Request
    • To Table:                             Change Request
    • Many to Many:         creates u_m2m_change_reque_change_reque but rename
    • M2M from field:     change_request
    • M2M from label:     Change Requests
    • M2M to field:               change_request
    • M2M to Label:           Change Requests
  • Then amend the Many to Many table name to less characters and more meaningful and easy to remember e.g.
    • Many to Many:         u_m2m_change_change
    • M2M from field:       change
    • M2M from label:     Change
    • M2M to field:               linked_change
    • M2M to Label:           Linked Change

Then Save Record / create new m2m table


  • Navigate to new table u_m2m_change_change.do
  • Both fields are named 'Change Request', so right hand click on Label and determine which is u_change and which is u_linked_change and amend labels accordingly


  • Create a Business rule to insert a corresponding record e.g.   When a Change is added to the Related List e.g.   CHG0000020 is added to CHG0000004 a record is created Linked Change = CHG0000004 and Change = CHG0000020, this rule creates a corresponding record where Linked Change = CHG0000020 and Change = CHG0000004, to the Link can be viewed from either record
  • Example:
    • Name:             Sync Linked Changes on Insert
    • Active:             true
    • Advanced: true
    • When to run:
      • When:             after
      • Order:               100
      • Insert:               true
      • Update:         false
      • Delete:           false
      • Query:             false
    • Actions:-
      • None
    • Advanced:
      • Condition:   none
      • Script:

(function executeRule(current, previous /*null when async*/) {


       


InsertLinkedChg();


       


function InsertLinkedChg() {


                               


//Check for an opposite corresponding record


          var gr = new GlideRecord('u_m2m_change_change');


          //Where Linked Change = Change


          gr.addQuery('u_linked_change', current.u_change);


          //Where Change Linked Change


          gr.addQuery('u_change', current.u_linked_change);


          gr.query();


          if(!gr.next()){


          //gs.log("Change is   "+ current.u_change.getDisplayValue());


          //gs.log("Linked is   "+ current.u_linked_change.getDisplayValue());


          //If no record is found, then create a new record


                                  gr.initialize();


                                  gr.u_linked_change = current.u_change;


                                  gr.u_change = current.u_linked_change;


                                  gr.insert();          


                                  }


          }


       


})(current, previous);




  • Create a Business rule to delete the corresponding record
  • Example
    • Name:             Sync Linked Changes on Delete
    • Active:             true
    • Advanced: true
    • When to run:
      • When:             before
      • Order:               100
      • Insert:               false
      • Update:         false
      • Delete:           true
      • Query:             false
    • Actions:-
      • None
    • Advanced:
      • Condition:   none
      • Script:

(function executeRule(current, previous /*null when async*/) {


       


DeleteLinkedChg();


       


//Delete the corresponding record for the 'Linked Change' where the link to the 'Change' has been removed or vice versa


function DeleteLinkedChg() {



//Check for the corresponding record which should already exist in the m2m table


          var gr = new GlideRecord('u_m2m_change_change');


          //Existing record Change = Linked Change Number e.g.CHG4


          gr.addQuery('u_change', current.u_linked_change);


          //Existing record Linked Change = Change Number e.g.CH20


          gr.addQuery('u_linked_change', current.change);


          gr.query();


          if(gr.next()){


          //Record found, REMOVE corresponding record from the M2M table


                                  //gs.log("deletion for Linked " + linked + " & Change " + chg);                


          gr.deleteRecord();


                                                       


          //Record not found, do nothing


                                  } else {


                                                       


                                  }


          }


       


})(current, previous);




  • On the Change form, add the related list 'Linked" and Configure List Layout adding the fields required
  • Configure the List Control and add
  • Label 'Linked Changes' and changed 'Omit new button' from false to true
  • 1. M2M Initial New Screen.png2. M2M Revised Naming New Screen.png3. New Record.png4. Relabel fields.png

View solution in original post

15 REPLIES 15

Hi Gaynor,



Think I have spotted the issue.   This line in the delete business rule:



gr.addQuery('u_linked_change', current.change);



should be:



gr.addQuery('u_linked_change', current.u_change);



All seems to be working now.



Thanks for all your help.


Kevin Recio1
Mega Contributor

Is there a role created when you apply this business rule? I was able to create the table and the business rules, but I noticed that only admins are able to view linked changes or add/edit linked changes. I added our itil role for all list functions but still nothing. Do I have to commit the update set before it takes effect even though I can at least see the tab while impersonating?

Hi Kevin,

From memory I ran into the same issue.  As there were no set ACLs it was hitting the default *.* ACL so I had to create some new ACLS for that table to allow access to the required users - in our example we create create, delete read and write ACLs for ITIL

Thanks samogden. I believe I have the ACLs needed now, however I still can't use the new linked change tab:

find_real_file.png

Am I missing a step? Sorry if this should be common knowledge.

Hi

You need to create ACLs on the 'u_m2m_change_change' table for read, write, create etc and add 'requires role' itil and then the non admin users will be able to view and update the linked changes