How to add an choice using the serverside script?

Fahimas
Tera Contributor

Hello! Iam new to Servicenow, I have one requirement that i have one field called software name which was an choice list type 

that also i have another field called other. when the other field was filled that i want to add these field value to the choice list of software name field.basically that i want to add an choice to the field using server side script . it was one of the requirement of my workflow task. That i  have written some code, 

gr = new GlideRecord('sys_choice');
     gr.addQuery("name","incident");
     gr.addQuery("element","State");
     gr.addQuery("label","New");
     if(gr.next()){
        gr.setworkflow(false);
        gr.label = "helo";
        gr.update();
     }
 
please verify whether it was correct otherwise provide a code to add an choice using serverside script

    

3 REPLIES 3

AakashG1819
Tera Contributor

Hlo @Fahimas,

Hope you are doing well.

 

 

Proposed Solution

In order to achieve this task and to give you an appropriate solution of this problem, I personally tried and implemented it on my Personal Developer Instance so that you can get an appropriate solution as soon as possible. For this, what I did I wrote my own server-side code/script using Background Script, and tried to update the label like you are doing and it seems to be working fine for me and I believe that it'll work for you as well. Hence, pasting my code down below that you can refer and update whereever you want to maybe in the Workflow or somewhere else.

 

Server Side Script

var grChoice = new GlideRecord("sys_choice");
grChoice.addQuery("name", "u_custom_users");
grChoice.addQuery("element", "u_software_name");
grChoice.addQuery("label", "Slack");
grChoice.query();
if (grChoice.next()) {
    // gs.info(gr.label);
    grChoice.label = 'Slack2';
    grChoice.update();
}

 

For your reference and just to show you the results, also attaching snapshots of the outputs that will give you better insights of how the script is working or the best thing will be to follow the solution and execute the scripts on your instance.

Before Running the Script

AakashG1819_1-1737306847890.png

 

Background Script

AakashG1819_3-1737306936281.png

 

After Running the Script

AakashG1819_2-1737306877426.png

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution as helpful and Accepted as a Solution.

 

Thanks 🙂

Aakash Garg

ServiceNow Developer

Thanks for your response. But I want to add the value as another choice in the field .

You are comparing that slack was there and you are adding that choice in the place of slack.litetrally I don't want that.basically I want to add an value as choice to the choice field in catalog item .sry that I didn't mentioned catalog item.

 

Fahimas
Tera Contributor

I was done some script which wasn't worked out !!