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
SalesRedSalesRed 

Dynamically determine required variable for assignment without using Large If Statement

Hi,  I have a requirement to do the following.

- In an object I have fields representating the days of the month (therefore up to 31 days).

- In a for loop I have values assigned for some days (possibly not all days of the month)

      The values have field value pairs of (daynumber, myvalue) 

 

       I need to assign the corrrect "myvalue" value to the correct field in my object which relates to the day value.

     .E.g. (10,'12.50')   should be assigned to field "Day_10_Price__c"

 

I know I can do this using a very large if statement (checking for the day and assigning it accordingly)  however this seems a bit too much or silly coding.  Does anyone know of a different way this can be achieved?  I know "Case" and "Switch" do not exist in Apex.

 

Any help or suggestions on this would be appreciated,  otherwise I assume I have to create the huge if statement.

 

Thanks in advance for your help!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jayjaysjayjays

Hi,

 

there is one trick you can do to allow you to specify field names using strings and that is to cast the object to an sObject and reference the fields like this:

 

sObject tempObject = (sObject)Custom__c;

string fieldValue =tempObject.get ('fieldName');

tempObject.put('fieldName','fieldValue');

 

in your case it would look something like the below:

 

sObject tempObject = (sObject)Custom__c;

tempObject.put('Day_' + valuePair.DayNumber + '_Price__c',valuePair.myValue);

 

As Apex creates a reference upon assignment the values on the original object Custom__c are updated using this method.  I have used this method to loop through fields in a fieldset and perform actions on them like complex validation rules in a class called by triggers.

 

Thanks,

James.

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus

Let's see if I understand this. You have a record for an SObject, an integer that corresponds to a number of a day in a month, and a String value. If the integer is 10, you want to put the value into record.Day_10_Price__c . Right?

 

Easy!

 

The SObject object supports get and put methods that you can use to access field names dynamically, rather than hard-coding them. The equivalent of getting the value of record.Day_10_Price__c is record.get('Day_10_Price__c') .

 

To assign a value to a field, you can use something like record.put('Day_10_Price__c', value)  .

 

If you have the integer in a variable named num, you can use this: record.put('Day_'+num+'_Price__c', value)

 

For details, see the Apex Language Reference's section on Dynamic SOQL, especially the section titled "Setting and Retrieving Field Values."

jayjaysjayjays

Hi,

 

there is one trick you can do to allow you to specify field names using strings and that is to cast the object to an sObject and reference the fields like this:

 

sObject tempObject = (sObject)Custom__c;

string fieldValue =tempObject.get ('fieldName');

tempObject.put('fieldName','fieldValue');

 

in your case it would look something like the below:

 

sObject tempObject = (sObject)Custom__c;

tempObject.put('Day_' + valuePair.DayNumber + '_Price__c',valuePair.myValue);

 

As Apex creates a reference upon assignment the values on the original object Custom__c are updated using this method.  I have used this method to loop through fields in a fieldset and perform actions on them like complex validation rules in a class called by triggers.

 

Thanks,

James.

This was selected as the best answer
SalesRedSalesRed

Magic,  thanks for the help!

SalesRedSalesRed

Hi MJ,   Thanks for your help yesterday.  It was your comment which answered my question.  I didn't realise James had added a further comment since I reviewed yours and I accepted the wrong one as the question answer when I went back into the question afterwards.  Anyhow.  I guess both answers would have worked.  

 

Thanks for your help & apologies for accepting the wrong answer!