glide setvalue creating new record instead of selecting pre-existing choices ..please help me

preetikumari
ServiceNow Employee
ServiceNow Employee
 
4 REPLIES 4

BalaG
Kilo Sage

Hello @preetikumari  please provide more context, perhaps a code snippet that shows enough code to be able to debug. Feel free to mask any sensitive data in the script

 

--

Bala G

Sandeep Rajput
Tera Patron
Tera Patron

@preetikumari Are you sure you are setting the value of the choice field using the choice value and not the choice label?

 

Rajdeep Ganguly
Mega Guru


Sure, I can help you with that. The GlideRecord setValue method is used to set a field to a specified value. If you're finding that it's creating a new record instead of selecting pre-existing choices, it's likely that you're using it incorrectly. Here's how you should be using it:

1. First, you need to initialize a GlideRecord object for the table you're working with. For example, if you're working with the 'incident' table, you would do something like this:
javascript
var gr = new GlideRecord('incident');


2. Next, you need to use the get method to retrieve the record you want to update. You can do this by passing the sys_id of the record to the get method. For example:
javascript
gr.get('sys_id_of_the_record');


3. Once you have the record, you can use the setValue method to set the value of a field. For example, to set the 'priority' field to '1', you would do this:
javascript
gr.setValue('priority', '1');


4. Finally, you need to use the update method to save your changes to the database. Like this:
javascript
gr.update();


So, your complete code would look something like this:
javascript
var gr = new GlideRecord('incident');
if (gr.get('sys_id_of_the_record')) {
gr.setValue('priority', '1');
gr.update();
}

Remember to replace 'sys_id_of_the_record' with the actual sys_id of the record you want to update.

If you're still having issues, please provide more details about your problem and I'll be happy to help further.


nowKB.com

Ehab Pilloor
Mega Sage

Hi @preetikumari,

If you are using `g_form.setValue()` or `g_form.addOption()` in ServiceNow and it seems to be creating a new record instead of selecting pre-existing choices, there are a few considerations and potential solutions:

1. Check the Field Type:
- Ensure that the field you are trying to set a value for is a reference field or a choice field. If it's a reference field, you need to pass the sys_id of the existing record as the value.

2. Verify Choice Values:
- If it's a choice field, make sure that the value you are trying to set matches one of the predefined choices exactly. Any deviation, even in case or spaces, can lead to the creation of a new record.

3. Use `addOption()` for Choice Fields:
- If you are working with a choice field, consider using `g_form.addOption()` instead of `g_form.setValue()` for dynamically adding new choices. Ensure that the value you are trying to add as an option exactly matches one of the existing choices.

4. Debugging:
- Use browser developer tools to debug and log the values you are passing to `setValue()` or `addOption()`. This can help you identify any discrepancies between the expected and actual values.

Here's an example using `g_form.addOption()` for a choice field:


// Assume 'my_field' is the field name, and 'My Existing Choice' is an existing choice
var choiceLabel = 'My Existing Choice';
var choiceValue = 'my_existing_choice_value';

if (!g_form.getOption('my_field', choiceValue)) {
g_form.addOption('my_field', choiceValue, choiceLabel);
g_form.setValue('my_field', choiceValue);
} else {
g_form.setValue('my_field', choiceValue);
}

 

Remember to adapt the script based on your specific field names and values. If the issue persists, examining the browser console for any error messages can provide additional insights into the problem.

 

If you found this reply useful, please mark it as solution/helpful.

 

Thanks and Regards,

Ehab Pilloor