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
Jesus GJesus G 

Update List of records

Hello,

Please, could you help me to determine which is the issue in this code? I am trying to update a list of records related (through a lookup field) to the object which is being modified.
 
public class HandlerResources {
    public static void handleAfterInsert(List<Resource__c> ResourceList) { 
        List <Project__c> ProjectsToUpdate = new List<Project__c>();
            for (Resource__c currentResource : ResourceList) {
                if (currentResource.Project__c != '') {
                    for (Project__c Project : currentResource.Project__c.Id) {
                    Project.Hours__c = Project.Hours__c + currentResource.Hours__c;
                	ProjectsToUpdate.add(Project);
                    }
                }
            }
        Database.update(ProjectsToUpdate);
    }
}

Thank you very much!
Jesus
Best Answer chosen by Jesus G
Jesus GJesus G
Hello,

My question have been solved in this other post: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D7ySIAS

Many thanks for your answers,
Jesus

All Answers

Tarun SuriTarun Suri
Hi Jesus
what error are you getting?
Jesus GJesus G
Hello,

The error with that code is 'Invalid foreign key relationship: Resource_Estimate__c.copado_Project__c' on the line 6.

If I replace Project__c.Id with Project__r.Id, the error is 'Loop must iterate over a collection type: Id'.

My question would be more about if my approach is right or if other one would be more recommended in this case.

Many thanks,
Jesus
sandeep sankhlasandeep sankhla
Hi Jesus,

Can you share your requirnmnet ? what exactly you need...I can help you out ? ABove approach is wrong.
sandeep sankhlasandeep sankhla
Hi Jesus,

Try below code and it will solve nad rllup teh data for you
 
public class HandlerResources {

	Map<Id, Decimal> MapOfProjectIdToHours = new Map<Id, Decimal>();
	
    public static void handleAfterInsert(List<Resource__c> ResourceList) {
	

        List <Project__c> ProjectsToUpdate = new List<Project__c>();

            for (Resource__c currentResource : ResourceList) {

                if (currentResource.Project__c != null) {
					
					Project__c objProject = new Project__c(Id = currentResource.Project__c);
					
					if (MapOfProjectIdToHours.containsKey(currentResource.Project__c))
					{
						
						objProject.Hours__c = currentResource.Hours__c + MapOfProjectIdToHours.get(currentResource.Project__c);
					}
					else
					{
						
						objProject.Hours__c = currentResource.Hours__c;
					}
					
                       

					MapOfProjectIdToHours.put(currentResource.Project__c, objProject);

                    

                }

            }

        Database.update(MapOfProjectIdToHours.values());

    }

}

Try and let me know if it works for you!

Thanks!
Sandeep
Jesus GJesus G
Hello Sandeep,

The idea would be to update a Project record when its related Resource record is edited, so when we change a number field on the Resource record, another number field on the Project record is updated to add the amount of the first one. I hope this makes sense :)

Thank you!
Jesus
sandeep sankhlasandeep sankhla
Hi Jesus,

I have provided the code above which you can use. check and let me know if that works for you.

Thanks!
sandeep sankhlasandeep sankhla
Hi Jesus,

Use below code where I haev updated it to get the data from object first and then update it ...so if already somedata is there then it will add it to...
 
public class HandlerResources {

 

    Map<Id, Decimal> MapOfProjectIdToHours = new Map<Id, Decimal>();
	Map<Id, Decimal> MapOfProjectIdToProject = new Map<Id, Decimal>();
	Set<Id> setProjectIds = new Set<Id>();
	
	
     

    public static void handleAfterInsert(List<Resource__c> ResourceList) {

     
	for (Resource__c currentResource : ResourceList) {
		if (currentResource.Project__c != null) 
			setProjectIds.add(currentResource.Project__c);
	}
 
	for(Resource__c objResource : [Select Id, Hours__c from Resource__c where Project__c IN: setProjectIds])
	{ 
                        if(objResource.Hours__c != '' && objResource.Hours__c !=null) {
		            MapOfProjectIdToHours.put(objResource.Project__c, objResource.Hours__c);}
else
     {
 MapOfProjectIdToHours.put(objResource.Project__c,0);
}
	}

    for (Resource__c currentResource : ResourceList) {

        if (currentResource.Project__c != null) {

        Project__c objProject = new Project__c(Id = currentResource.Project__c);
                   

			if (MapOfProjectIdToProject.containsKey(currentResource.Project__c))

			{
				
				objProject.Hours__c = currentResource.Hours__c + MapOfProjectIdToProject.get(currentResource.Project__c);
			}
			else

			{
				
				objProject.Hours__c = MapOfProjectIdToHours.get(currentResource.Project__c);

			}

                   

                        

 

                    MapOfProjectIdToHours.put(currentResource.Project__c, objProject);

 

                     

 

                }

 

            }

 

        Database.update(MapOfProjectIdToHours.values());

 

    }

 

}

 
Michał Zadrużyński 2Michał Zadrużyński 2
OK, my first question is what is relation between project__c and resource? And which is parent? This is not so obvius when I read your code. If this is master-detail you should use roll up field on master to sum hours in child objects. I think in your code you are trying to add hours to master every time when child record is inserted. It's very bad approach.
Jesus GJesus G
Many thanks for the code Sandeep. I will be testing it during the next few days and check your reply as the solution if it works properly.
Jesus GJesus G
Hello!

I am afraid I have not managed to have my class working with the code provided by Sandeep. However, it has given me a good idea about the approach to follow using the Map and Set classes.

Now, I am trying to simplify the scenario in order to understand the last step (adding the value of the child records to the parent).

Please, could you let me know how to complete the code?
public class CasesTriggerHandler {
    static Set<Id> setAccountIds = new Set<Id>();
    static Map<Id, Decimal> MapOfCaseIdToHours = new Map<Id, Decimal>();
    public static void handleAfterInsert(List<Case> CasesList){
        for (Case currentCase : CasesList) {
            if (currentCase.Update_Account__c = TRUE) {
                setAccountIds.add(currentCase.AccountId);
            }
        }
        for (Case currentCase : [SELECT Id,Hours__c FROM Case WHERE AccountId IN : setAccountIds]){
        	MapOfCaseIdToHours.put(currentCase.Id, currentCase.Hours__c);    
            // (I am not sure how to proceed from this point) currentCase.Account.Total_Hours__c = currentCase.Account.Total_Hours__c + MapOfCaseIdToHours.get(currentCase.Id);
        }
    }
}
Thank you very much!
J
Jesus GJesus G
Hello,

My question have been solved in this other post: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000D7ySIAS

Many thanks for your answers,
Jesus
This was selected as the best answer