Auto populate a reference variable with user input from a text field

A Kose
Tera Contributor

I have a request to auto populate a reference field on the Task form of a requested item, with user input from a single line text variable on a Catalog Item.

I created a BR on the catalog task table. 

When to Run: before Insert, Order: 100, Filter Conditions: request item.item = abc

Advanced: 

(function executeRule(current, previous /*null when async*/) {
if(current.variables.action == 'new') //is new ict is selected
{
current.cmdb_ci = current.request_item.variables.name; //user inserts text into the name variable
}
else //if modify existing ict 
{
current.cmdb_ci = current.request_item.variables.ict_system_name; //select from look up
}
})(current, previous);

 

When I go to the task it doesn't auto populate the cmdb_ci field and there is no preview available for this variable: 

AKose_2-1692061996722.png

 

 

BUT when I double click on the variable name I get this, so the value which the user input 'debug' is there but just not populating the reference field ?

AKose_1-1692061910246.png

if I click ok it will populate the value into the field and then I can preview information via the 'i'. 

The record is created in the cmdb table and I can see it. 

Has anyone experienced this? 

I have tried a few ways to do this but none were successful. I feel it should be pretty straight forward and easy but I just can't get it. Any advice?

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @A Kose ,

You need to pass sys_id to reference fields, you can try like this.

 

if(current.variables.action == 'new') //is new ict is selected

{

var gr = new GlideRecord("cmdb_ci");

if(gr.get("name", current.request_item.variables.name)){

 current.cmdb_ci = gr.getUniqueValue(); //user inserts text into the name varible

}else{

  gs.info("Didn't found CI with given name");

}

}

else //if modify existing ict 

{

current.cmdb_ci = current.request_item.variables.ict_system_name;//select from lookup

}

})(current, previous);

 

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @A Kose ,

You need to pass sys_id to reference fields, you can try like this.

 

if(current.variables.action == 'new') //is new ict is selected

{

var gr = new GlideRecord("cmdb_ci");

if(gr.get("name", current.request_item.variables.name)){

 current.cmdb_ci = gr.getUniqueValue(); //user inserts text into the name varible

}else{

  gs.info("Didn't found CI with given name");

}

}

else //if modify existing ict 

{

current.cmdb_ci = current.request_item.variables.ict_system_name;//select from lookup

}

})(current, previous);

 

Thanks,
Anvesh

Hi @AnveshKumar M ,

Thanks for coming back to me but this didn't work, the value is blank and is not even appearing now on double click of the variable name. 

AKose_0-1692238147350.png

 

@A Kose 

Have you checked the system logs for this message "Didn't found CI with given name"?

Create a new request and check the task, if there is any CI available in cmdb it should be visible in task form otherwise you should see the message "Didn't found CI with given name" in system logs.

Thanks,
Anvesh

DanielCordick
Mega Patron
Mega Patron

Why not use flow designer to accomplish this? You can take whatever then entered in the field and populate the reference field on the Task without breaking a sweat, you can add error handling directly in the flow aswell

 

your also relying on the end user to enter the correct information, 

 

Mark Correct if this solves your issue and also mark 👍Helpful if you find my response helped in any way