Populate reference field from list collector

Hervi
Tera Contributor

Hi Team,

 

I am working on portal end. I am trying to populate type field with my reference value when list collector "Parent" contains RITM type tickets.

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var list = g_form.getDisplayValue('parent').toString();
    var array = list.split(',');
	alert("list is " +array);
    for (var i = 0; i < array.length; i++) {
        if (array[i].includes('RITM')) {
            g_form.setValue('type', 'Enhancement::Planned Change');
			alert("set type field for ritm");
           // return;
        } else if (array[i].includes('PRB')) {
            g_form.setValue('type', 'HI22');
           // return;
        }
    }


}

 

 

 

 Can you please let me know what is the issue with this script? i am writing an onchange catalog client script. 

@Ankur Bawiskar 

@Tai Vu 

@Anil Lande 

 

1 ACCEPTED SOLUTION

Hi @Hervi 

I've checked around your script, you can find my observation below.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var list = g_form.getDisplayValue('parent').toString(); //getDisplayValue won't work in servicecatalog_cat_item_view within platform view.
    var array = list.split(',');
    for (var i = 0; i < 1; i++) { //loop only the first element => i < array.length
        if (array[i].includes('RITM')) {
            var b = g_form.setValue('type', '152c8359375293402c4a005a54990e56', 'Enhancement::Planned Change'); 
            break; //not required
        } else if (array[i].includes("PRB")) {
            g_form.setValue('type', 'HI22');
            break; //not required
        }

		//What if the parnet list collector contains both RITM and PRB?
    }

}

 

 

You can consider to pass the list collector to an Ajax script include to do your validation, then return the Type accordingly.

Sample

Script Include

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

    getTypeByTaskID: function() {
        var taskIDs = this.getParameter('sysparm_parent');
        var classes = [];
        var grTask = new GlideRecord('task');
        grTask.addQuery('sys_id', 'IN', taskIDs);
        grTask.query();
        while (grTask.next()) {
            var className = grTask.getRecordClassName();
            if (classes.indexOf(className) === -1) {
                classes.push(className);
            }
        }

		var type = {};
		if(classes.indexOf('sc_req_item') >= 0){
			//try to avoid hard-coding by storing sys_id in a system property
			type.sys_id = '152c8359375293402c4a005a54990e56'; 
			type.name = 'Enhancement::Planned Change';
		}
		if(classes.indexOf('problem') >= 0){
			//try to avoid hard-coding by storing sys_id in a system property
			type.sys_id = '152c8359375293402c4a005a54990e52'; //replace the HI22's sys_id record
			type.name = 'HI22';
		}

		//What if the parent list collector contains both RITM and PRB?
		return JSON.stringify(type);

    },

    type: 'CLCatalogItemUtilAJAX'
});

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

	var ga = new GlideAjax('CLCatalogItemUtilAJAX');
	ga.addParam('sysparm_name', 'getTypeByTaskID');
	ga.addParam('sysparm_parent', newValue);
	ga.getXMLAnswer(function(answer){
		var type = JSON.parse(answer);
		if(type && type.sys_id != undefined){
			g_form.setValue('type', type.sys_id, type.name);
		}
	});

}

 

Just keep in mind that there's a scenario the parent can have both RITM and PRB because it's a list collector.
 
Cheers,
Tai Vu

View solution in original post

19 REPLIES 19

I think i figured out the issue , But i still dont know how to fix it.

My type field is basically a calculated field from task_rel_type table ..i am using this as a reference in my code. as per servicenow..this is an issue

Calculated display value is not showing the correct calculated value on a reference field - Known Er...

AshishKM
Kilo Patron
Kilo Patron

Refer @Nick Parsons explanation, the for loop will set the type value every time the list collector updated.

so the type will be finally set as per the last record.  

Code is perfectly working fine and type is updating on every update on list collector, i checked at my end. 

 

Your question states that , list collector contains RITM type records then why you are checking for PRB.

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hervi
Tera Contributor

thats my additional requirement. if it runs for ritm i will make it run for prb as well

Hervi
Tera Contributor

FYI - it is working on native view but not on portal view

Hi @Hervi 

Make sure the UI Type of your client script is set to All.

Timi_0-1706760613077.png

 

Cheers,

Tai Vu