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
pixelpixel 

Help! Decimal Oject value into sObject field?

How can I take the results of a for loop, saved in decimal object, and put them into a field of a custom sObject? The field also holds decimal values. Below is an example of what I'd like to do that doesn't work. I can't assign the value of a decimal object to an sObject or equate the two directly. So how do I do it by reference? After trying countless methods, I'm at a loss.

 

// Code below is enclosed in larger for loop

for(List<Time__c> BrnH:BrnH_Tmp){
tempSum += BrnH.A_Hours__c ;
}
RsrcHrsLst.B_Hours__c = valueOf(tmpSum);

 

I should note that the lists in the for loop are Time__c Lists while the one outside of it is a ResrceTrack__c List, so they're not the same kinds of Lists even though A_Hours__c are related to B_Hours__c. I just can 't seem to find a direct way to get the sum of the A_Hours__c (Time__c) into a B_Hours__c (ResrceTrack__c) field of a RescrceTrack object after I've located and summed the A_Hours records in my trigger!

 

As one possibility, Is there a method I could use to "put" the values of tempSum into B_Hours__c? Do I need to create a map between the pairs of variables to do that? I'm at my wits end trying to do something that seems as though it should be simple.

 

- pixel

 

 

Alex.AcostaAlex.Acosta

I'm a bit lost with your issue, but I'm assuming your fields are probably not of number / decimal type. What you can do is something like such:

 

for(List<Time__c> BrnH:BrnH_Tmp){
     RsrcHrsLst.B_Hours__c += Decimal.valueOf(BrnH.A_Hours__c);
}

 

If the issue is that your field RsrcHrsLst.B_Hours__c is not of number / decimal type

for(List<Time__c> BrnH:BrnH_Tmp){
     tempSum += BrnH.A_Hours__c ;
}
RsrcHrsLst.B_Hours__c = String.valueOf(tmpSum);
pixelpixel

Hi Alex,

I just looked and according to what I can tell, both of the fields in each of the two different sObjects show as being of the type "Number". I know that they also are formatted to display with a length of 6 whole numbers with 2 digits beyond the decimal point.

 

Just the same, I'll try your solution to see if it works for me. I appreciate your reply and if it works, I'll mark it as solved. 

 

 - pixel