Not able to get the value of a choice field in Business rule

Anusha_Rao
Tera Contributor

Hi,

I have added new field to resource_plan table i.e u_type which has the choice values type_capex and type_opex

Anusha_Rao_0-1669087568457.png

I am writing a business rule on resource_plan table- Where based on the u_type field value on resource form, the resource_type field of corresponding cost_plan should get updated.

 

i.e if u_type is 'type_opex' then resource_type of corresponding cost plan should be updated as 'Labor Opex'

 

In my After Insert Business rule -> I am trying to update the resource_type field of cost_plan table based on the choice selected in current resource plan. But it is not updating as expected. Please help me with what is wrong with the following script:

 

Anusha_Rao_1-1669087791362.png

I think if condition is not getting evaluated properly, I tried putting

if ((current.u_type == 'type_opex') 

 

But no luck. Please help me correct the script.

 

Thanks,

Anusha

 

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Anusha_Rao ,

did you try putting some logs into the script and see if the "==" operator is working or not, also try to print current.getValue("u_type ") in the logs, you can use gs.info() function for that.

And in the line 7, don't use current.number, you should be using  current.getUniqueValue(), this will give you the sys_id of the field, as it is present as reference field in the cost plan table.

Something as for line 7:

gr.addQuery("resource_plan", current.getUniqueValue());

 

Best Regards
Aman Kumar

View solution in original post

4 REPLIES 4

Aman Kumar S
Kilo Patron

Hi @Anusha_Rao ,

did you try putting some logs into the script and see if the "==" operator is working or not, also try to print current.getValue("u_type ") in the logs, you can use gs.info() function for that.

And in the line 7, don't use current.number, you should be using  current.getUniqueValue(), this will give you the sys_id of the field, as it is present as reference field in the cost plan table.

Something as for line 7:

gr.addQuery("resource_plan", current.getUniqueValue());

 

Best Regards
Aman Kumar

@Aman Kumar S I am newbie to ServiceNow, Please share some light on reference fields. So for all the reference fields in the table sys_id is used to access the corresponding record? 

Anusha_Rao_2-1669091963270.pngAnusha_Rao_3-1669091985089.png

 

Why shouldn't we compare the value of the fields instead of sys_id since values are going to be the same? 

 

Anusha_Rao_5-1669092111837.png

 

 

 

 

Yes, so talking about reference fields, it is just a field type which is used to store records from some table, and how do you identify a particular record in SN is by sys_id.

So for example, if you observe Users table and if you check department or company field on it, it will be pointing to some record sitting on its respective table, and to verify what value is exactly is stored on the user form, you can do Right click on header of the form and "Show XML" and search for the field name, you will find sys_id there.

Here in your case, you also have resource plan field present on the cost plan form, which denotes a record sitting in resource plan table, so the same sys_id will be reflected there.

Let me know, if it gives you an understanding.

 

Best Regards
Aman Kumar

Yes @Aman Kumar S I understood your point. Thank you.