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
Neeru Nagpal 10Neeru Nagpal 10 

need to update different fields on single parent object from its different child records

Requirement Details:

I have one object say named A which have two diffrent child object say attachment & data. 
  1. a record is created in object A.
  2. Assignment records are created off the related record as CHILDREN of the A
  3. Each Assignment record has a Resource lookup, which holds a Contact record
  4. There may be up to 10 Assignment records created
  5. At a later point, a Data (third object) Record is created off the related record as a CHILD of the A
Requirement is when a nea record of Data is created, there are 10 fields for Resources. we have to copy that fields from related assignments and paste that to the fields in data record.

Please help me here to code this requirement and how we can iterate both the lists in final loop.

Thanks
LBKLBK
Hi Neeru,

I am trying to understand your requirement a little better so that I can help you out.

1. You have ObjectA (which is parent) and two children (Assignment and Data).

2. Assignment has a lookup to Contact. Assignment can have upto 10 records per record in ObjectA

3. You create one record in Data per record in ObjectA, which has 10 fields to hold Resource information.

My question is what kind of fields are these 10 fields? Are they lookup to Assignment? Or, are they lookup to Contact?
 
Neeru Nagpal 10Neeru Nagpal 10
thanks for your reply..

No these are simple text fields.. in which i want to just add the contact full name from assignment records.
LBKLBK
Hi Neeru,

Try this trigger.
trigger updateResourceData on Data__c (before insert, before update){
	List<Id> lstObjectAIds = new List<Id>();
    Map<Id, List<Assignment__c>> mapAssignments = new Map<Id, List<Assignment__c>>();
	for (Assignment__c assignment : Trigger.new){
		lstObjectAIds.add(assignment.ObjectA__r.Id);
	}
	
	List<Assignment__c> lstAssignments = [SELECT ID, ObjectA__r.Id, Resource__r.FirstName, Resource__r.LastName FROM Assignment__c WHERE ObjectA__c =:lstObjectAIds ORDER BY ObjectA__r.Id, CreatedDate];
	
	for(Assignment__c assignment : lstAssignments){
		List<Assignment__c> lstA = new List<Assignment__c>();
		if(mapAssignments.containsKey(assignment.ObjectA__r.Id)){
			lstA = mapAssignments.get(assignment.ObjectA__r.Id);
		}
		lstA.add(assignment);
		mapAssignments.put(assignment.ObjectA__r.Id, lstA);
	}
	
	for (Data__c data : trigger.new){
		List<Assignment__c> lstA = new List<Assignment__c>();
		if(mapAssignments.containsKey(assignment.ObjectA__r.Id)){
			lstA = mapAssignments.get(assignment.ObjectA__r.Id);
			Integer nCount = 1;
			for(Assignment__c assignment : lstA){
				String fieldName='Resource_' + (String)nCount + '__c';     //make sure your field name is formed properly. I have assumed that your field names would be Resource_1__c till Resource_10__c
				
				data.put(fieldName,assignment.Resource__r.FirstName + ' ' + assignment.Resource__r.LastName);
			}
		}
	}
}
I have made assumptions on your object names and field names.
For example,

1. Objects names are ObjectA__c, Assignment__c and Data__c

2. Contact look up in Assignment__c is Resource__c

3. Resource fields in Data__c are Resource_1__c till Resource_10__c

If these names are not matching, you need to update the code accordingly.

Let me know how it goes.
Neeru Nagpal 10Neeru Nagpal 10
Hi,
Thanks for replying.
I have written code as per design provided to me. the same code.. can you please help me get the set of resources name in this code?? as i want unique resource to be updated on survey. here is my code:
 
/**
* @author 
* @date   26-Apr-2017
* @description Handler class to hold the logic for SurveyServicesTrigger  
*/
public class SurveyServices{     
    
    /**
    * @description setResources call filterNewSurveysOnStatus method to get list of filtered survey records on basis of status
    */
    public static void setResources(List<NPX_Survey_Record__c> lstSurveyRecords){
    
        List<NPX_Survey_Record__c> filteredSurvey = filterNewSurveysOnStatus(lstSurveyRecords, 'new');
        Map<Id, List<pse__Assignment__c>> projectAssignment = getRelatedAssignments(filteredSurvey); 
        updatesResourceFields(projectAssignment);
    }
    
    /**
    * @description Filter List of resources on basis of status passed
    * @return list of survey
    */
    private static List<NPX_Survey_Record__c> filterNewSurveysOnStatus(List<NPX_Survey_Record__c> lstSurveyRecords, String status){
        List<NPX_Survey_Record__c> lstFilteredSurvey = new List<NPX_Survey_Record__c>();
        for(NPX_Survey_Record__c survey: lstSurveyRecords){
            if(survey.Status__c == status){
                lstFilteredSurvey.add(survey);
            }
        }             
        return lstFilteredSurvey;     
    }
    
    /**
    * @description query related assignment on basis of project id
    * @ fetch project id's from list of survey
    * subquery list of assignments from system filter on project ids and create map of projectid's and list of related assignments
    * @return map of related assignments
    */
    private static Map<Id, List<pse__Assignment__c>> getRelatedAssignments(List<NPX_Survey_Record__c> lstFilteredSurvey){
        
        // Set of project Id's
        Set<Id> setProjectIds = Pluck.ids('Project__c', lstFilteredSurvey);
        
        // Map of Project Id's and related list of assignments
        map<Id,List<pse__Assignment__c>> mapProjectToAssignment = new map<Id,List<pse__Assignment__c>>();
        
        List<pse__Proj__c> lstProject = [Select p.Id, (Select Id, pse__Resource__r.Name, pse__Resource__c From pse__Assignments__r LIMIT 10)
                                        From pse__Proj__c p where p.Id IN: setprojectIds];
        
        // Iterate list of project to create map of project ids and list of assignments
        for(pse__Proj__c p: lstProject){
            if(!mapProjectToAssignment.containsKey(p.Id)){
                mapProjectToAssignment.put(p.Id,new List<pse__Assignment__c>{p.pse__Assignments__r});
            }           
        }        
        return mapProjectToAssignment;           
    }
    
    /**
    * @description update resources field on survey object
    * Query survey record related to project and create map of project ids and related survey record
    * Iterate map of survey and list of assignments to update the resources fields
    */
    private static void updatesResourceFields(map<Id,List<pse__Assignment__c>> mapProjectToAssignment){
        
        // Map of project ids and related survey record
        map<Id,NPX_Survey_Record__c> mapProjectToSurvey = new map<Id,NPX_Survey_Record__c>();
        
        for(NPX_Survey_Record__c s: [Select Id,Project__c from NPX_Survey_Record__c where Project__c IN :mapProjectToAssignment.keySet()]){
            if(!mapProjectToSurvey.containsKey(s.Project__c)){
                mapProjectToSurvey.put(s.Project__c,s);
            }
        }
        
        // Iterate loop to update survey fields
        for(Id key: mapProjectToSurvey.keySet()){
		List<pse__Assignment__c> lstAssignment = mapProjectToAssignment.get(key);
			if(!lstAssignment.isEmpty() && lstAssignment.size() > 0){
				if(lstAssignment[0].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource1__c = lstAssignment[0].pse__Resource__r.Name;
				}
				else if(lstAssignment[1].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource2__c = lstAssignment[1].pse__Resource__r.Name;
				}
				else if(lstAssignment[2].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource3__c = lstAssignment[2].pse__Resource__r.Name;
				}
				else if(lstAssignment[3].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource4__c = lstAssignment[3].pse__Resource__r.Name;
				}
				else if(lstAssignment[4].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource5__c = lstAssignment[4].pse__Resource__r.Name;
				}
				else if(lstAssignment[5].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource6__c = lstAssignment[5].pse__Resource__r.Name;
				}
				else if(lstAssignment[6].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource7__c = lstAssignment[6].pse__Resource__r.Name;
				}
				else if(lstAssignment[7].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource8__c = lstAssignment[7].pse__Resource__r.Name;
				}
				else if(lstAssignment[8].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource9__c = lstAssignment[8].pse__Resource__r.Name;
				}
				else if(lstAssignment[9].pse__Resource__r.Name <> null){
					mapProjectToSurvey.get(key).Resource10__c = lstAssignment[9].pse__Resource__r.Name;
				}
			}	    	              
		}
	}
}