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
Donald Reagan 3Donald Reagan 3 

Assign row values from one object to another object as columns or fields

I have row values as dates in one object, i.e dates within a given year. I have a second object with field names as Day1__c, Day2__c and so on till Day366__c. I want to assign the dates to the fields in the 2nd object.
Example, if the date in the first object is 2018-01-01 I want to assign a value to field Day1__c in the second object and if the date is 2018-12-31 I want to assign a value to Day365__c.
Thanks for any help.
Best Answer chosen by Donald Reagan 3
KapilCKapilC
Hi Donald,

Please find the code below.
object1 obj1 = new object1(mtdate__c = '2018-12-31');
object2 obj2 = new object2();


obj2.put('Day'+obj1.mtdate__c.dayOfYear()+'__c',obj1.mtdate__c);
update obj2;

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Donald,

I hope the dayOfYear() method of Date class https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm can help you to achieve this requirement.

As per Salesforce it "Returns the day-of-year component of a Date." First, you have to get this number (suppose 294) based on the Date from the first object and you can relate the corresponding field in the second object as Day241__c.
date myDate = date.newInstance(1998, 10, 21); Integer day = myDate.dayOfYear(); system.assertEquals(294, day);
Hope this helps.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra

 
KapilCKapilC
Hi Donald,

Please find the code below.
object1 obj1 = new object1(mtdate__c = '2018-12-31');
object2 obj2 = new object2();


obj2.put('Day'+obj1.mtdate__c.dayOfYear()+'__c',obj1.mtdate__c);
update obj2;

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
 
This was selected as the best answer