Script Help ! Add record to a related list through a UI action !partial script not working

Servicenow lear
Tera Contributor

Hello All,

We have a requirement :

Requirement >> When a Ui action is clicked on sn_si_task (security task)it opens sn_ti_observable (observable table) table form in glide window , once the record is submitted that record is added to the related list sn_ti_m2m_task_observable (m2m related list) on sn_si_incident (security inicdent)table.

 

I have written a UI action glide window call, created a custom field on observable table to capture the sysid from sn_si_task table and a Script include to capture the sysid of new record and task record. 

 

ISSUE : my logic of adding the record to related list is not working.

My code below :

 

UI action :

 

function openForm() {

    var tableName = 'sn_ti_observable';
    var sysID = -1;
    var incSysid = g_form.getUniqueValue();
    alert(incSysid);



    //Create and open the dialog form
    var dialog = new GlideDialogForm('Open Form', tableName, dothis); //Provide dialog title and table name
    dialog.setSysID(sysID); //Pass in sys_id to edit existing record, -1 to create new record
    dialog.addParm('sysparm_view', 'default'); //Specify a form view
    dialog.addParm('sn_ti_observable', 'true'); //Add or remove related lists
    dialog.setLoadCallback(function(iframeDoc) {
        // To get the iframe: document.defaultView in non-IE, document.parentWindow in IE
        var dialogFrame = 'defaultView' in iframeDoc ? iframeDoc.defaultView : iframeDoc.parentWindow;
        dialogFrame.g_form.setValue('u_sit_id', incSysid);   //custom field on observable table
        dialogFrame = null;
    });
    dialog.render(); //Open the dialog


}

function dothis(action, sys_id, table, displayValue) {
    //g_form.setValue('caller_id', sys_id);
    var observeId = sys_id;
    var incSysid2 = g_form.getUniqueValue();

    var createRel = new GlideAjax('sn_si.Createrelationships');
    createRel.addParam('sysparm_name', 'createRelObservables');
    createRel.addParam('sysparm_secid', incSysid2);
    createRel.addParam('sysparm_obsid', observeId);
    createRel.getXML(callbackFunc);

function callbackFunc(response)
{
  var ans = response.responseXML.documentElement.getAttribute("answer")
	alert("answer is " + ans );
}
  



    alert(observeId + " " + incSysid2);
    // gs.getSession().putClientData('submittedid', observeId);
    // gsftSubmit(null, g_form.getFormElement(), "open_form");
}

 

 

SCRIPT INCLUDE :

 

var Createrelationships = Class.create();
Createrelationships.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	createRelObservables: function(){
   var secID = this.getParameter('sysparm_secid');
   var obsid = this.getParameter('sysparm_obsid');
   gs.info('ids are ' + secID + "::" + obsid);
   //return secID;
   var gr = new GlideRecord ("sn_ti_m2m_task_observable");
   gr.initialize();
   gr.observable=obsid;
   gr.task=secID;
   //gr.setValue("observable",obsid);
   //gr.setValue("task",secID);
   gr.insert();

   return secID;

	},

	

    type: 'Createrelationships'
});

 

 

1 ACCEPTED SOLUTION

IceIronDragon
Tera Guru

you do not need a custom field on the new record form.

 

you would need to get parent record sys id and created record sys id.

get it through a callback function.:

 

function openForm() {

    var tableName = 'sn_ti_observable';
    var sysID = -1;
    var incSysid = g_form.getUniqueValue();
    alert(incSysid);



    //Create and open the dialog form
    var dialog = new GlideDialogForm('Open Form', tableName, dothis); //Provide dialog title and table name
    dialog.setSysID(sysID); //Pass in sys_id to edit existing record, -1 to create new record
    dialog.addParm('sysparm_view''default'); //Specify a form view
    dialog.addParm('sn_ti_observable''true'); //Add or remove related lists
    dialog.setLoadCallback(function(iframeDoc) {
        // To get the iframe: document.defaultView in non-IE, document.parentWindow in IE
        var dialogFrame = 'defaultView' in iframeDoc ? iframeDoc.defaultView : iframeDoc.parentWindow;
        //dialogFrame.g_form.setValue('u_sit_id', incSysid);// u dont need this
        dialogFrame = null;
    });
    dialog.render(); //Open the dialog


}

function dothis(action, sys_id, table, displayValue) {
    //g_form.setValue('caller_id', sys_id);
    var observeId = sys_id;
    var incSysid2 = g_form.getValue('parent').toString();
    alert("parent is " + incSysid2);

    var createRel = new GlideAjax('sn_si.Createrelationships');
    createRel.addParam('sysparm_name''createRelObservables');
    createRel.addParam('sysparm_secid', incSysid2);
    createRel.addParam('sysparm_obsid', observeId);
    createRel.getXML(callbackFunc);

function callbackFunc(response)
{
  var ans = response.responseXML.documentElement.getAttribute("answer");
    alert("answer is " + ans );
}
  



    alert(observeId + " " + incSysid2);
    // gs.getSession().putClientData('submittedid', observeId);
    // gsftSubmit(null, g_form.getFormElement(), "open_form");
}

View solution in original post

3 REPLIES 3

Rahul Talreja
Mega Sage
Mega Sage

Hi @Servicenow lear ,
The ID's which you are Getting in 'ids are' are valid?

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Yes.

I am logging those ids as well through UI action. 

IceIronDragon
Tera Guru

you do not need a custom field on the new record form.

 

you would need to get parent record sys id and created record sys id.

get it through a callback function.:

 

function openForm() {

    var tableName = 'sn_ti_observable';
    var sysID = -1;
    var incSysid = g_form.getUniqueValue();
    alert(incSysid);



    //Create and open the dialog form
    var dialog = new GlideDialogForm('Open Form', tableName, dothis); //Provide dialog title and table name
    dialog.setSysID(sysID); //Pass in sys_id to edit existing record, -1 to create new record
    dialog.addParm('sysparm_view''default'); //Specify a form view
    dialog.addParm('sn_ti_observable''true'); //Add or remove related lists
    dialog.setLoadCallback(function(iframeDoc) {
        // To get the iframe: document.defaultView in non-IE, document.parentWindow in IE
        var dialogFrame = 'defaultView' in iframeDoc ? iframeDoc.defaultView : iframeDoc.parentWindow;
        //dialogFrame.g_form.setValue('u_sit_id', incSysid);// u dont need this
        dialogFrame = null;
    });
    dialog.render(); //Open the dialog


}

function dothis(action, sys_id, table, displayValue) {
    //g_form.setValue('caller_id', sys_id);
    var observeId = sys_id;
    var incSysid2 = g_form.getValue('parent').toString();
    alert("parent is " + incSysid2);

    var createRel = new GlideAjax('sn_si.Createrelationships');
    createRel.addParam('sysparm_name''createRelObservables');
    createRel.addParam('sysparm_secid', incSysid2);
    createRel.addParam('sysparm_obsid', observeId);
    createRel.getXML(callbackFunc);

function callbackFunc(response)
{
  var ans = response.responseXML.documentElement.getAttribute("answer");
    alert("answer is " + ans );
}
  



    alert(observeId + " " + incSysid2);
    // gs.getSession().putClientData('submittedid', observeId);
    // gsftSubmit(null, g_form.getFormElement(), "open_form");
}