You need to sign in to do that
Don't have an account?
Transferring values between fields of two different sObjects
I have two different custom sObjects that have custom decimal fields. After running a for loop to sum the values of the fields in one of the sObjects, I need to transfer the sum to a field in the other sObject. How do I do that? I've tried several different ways that don't work. I should add that this is inside of a larger for loop in case that makes any difference.
Here's the general psuedo logic
List <TimeOject__c> BHrs; List<RscrcTrackObject__c> Rscrc; Decimal tempSum = 0.00
Outer forLoop{ for(TimeOject__c bh:BHrsTmp) { tempSum += bh.A_Hours__c ; } BHrs[0].A_Hours__c = tempSum; // The above works because I'm only using one element of the list/array // Now I want to assign tempSum to the hours in a RscrcObject Field, but can't set one equal to the other Rscrc.B_Hours__c = BHrs[0].A_Hours__c; // The above doesn't work because they're different kinds of sObjects. // trying to assign Rscrc.B_Hours__c = tempSum didn't work either // Reset tempSum for next iteration of for loop tempSum = 0.00 update Rscrc; } // Close of outer for loop
So how do I assign the results of the inner for loop that calculated the sum of A_Hours__c for that instance of BHrsTmp (a TimeObject) to the B_Hours__c in the current instance of Rscrc which is a different type of custom sObject (a RescrcTrackObject) even though both of the Hours fields are decimal?
Do I need to create a map and do a put/get or is there hopefully a simpler solution?
- pixel
Is the two custom objects holds the Master-Detail relationship? If yes, then use Roll-Up Summary data type field in your Master object to sum up hours from Detail object.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Madhan Raja M
If I correctly understand what you're asking, the answer would be that's not something which would solve my problem.
This code is part of a trigger. These sObjects are also part of a custom application. I'm new to programming in APEX and the CRM platform, but from having looked at the Master Detail, I don't think they're directly connected in through it in a way that I could use them in the trigger. Roll-up functions aren't even available for use in APEX are they?
Needless to say, I know I've much to learn...