What is the preferred way of setting value for a field?

Suggy
Giga Sage

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');

 

5 REPLIES 5

swathisarang98
Giga Sage
Giga Sage

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,

https://www.servicenow.com/community/developer-forum/which-is-best-and-efficient-method-kindly-sugge... 

 

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

Regards,

Swathi Sarang

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()

@Suggy , the best way to set a field value is setValue please find the reason as mentioned below,

 

  1. 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.
  2. 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.
  3. 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.
  4. 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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Suggy 

 

.setvalue is right way:

 

https://www.servicenow.com/community/developer-articles/setvalue-reference-fields-variables-use-the-...

*************************************************************************************************************
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]

****************************************************************************************************************