What is the preferred way of setting value for a field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2024 12:54 PM - edited 03-25-2024 05:30 AM
What is the preferred way of setting value for a field? All the below 3 lines works.
Question - Which one to use where and WHY? why there are so many methods? Is there any difference between them?
gr.setValue('description','test desc');
OR
gr.description = 'test desc';
OR
gr.description.setValue('test desc');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2024 01:24 PM
Hi @Suggy ,
The preferred way is to use gr.setValue('state', '10');
You can refer the below article where AnveshKumar M has explained it in simple words,
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-25-2024 05:39 AM
Hi,thanks for replying. I have updated my question 🙂
And the article that you have shared does not talk about setValue(), instead talks about getValue()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 06:43 AM
@Suggy , the best way to set a field value is setValue please find the reason as mentioned below,
- Data Integrity: setValue ensures that the value being set meets the field's data type and any other validation rules.Directly setting values may bypass these validations, potentially leading to data integrity issues.
- Maintainability: Code using setValue is more maintainable as it explicitly sets values to fields, making it easier for other developers to understand the intent of the code.
- Compatibility: setValue is a standardized method provided by the ServiceNow platform. Setting values directly may work in some cases but might not be compatible with future platform updates or configurations.
- Logging and Auditing: When values are set using setValue, the platform's logging and auditing features can capture these changes accurately. Coerced values may not be logged or audited correctly, leading to gaps in the audit trail.
Overall, setValue provides a safer, more maintainable, and compatible approach to setting field values in ServiceNow, making it the preferred method over coercing values directly.
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-25-2024 05:43 AM
Hi @Suggy
.setvalue is right way:
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************