The CreatorCon Call for Content is officially open! Get started here.

How to update a field of multiple records on the basis some records in array;

Not applicable
How to update a field of multiple records on the basis some records in array.
Scenario: There is array X and need to update a field of multiple records by inserting element from array X.
Example:
var x = 0;
var x2 = 3;
X = [ "xyz", "abc", "def", "mno" ];

        for (x; x<=x2; x++) {    
        var obj6 = new GlideRecord('incident');
        obj6.addEncodedQuery('active=true^caller_id=46d44a23a9fe19810012d100cca80666');
        obj6.query();
        while(obj6.next())
        {
         obj6.short_description = X;
        obj6.update();  
        }      
        }



Expected output: As per the above script, the short description of 4 Incident records should be updated as
xyz in 1st Incident, abc in 2nd incident, def in 3rd incident and mno in 4th incident.

I used above code but it is not working properly.
Any idea how to achieve this sol?



8 REPLIES 8

Murthy Ch
Giga Sage

Hi 

Try this:

var x = 1;
var x2 = 3;
var arr = [ "xyz", "abc", "def", "mno" ];  //changed here

        for (var i=0;i<arr.length;i++) {    
        var obj6 = new GlideRecord('incident');
        obj6.addEncodedQuery('active=true^caller_id=46d44a23a9fe19810012d100cca80666');
        obj6.query();
        while(obj6.next())
        {
         obj6.short_description = arr[i];  //changed here
        obj6.update();  
        }      
        }

 

Thanks,

Murthy

Thanks,
Murthy

Not applicable

Hi,

Thanks for reply.

This script is not working properly.

It is not updating the current record.

It is creating new record and there short desc is updating.

Please check in your instance.

Hi,

Just want to know after the giving the query filter I think it will return only 1 record because you kept active is true and caller id is some sys_id in encoded query.

So it will update the same each value in that record only.

 

Thanks,

Murthy

Thanks,
Murthy

Emmanuel Jay Mu
Tera Contributor

Hi,

You can try this:

X = [ "xyz", "abc", "def", "mno" ];

for(var x in X){
    var textVal = X[x]; // each index value

	var obj6 = new GlideRecord('incident');
	obj6.addEncodedQuery('active=true^caller_id=46d44a23a9fe19810012d100cca80666');
	obj6.setLimit(X.length); // set limit of total records to query, matching array
	obj6.query();
	while(obj6.next())
	{
		obj6.short_description = textVal;
		obj6.update();  
	}
}