+ Start a Discussion
Jean Grey 10Jean Grey 10 

Match user to user lookup on custom object to update field in for loop

I have a custom object Daily_Forecast__c, with a field called Forecast_Amount__c (currency) and another field that looks up to User. For each day I need to update the custom field on User.Monday__c with the corresponding Forecast_Amount__c from the Daily_Forecast__c object. There will only ever be one forecast record for each day. I feel I am almost there but my for loop is looping through all forecast records instead of just the one with the correct user lookup. How do I match the User to the Forecast record in my loop? 
//list of forecasts for user & direct reports (this is working)
        fList = new List<Daily_Forecast__c>([SELECT Id,User__r.Name,User__r.Team__c,User__r.Territory__c,User__r.UserRole.Name,Split_Forecast_Amount__c,Forecast_Amount__c,Day__c,Day_Date__c,RecordType.Name 
                                                   FROM Daily_Forecast__c 
                                                   WHERE Date__c = THIS_WEEK 
                                                    AND (RecordType.Name = 'Daily Forecast' OR RecordType.Name='Weekly Forecast - Monday' OR RecordType.Name='Weekly Forecast - Thursday')
                                                   AND User__c IN :myTeam]);
        system.debug('fList '+fList);
//MONDAY - add forecast records from monday to monday list (this is working)
        MondayListE = new List<Daily_Forecast__c>();
            for(Daily_Forecast__c e :fList){
                if(e.Day__c=='Monday' && e.RecordType.Name=='Daily Forecast'){
                    MondayListE.add(e);}}
        system.debug('MondayListE should be daily forecasts from fList with day=monday '+MondayListE);
//get users who are represented in the list of Monday forecasts (this is working)
        userMon = new List<User>([SELECT Id, Name,Monday__c FROM User WHERE Id IN (SELECT User__c FROM Daily_Forecast__c WHERE Id IN :MondayListE)]);
        system.debug('userMon '+userMon);
//my for loop - should loop through the list of users and update them with monday's forecast amount - not working - every user ends up with the same amount, the forecast amount from the last user in the list
        if(userMon.size()>0){
            for(User u :userMon){
                for(Daily_Forecast__c f :MondayListE){
                    u.Monday__c = f.Forecast_Amount__c;
                    update u;
                    system.debug('updates on users '+u);
                }
            }

Please help! Thanks in advance.
Best Answer chosen by Jean Grey 10
Neha AggrawalNeha Aggrawal
Hi Jean,

You have got a list of users in userMon and monday forecast details in MondayListE, but while running the for loop where are you checking that which forecast belongs to which user. I think you need an if condition after the second for loop to check that u.UserName equals f.User__r.Name. Something like this. Than correct values should get updated.
Thanks.

 

All Answers

Neha AggrawalNeha Aggrawal
Hi Jean,

You have got a list of users in userMon and monday forecast details in MondayListE, but while running the for loop where are you checking that which forecast belongs to which user. I think you need an if condition after the second for loop to check that u.UserName equals f.User__r.Name. Something like this. Than correct values should get updated.
Thanks.

 
This was selected as the best answer
Jean Grey 10Jean Grey 10
Ah, so simple. This worked perfectly, thank you!