gr.setValue() or direct assignment – what’s your go-to?

TooManyTabs
Tera Contributor

My usual approach when working with GlideRecord is to assign values using gr.setValue(<fieldname>, <string>). However, I've noticed that many other developers prefer direct assignment, like gr.fieldname = <string>.


Today, I tried to set the task.comments field using setValue() and realized that Journal Input fields only work with direct assignment — the comment simply didn’t go through otherwise.


That got me thinking:
How do you typically assign values to fields in Business Rules or Script Includes?
Do you stick with setValue(), or do you prefer direct assignments depending on the field type?

2 ACCEPTED SOLUTIONS

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Using setValue is better.

Embarassed to say how often I have used gr.fieldname = <string> myself 😞

 

Do as i say, not as I do 😛

 

-Anurag

View solution in original post

TaironeA
Tera Expert

Hi,

Great question!

I usually use setValue() for most fields, especially in generic scripts where the field name might be dynamic. But when it comes to Journal fields like comments or work_notes, I always use direct assignment (gr.comments = 'text') — like you noticed, setValue() doesn't work for them.

So for me, it depends on the field type:

  • Regular fields → setValue() or direct assignment (both work)

  • Journal fields → always use direct assignment

 

If my answer has helped with your question, please mark my answer as an accepted solution and give a thumbs up.

Best,
Tairone Alb.

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

The recommended approach is to use 'getters and setters' so setValue and getValue to avoid GlideRecord issues when using direct assignment, like sys_ids and other objects to an array.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Using setValue is better.

Embarassed to say how often I have used gr.fieldname = <string> myself 😞

 

Do as i say, not as I do 😛

 

-Anurag

TaironeA
Tera Expert

Hi,

Great question!

I usually use setValue() for most fields, especially in generic scripts where the field name might be dynamic. But when it comes to Journal fields like comments or work_notes, I always use direct assignment (gr.comments = 'text') — like you noticed, setValue() doesn't work for them.

So for me, it depends on the field type:

  • Regular fields → setValue() or direct assignment (both work)

  • Journal fields → always use direct assignment

 

If my answer has helped with your question, please mark my answer as an accepted solution and give a thumbs up.

Best,
Tairone Alb.

TooManyTabs
Tera Contributor

Thanks so much to everyone who replied!
Your input really helped me understand whether I’m on the right track – and that’s exactly what I was hoping for.

Appreciate this awesome community!

– tooManyTabs