pre-selected value

mouradhadj-
Tera Contributor

Hi,

How to  pre-selecte value on New Location field with the only one available value.
and my field New Location is Reference
i didn't know how to do that ?

8 REPLIES 8

Medi C
Giga Sage

If the requirement is populate the location field if it is only one based on some criteria/query:
You can use a script include and GlideAjax on Client Script / Catalog Client Script. The Script Include should be client callable and should have a function to do GlideRecord operations and return a location if that is the only one existing as per your query.

 

On the form, catalog item or record producer..., you can create an OnLoad Client script which would call the previously created function using GlideAjax and based on the return value, you can setValue for the location field (If the script returns a value) or do nothing if the script does not return any value.

 

You can take a look at the following for GlideAjax and Script Include:
https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Hi Medi,

I tried your solution but in my code source i am blocked : 

 if (userType == "partner") {
            if (answer["location"].length === 1) {
             
                    g_form.setValue("new_location", answer["location"][0]);
               
            } else {
                g_form.setValue("new_location", ''); // Aucune valeur trouvée, laisser vide
            }
        } else {
            g_form.setValue('new_location', answer["location"]);
        }
    }
i dont' know how to say to select juste one selection when field location has juste one display 

Hi @mouradhadj- 

 

From your script include, make sure you are return the value as a string.

  • If it is JSON, you can do JSON.stringify(YOUR_DATA)
  • If it is an array, you can return YOUR_ARRAY.join()

Then on your client script, you can parse your data before you work with it

  • For JSON: JSON.parse(answer)
  • For Array: answer.split(",")

If it still does not work, please provide screenshots from your script include and client script.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Amit Verma
Kilo Patron
Kilo Patron

Hi @mouradhadj- 

 

Below link could be helpful:

https://www.servicenow.com/community/now-platform-forum/how-to-set-a-default-value-on-a-reference-fi...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.