incident table assigned to field who name starts with david in-activate that user

Gillerla Rajesh
Tera Contributor
var user=new GlideRecord('sys_user');
var gr=new GlideRecord('incident');
gr.addQuery('assigned_to.name','STARTSWITH','david');
var k=gr.addExtraField('assigned_to.active');
gr.query();
while(gr.next())
{
   
    gr.assigned_to.active=false;
    //gr.assigned_to.locked_out=false;
    gr.assigned_to.phone='123';
    //k=false;
    user.update();
}
gs.info(gr.assigned_to.active); // here getting false but in backend form not updaing
 
i written this script not working any one help me here
4 REPLIES 4

Bert_c1
Kilo Patron
var user=new GlideRecord('sys_user');
var gr=new GlideRecord('incident');
gr.addQuery('assigned_to.name','STARTSWITH','david');
//var k=gr.addExtraField('assigned_to.active');
gr.query();
while(gr.next())
{
    gs.info("Found inc: " + gr.number + " with assigned_to " + gr.assigned_to.getDisplayValue());
//    gr.assigned_to.active=false;
    //gr.assigned_to.locked_out=false;
//    gr.assigned_to.phone='123';
    //k=false;
//    user.update();
    user.addQuery('sys_id', gr.assigned_to.toString());
    user.query();
    if (user.next()) {
        user.active = false;
        user.update()
    }
}
gs.info(gr.assigned_to.active); // here getting false but in backend form not updaing
 

You can't update records in referenced tables like you have. the above set user "David Loo" active to false.

swathisarang98
Giga Sage
Giga Sage

Hi @Gillerla Rajesh ,

 

You cant update a record directly by dot walking you have to glide record to user table from incident table then in user table you can update the user record and add a active query so that same record will not get updated again ans again, please find the code below,

 

 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('assigned_toSTARTSWITHDavid');
gr.query();
var assignTo;
while (gr.next()) {
    assignTo = gr.assigned_to;
    gs.print(assignTo);
    var user = new GlideRecord('sys_user');
    user.addQuery('sys_id', assignTo);
	user.addActiveQuery();
    user.query();
    if (user.next()) {
		gs.print("inside if");
        user.active = false;
        user.phone = '123';
        user.update();
    }

}

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query.

 

Thanks 

Swathi

Maddysunil
Kilo Sage

@Gillerla Rajesh 

It seems like you're trying to update the active field of the assigned_to user on incident records. There are a few issues in your script that might be causing it not to work as expected:

  1. You're creating a new GlideRecord object user, but you're not querying or updating it anywhere in your script.
  2. You're adding an extra field assigned_to.active to the incident GlideRecord (gr), but you're not using it anywhere.
  3. You're setting gr.assigned_to.active to false, but you're not updating the incident record after this assignment.
  4. The gr.assigned_to.active is logged after the while loop, which might not give you the desired result since it's outside the loop.

 

var gr = new GlideRecord('incident');
gr.addQuery('assigned_to.name', 'STARTSWITH', 'david');
gr.query();
while (gr.next()) {
    gr.assigned_to.active = false;
    // Optionally update other fields here
    gr.update(); // Update the incident record after setting fields
}

// Log the value of assigned_to.active after the loop
gs.info("Assigned_to Active Status: " + gr.assigned_to.active);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks