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
Kristen LundKristen Lund 

Setting values via Apex

Hello - I have a record that is used to create multiple other records of a different object. Our consultants wrote the code and it works great, however I'd like to reference specific field values from the original record and don't have enough code knowledge to do so.
 
The section of code I'd like to update is:
Reservation__c[] newReservations = new List<Reservation__c>();
		for(Id contactId:contactIds) {
			newReservations.add(new Reservation__c(Contact__c=contactId, 
												   Box__c = br.Box__c));
br is being identified in ealier code as the primary (#1). Instead of using the main br.Box__c field, I'd like to use the specific numbered field on the original. I feel like I need to use something like br.put('Name_'+String.valueOf(i)+'_Box__c') and if(br.get('Name_' + String.valueOf(i) + '_Contact__c')!=null) but I don't know how to successfully add these to the code section.

Hopefully this makes sense and any guidance would be much appreciated!
 
Waqar Hussain SFWaqar Hussain SF
You can use below code
 
Reservation__c[] newReservations = new List<Reservation__c>();
		for(Id contactId:contactIds) {
			Reservation__c rev = new Reservation__c();
			rev.Contact__c=contactId;
			rev.Box__c = br.Box__c;
			//rev.YOUR_CUSTOM_FIELD__c = ANY VALUE
			newReservations.add(rev);
		}

 
Kristen LundKristen Lund
Thanks, @waqar - but I don't think this quiete works as to what I'm after. We have 5 different Box fields: Name_1_Box__c, Name_2_Box__c, Name_3_Box__c, etc. So I want to be able to tell my inital code snip to take the n number version of the field in Br.Box__c instead of defaulting to Name_1_Box__c. Which is why I feel I need something like br.get('Name_'+String.valueOf(i)+'_Box__c')
Waqar Hussain SFWaqar Hussain SF
OK I got it now. First you will have to create a  map of string vs string. So the map will hold field name and its values. 
map<string, string> FieldValuesMap = new map<string, string>();

then populate this map in a for loop of br object.
For(Object_Name__c br: brList) {
    FieldValuesMap.put('Name_1_Box__c', br.Name_1_Box__c)
    FieldValuesMap.put('Name_2_Box__c', br.Name_2_Box__c)
    FieldValuesMap.put('Name_3_Box__c', br.Name_3_Box__c)
}
Now in your for loop you can get it 
 
Reservation__c[] newReservations = new List < Reservation__c > ();
for (Id contactId: contactIds) {
    newReservations.add(new Reservation__c(Contact__c = contactId, Box__c = FieldValuesMap.get('Name_' + String.valueOf(i) + '_Box__c')));

Hope this make sense.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
It's still a little unclear to me exactly what you're trying to do, but this could be it.  Let me know if I'm wrong and I'll try again.  I'm assuming the "Box__c" field on "br" is an Integer that determines which "Name_X_Box__c" field should be copied to the "Box__c" field on the Reservation__c record.

<pre>
    Object boxValue = br.get( 'Name_' + br.Box__c + '_Box__c' );

    List<Reservation__c> newReservations = new List<Reservation__c>();
    for ( Id contactId : contactIds )
    {
        newReservations.add
        (   new Reservation__c
            (   Contact__c = contactId
            ,   Box__c = boxValue
            )
        );
    }
</pre>
Kristen LundKristen Lund
Thanks, Glyn, it's a confusing use-case for sure! Unexpectedly, I was able to score some time from our contractors and get some assistance. I don't know what they're doing to do to make this work, but we're on the right path. Appreciate the assistance, Waqar and Glyn!