How to Remove Value from field

chanikya
Tera Guru

Hi All,

kindly help me on below one

there are two List type fields PR Parent, PR Child

if Parent value removed from PR Parent then that Parent related Child values should be removed from PR Child.

find_real_file.png

 

PR Details table designed like this.

find_real_file.png

17 REPLIES 17

Hi,

So AAAAA is child of A and BBBBB  is child of B

you can use before update BR on that table and check whether the value present in Child field are child of the values present in Parent field.

if not then remove; if yes then keep

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

SumanthDosapati
Mega Sage
Mega Sage

Hi Chanikya,

To achieve that, you need to write a client script and call a script include via glideAjax.

 

Write below onChange client script on parent PR :

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

	var ga = new GlideAjax('script_include_name'); //client callable script include name
	ga.addParam('sysparm_name', 'getDetails');
	ga.addParam('sysparm_par', newValue.toString());
	ga.getXMLAnswer(callback);

	function callback(response)
	{
		g_form.setValue('pr_child', response);
	}
   
}

 

Client callable script include as below :

getDetails: function()
{
var child = [];
var par = this.getParameter('sysparm_par');
        var gr = new GlideRecord('pr_details_table'); //update table name 
	gr.addEncodedQuery("u_parentIN"+ par); //update parent field name as "field_nameIN";
	gr.query();
	while(gr.next())
		{
			childs.push(gr.sys_id);
		}
var childs_string = childs.join(",");
return childs_string;
},

 

With this, when a pr parent is added, pr child will be automatically added. and if pr parent is removed, its respective pr child is removed.

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

Hi Sumanth,

Thanks for reply.

this what i m looking for . but here I don't want to add automatically . Because depending on Parent there may be huge child records. So i don't want to add automatically those huge childs into PR Child list.

I just want to remove childs from PR child list field if any child records already added in PR child with respective of Parent .

Hey Chanikya,

We can just see the value got changed or not but cannot determine if added or removed.

I just wrote code with weird logic. You can try this

 

Write below onChange client script on parent PR :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var old_array = oldValue.toString().split(',');
    var new_array = newValue.toString().split(',');
    var new_array2 = new_array;
    var old_count = old_array.length;
    var new_count = new_array.length;
    var unique_array = [];
    if (old_count > new_count) //it means values are removed
    {
        for (var i = 0; i < old_count; i++) {
            if (new_array.indexOf(old_array[i]) == '-1') {
                unique_array.push(old_array[i]);
            }
        }
        var uns = unique_array.join(',');
        var ga = new GlideAjax('script_include_name'); //client callable script include name
        ga.addParam('sysparm_name', 'getDetails');
        ga.addParam('sysparm_par', uns);
        ga.getXMLAnswer(callback);

        function callback(response) {
            var all_child_array = response.split(',');
            for (var i = 0; i < new_count; i++) {
                if (all_child_array.indexOf(new_array[i]) != '-1') {
                    var ind = new_array2.indexOf(new_array[i]);
                    new_array2.splice(ind, 1);
                }
            }

            g_form.setValue('pr_child', new_array2.join(','));
        }

    }

}

 

Client callable script include as below :

getDetails: function()
{
var child = [];
var par = this.getParameter('sysparm_par');
        var gr = new GlideRecord('pr_details_table'); //update table name 
	gr.addEncodedQuery("u_parentIN"+ par); //update parent field name as "field_nameIN";
	gr.query();
	while(gr.next())
		{
			childs.push(gr.sys_id);
		}
var childs_string = childs.join(",");
return childs_string;
},

 

Mark as correct and helpful if this solved your query.

Regards,
Sumanth

@chanikya 

Feel free to reach out if you have further questions or else you can mark an answer as correct and helpful to close the thread so that it benefits future visitors also.

Regards,
Sumanth