Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

uable to update field with dynamic value as it is Choice field.

srinidhi
Tera Guru

Hello Team,

 

How to update choice field dynamically, when i try to use below syntax it is creating duplicate record.

 current.setValue(FieldName, displayName);

please suggest me here Thanks.

 

 

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage

Hi @srinidhi ,

 

You can not directly do this through set value, try to get values from Payload if that is not at all possible then as a workaround what you can do is store the state choice label inprogress or fetch that value in a variable and query sys_choice table and add table name in query and then set the state value .

 

example;

for testing i have a randon=m string field where i set value as In Progress and in before update business rule i gliderecord to sys_ choice table and got the value for label In progress and set the state value in incident,

 

BR:

swathisarang98_0-1711451282608.png

 

(function executeRule(current, previous /*null when async*/ ) {

   

    var choiceLabel = current.u_string_1;

	//gs.info('label: ' + choiceLabel);
    var gchoice = new GlideRecord('sys_choice');
    gchoice.addEncodedQuery('nameSTARTSWITHINCIDENT^element=state');
    gchoice.addQuery('label', choiceLabel); // here i have added dummy value stored in a string to check but what you can do is get the pay load value store it in a variable and use that to query
    gchoice.query();
    if (gchoice.next()) {
		//gs.info('value '+ gchoice.getValue('value'));
        current.setValue('state', gchoice.getValue('value'));
    }


})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

15 REPLIES 15

Yes no matter what value you get from the payload even if its 3 or inprogress you can do a lookup/gliderecord on choice table get the appropriate value and use g_form.setValue(); to set the value. Let me know were your struck...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @srinidhi 

Please follow these steps to avoid creating duplicate entries when updating a choice field:

1. Unique Identification: Ensure that you’re using a unique identifier (like sys_id) to fetch the correct record you wish to update. This avoids creating a new record accidentally.

2. Correct Use of setValue for Choice Fields: For choice fields, make sure to use the internal value rather than the display value unless you’re using a method that specifically requires the label (display value).

3. Correctly Differentiate Between Insert and Update: Check if a record exists before deciding to insert a new one or update an existing one.

Considering you’re reading from a payload and creating or updating a record in a new table, here’s a refined approach:

 

var targetGR = new GlideRecord('target_table_name'); // Replace ‘target_table_name’ with the actual name of your target table

// Assuming you have a unique identifier to check if it’s an update vs. insert.
// For illustration, let’s use ‘unique_field’.
targetGR.addQuery('unique_field', payload.unique_value); // ‘payload.unique_value’ should be replaced with how you’re accessing your payload’s unique identifier value
targetGR.query();

if (targetGR.next()) {
    // This means a record exists, and we are updating it.
} else {
    // No record found, so we’re creating a new one.
    targetGR.initialize();
    targetGR.unique_field = payload.unique_value; // Set the unique field value for identification
}

// Set other fields including the choice field. Assuming ‘choiceFieldName’ is your choice field and ‘choiceFieldValue’ is the internal value you want to set.
targetGR.choiceFieldName = payload.choiceFieldValue; // Ensure ‘payload.choiceFieldValue’ contains the internal value, not the display label

// Save the changes
targetGR.update(); // or targetGR.insert() if you’re creating a new record and haven’t used ‘next()’ on the GlideRecord.

 

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 


Thanks for the detailed script.

My query here is for the below line of script, i need to give displayname only for choice field value, because for the payload i will be getting the displayname only.

targetGR.choiceFieldName = payload.choiceFieldValue; // 

  

The do like this :

 

if(payload.choiceFieldValue == 'xxxx'){ //check values accordingly
target.choiceFieldValue = 'backendvalue of choice field'; //set value based on your system techenical name
}

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

why chat GPT ?


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect