function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

assigning values dynamically.

I need to know if  I can do this in salesforce



Create a map in the following way:


key--> api name of a field on account

value--> a number.


Then assign map.get('key')=='another number';(Assume this stmt as assigning some value to a field on account)

Then call update account.


Any help is much appreciated.


Sfd developerSfd developer



You can create a map like that, but if you do this,

map.get('key')='another number';

you will get an error (Expression cannot be assigned)

Instead you do like this, map.put(key, newnumber)

and when you dml on  account,

Account acc = new Account();
acc.NumberOfEmployees = map.get(key); //work fine because its datatype is number i.e map returns integer
acc.Description = map.get(key); // throws error. its is a LongTextArea


If you are actually trying to set values on an sObject, you don't need a map. You can put the value onto the object.


Account acc = new Account();

acc.put('Name', 'NewAccount');
acc.put('Employees', 20);

Insert acc;

 But you must be careful with the value, and use putSobject for references.