Many to Many on same table?

Nate23
Mega Guru

Hello All,

 

I've been put to the task of trying to create a Many-to-many relationship on the Change Request table. We sort of have it working but not to our liking. We created another table to associate the changes with each other. but if you go on one change request and show the related list for the Change Request table it will show the related change requests like it should. However, you click on one of the related change requests to view it and it shows the relation on the other table we created not the Change Request table.

 

We want it to be consistent on one table not two. The attached link shows the related lists on two different Change Requests. Notice that the table names are different.

 

Any help would be appreciated.

 

Thanks,

 

Nathan G.

1 ACCEPTED SOLUTION

Hello,



I ended up creating a list type field on the change table. From there I created a relationship(System Definition>Relationships) From the Change Req table to the Change Req table with the following query:


current.addQuery('sys_idIN' + parent.u_related_changes);


Then I overwrote the save and update functions for when they edited the list to query the change table and add the current change to the associated changes added or updated. as well as removed them with this code:


var gr = new GlideRecord('change_request');


gr.addQuery('sys_id','IN' , current.u_related_changes);


gr.query();


/*


gs.addInfoMessage('Current Sys ID: ' + current.sys_id);


gs.addInfoMessage('selected Sys ID: ' + current.u_related_changes);*/




while(gr.next()){


  if(gr.u_related_changes == ''){


// gs.addInfoMessage('No Recored so the parent Child is entered1');


  gr.u_related_changes =   current.sys_id;


  }else if(gr.u_related_changes.indexOf(current.sys_id) == -1){


// gs.addInfoMessage('No Recored so the parent Child is entered2');


  gr.u_related_changes = gr.u_related_changes + ',' + current.sys_id;


  }


  gr.update();


}


action.setRedirectURL(current);


current.update();






Hope this helps


View solution in original post

16 REPLIES 16

I forgot I added this onChange client script to remove the change from other if it was removed from the list field:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading) {


              return;


      }


     


      var old = oldValue;


      var val = newValue;


      var seperate = old.split(',');


      var filterString = '';


     


      for(var i = 0; i < seperate.length; i++){


             


              if(val.indexOf(seperate[i]) == -1){


                      if(filterString == '')


                              filterString = filterString + seperate[i];


                      else


                              filterString = filterString + ',' + seperate[i];


                      //alert('this was removed: ' + seperate[i]);


              }


             


      }


  //alert(filterString);


     


      if(filterString != ''){


              var ga = new GlideAjax('RemoveChangeM2M');


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


              ga.addParam('sysparm_filter',filterString);


  ga.addParam('sysparm_number',g_form.getValue('number'));


              ga.getXML(HelloWorldParse);


             


           


      }


        alert('Be sure to always save or update after changing this field');


     


     


}




function HelloWorldParse(response) {


                      var answer = response.responseXML.documentElement.getAttribute("answer");


                    // alert(answer);


              }


and it references this script include:



var RemoveChangeM2M = Class.create();


RemoveChangeM2M.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      removeChangeM2M: function() {


              var filterString = this.getParameter('sysparm_filter');


              var number = this.getParameter('sysparm_number');


              var test = '';


              var test2 = '';


              var hold = '';


              try{


                      var record = new GlideRecord('change_request');


                      record.addQuery('number', number);


                      record.query();


                      if(record.next())


                              test = record.sys_id;


                     


                     


                      var gr = new GlideRecord('change_request');


                      gr.addQuery('sys_idIN' + filterString);


                      gr.query();


                      while(gr.next()){


                              hold = gr.u_related_changes;


                             




                              if(hold.indexOf(','+test) != -1){


                                      gr.u_related_changes = hold.replace(','+test, "");


  hold = hold.replace(','+test, "");


                                      test2 = 'with comma';



                              }else   if(hold.indexOf(test) != -1){


                                      gr.u_related_changes = hold.replace(test, "");


                                      test2 = 'no comma';


                              }


                             


                             


                             


                             


                             


                             


                             


                             


                              gr.update();


                      }


              }catch(e){}


             


              return test2;


      },


     


      _privateFunction: function() { // this function is not client callable


     


}




});



Hi Nathan,



did you manage to add   the Edit Button in a customized relationship?



Thanks


This was a while back for an old client. I dont think we had the "edit" button on the related list, because the related list was populated by the list collector variable.


I honestly would not use this method anymore they are better ways lol.


smr_itsm
Kilo Contributor

I'm trying to do something similar with Knowledge articles, what do you think is a better way?