- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 12:06 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 04:09 AM
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:
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 04:09 AM
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:
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 10:57 AM
@srinidhi ,Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query.
Thanks,
Swathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 11:30 AM
Hello @swathisarang98 and @Sohail Khilji ,
Below is my sample script and its output. In the screen shot we can see that two Software value came.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var requestBody = request.body.data;
var caller = requestBody.caller;
var category = requestBody.category;
var shortdescription = requestBody.shortdescription;
// Insert record into UserTable
var gr = new GlideRecord('incident');
gr.initialize();
gr.setValue('caller_id', caller);
gr.setValue('category', category);
gr.setValue('short_description', shortdescription);
gr.insert();
response.setStatus(201); // 201 Created
response.setBody({
message: "Records created successfully in incident table"
});
} catch (ex) {
response.setStatus(500); // 500 Internal Server Error
response.setBody({
error: "An error occurred while processing the request"
});
}
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 11:37 AM
@srinidhi can you put some log (gs.info) and add requestBody.category in it and share the screenshot or paste the log here?
Thanks
Swathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 11:51 PM