0

How could I go about updating a field value of an object?

For example, I have an object of type A and its id is 474xxxxxxx. The object of type A have a field f which type is a Picklist. I have a string representation of a picklist value I would like to set.

I could retrieve the values of my object by id using SOQL in Apex. Construct a new object of type A from the values and while doing that set the desired value for the field f.

But here are two problems: now every time someone will add a new field to the A type that someone should go to my code and retrieve values of those added fields and the second problem how could I set the value for the Picklist type using a string?

A simple implementation example would be very helpful in the case, since I was not able to find one on the Internet.

  • 1
    Did you check FieldSet Class in apex? and picklist is collect of string i think you could directly assign a string value to picklist. – naruto-sfdc Jun 14 '18 at 21:09
3

If you already have an Id value, you do not have to do a query to update the value. Instead you construct an instance using the Id you have and set the field value you need and then pass that object to the update operation:

update new A__c(Id = '474xxxxxxx', F__c = 'Some Value');

To set picklist fields, you can just assign the string value.

On the subject of what your code does when extra fields are added, it is possible using describe calls and the map-life syntax that the base class of all SObject types supports, to write code that automatically adapts to the extra fields. But that is harder to get right than the more normal code that just adds code for each extra field.

  • I use this pattern whenever possible as well. – gNerb Jun 14 '18 at 22:14
  • So, will other fields of the record with id equal 474xxxxxxx be preserved using this approach? I mean if I am not mentioning them in the query at all. Like, for example, if the object of type A__c above would have the field of type G__c would the value of this field be preserved after executing the code above? – Yaroslav Trofimov Jun 15 '18 at 0:36
  • @YaroslavTrofimov Yes only the fields set on the object are updated; unset fields are not modified. – Keith C Jun 15 '18 at 8:01
5
  1. Someone will only have to come include that field in the query if they actually need to do something with it in your code. You do not need to (and absolutely should not) include every field on your object in the query. If you did find yourself in a situation in which you need every field, there are ways using dynamic apex or Field Sets to obtain all of the fields for a specified object and use those fields to build a query dynamically so users do not need to update the query every time a new field is added.
  2. This is pretty straightforward in APEX

Example:

Case c = [SELECT Status FROM Case WHERE 'blah blah blah'];

// Here status is a picklist so setting it to a string works like
c.status = 'String value';

update c;

Note:

When setting picklists using strings you need to take into consideration the configuration of the field. There is a setting that can be applied to fields that says something like "Strictly enforce picklist values". If this is enabled your string must be an exact match for a pre-defined picklist value. If this setting is disabled, any string can be used.

  • +1 What I was about to say. But you know, faster. – sfdcfox Jun 14 '18 at 21:09
  • @sfdcfox You honor me – gNerb Jun 14 '18 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.