Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove User Criteria from Canread on Knowledge block

Michael51
Tera Guru

hello All,

we need to Remove an XYZ criteria from knowledge block in Bulk , if we have 4 to 5 criteria's to single block we need to Just remove XYZ alone and we need to keep other Criterias as it is 

 I have used this logic but it is removing all criteria's 

 

var gr = new GlideRecord('kb_knowledge_block');
gr.addEncodedQuery('kb_knowledge_base=ab0372fb1b668914619fed79b04bcba6^can_read_user_criteriaLIKE070120ae1b35695072e6eb99bd4bcb2a');
gr.query();

while (gr.next()) {
    gs.info(gr.getRowCount());
    gr.setValue('can_read_user_criteria','')
    gr.update();
}

 

@Ankur Bawiskar @Sandeep Rajput @jaheerhattiwale @Pavankumar_1 

 

can you please correct the script just remove only xyz from block 

TIA

1 ACCEPTED SOLUTION

@Michael51 

update as this

var gr = new GlideRecord('kb_knowledge_block');
gr.addEncodedQuery('kb_knowledge_base=ab0372fb1b668914619fed79b04bcba6^can_read_user_criteriaLIKE070120ae1b35695072e6eb99bd4bcb2a');
gr.query();

while (gr.next()) {
	var arr = gr.can_read_user_criteria.toString().split(',');
	var  index = arr.indexOf('070120ae1b35695072e6eb99bd4bcb2a');
	if (index > -1) { // only splice array when item is found
		arr.splice(index, 1); // 2nd parameter means remove one item only
	}

	gr.setValue('can_read_user_criteria', arr.toString());
	gr.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael51 

you can simply get all the values, convert it to array and then slice it and remove that sysId and then update it again

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

@Ankur Bawiskar can i have script for this Ankur

@Michael51 

try this

var gr = new GlideRecord('kb_knowledge_block');
gr.addEncodedQuery('kb_knowledge_base=ab0372fb1b668914619fed79b04bcba6^can_read_user_criteriaLIKE070120ae1b35695072e6eb99bd4bcb2a');
gr.query();

while (gr.next()) {
	var arr = gr.can_read_user_criteria.toString().split(',');
	var  index = arr.indexOf('070120ae1b35695072e6eb99bd4bcb2a');
	if (index > -1) { // only splice array when item is found
		arr = arr.splice(index, 1); // 2nd parameter means remove one item only
	}

	gr.setValue('can_read_user_criteria', arr.toString());
	gr.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar  , this logic is removing other criteria's and keeping only the criteria which I need to remove actually 

My requirement is to Just XYZ criteria and keep the other criteria's as it is

@Michael51 

update as this

var gr = new GlideRecord('kb_knowledge_block');
gr.addEncodedQuery('kb_knowledge_base=ab0372fb1b668914619fed79b04bcba6^can_read_user_criteriaLIKE070120ae1b35695072e6eb99bd4bcb2a');
gr.query();

while (gr.next()) {
	var arr = gr.can_read_user_criteria.toString().split(',');
	var  index = arr.indexOf('070120ae1b35695072e6eb99bd4bcb2a');
	if (index > -1) { // only splice array when item is found
		arr.splice(index, 1); // 2nd parameter means remove one item only
	}

	gr.setValue('can_read_user_criteria', arr.toString());
	gr.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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