My code works correctly with the list collector variables on the form but

Community Alums
Not applicable

Hi,

I have my code correctly working when the data is coming from the list collector variables on the sc_task form. Now the requirement has changed and the data should come from related list. Can you please help me in updating the code. Kindly help.

 

1.PNG

 

Client Script

 

2.PNG

 

function onSubmit() {

    //Type appropriate comment here, and begin script below

    var inc = g_form.getValue("incident.parent");

    var rq_item = g_form.getValue("sc_request.parent");

	var chg = g_form.getValue("change_request.parent");

    /*Inc & Ritm both*/

    var ga = new GlideAjax("listCollectorWNClass");

    ga.addParam("sysparm_name", "listCollectorIncRitmFunction");

    ga.addParam("sysparm_ritmId", rq_item);

	ga.addParam("sysparm_changeID", chg);

    ga.addParam("sysparm_incId", inc);

    ga.getXMLWait();

    /*Set values*/

    /*Put some alerts- info messages to check what you are receiving from script include*/

  //  g_form.addInfoMessage('Response=' + JSON.parse(ga.getAnswer()));

    var answer = JSON.parse(ga.getAnswer());

    var setValues = "Incident(s) :" + answer.incident.toString() + '\n' + "Service Request(s) :" + answer.ritm.toString() + '\n' + "Change Request(s) :" + answer.change.toString();

    g_form.setValue("work_notes", setValues);

}

 

Script Include

 

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

    listCollectorIncRitmFunction: function() {

        var incArr = this.getParameter('sysparm_incId').toString().split(',');

        var ritmArr = this.getParameter('sysparm_ritmId').toString().split(',');

         var changeArr = this.getParameter('sysparm_changeID').toString().split(',');

        var newIncArray = [];

        var newritmArray = [];

        var newChangeArr = [];

        var object = {

            incident: '',

            ritm: '',

            change: ''

        };



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

            var inc1 = new GlideRecord('incident');

            if (inc1.get(incArr[i].toString())) {

                newIncArray.push(inc1.number);

            }

        }

        object.incident = newIncArray.toString();


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

            var change1 = new GlideRecord('change_request');

            if (change1.get(changeArr[i].toString())) {

                newChangeArr.push(change1.number);

            }

        }

        object.change = newChangeArr.toString();

        gs.log('Suman', newChangeArr);


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

            var rq_itm = new GlideRecord('sc_req_item');

            if (rq_itm.get(ritmArr[i].toString())) {

                newritmArray.push(rq_itm.number);

            }

        }
        object.ritm = newritmArray.toString();
        return JSON.stringify(object);

    },

    type: 'listCollectorWNClass'
});

 

Now the code should be updated so that the data should come from incident, service requests, and change requests of Related list for sc_task table.

 

3.PNG

 

4.PNG

 

I will make Active is false for the list collectors

 

5.PNG

 

Can you please help me update the code so that when I select the incident, service requests, and change requests when selected, and saved, the selected record numbers will be on the work notes. Kindly help.

 

Regards

Suman P.

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

These are three out of box Related Lists that have a simple filter: parent = the current sctask record, so the client script will use 

g_form.getUniqueValue()
to send only the sys_id of the current record to the Script Include.  The Script Include will have the same 3 GlideRecords, just not inside for loops.  Each GR will have a line like
inc1.addQuery('parent', tsksysid);
or whatever you name the variable on the parameter received from the client.  Then in the while loop of the returned records, increment a counter - 3 unique script variables that you've previously declared and initialized.  Then add the three counts to the object to return to the client, if I'm following your requirements correctly.