You need to sign in to do that
Don't have an account?
Update Master record field with concatination of all child record field value
Hi all
I am new to salesforce.i need help with trigger of below requirements
I want to update the parent record field with the value of its all child records field values like the following.
Suppose parent has 2 childs.
parent field= (childrec1.field1 childrec1.field2 childrec1.field3) and( childrec2.field1 childrec2.field1 childrec2.field1).This is the pattern of parent field need to update.
Parent field wud be updated on any event on child record respectively.Trigger will be on child record.
I would be very thankful to if any one can tell me the logic behind this.
Thanks
Ishan Sharma
I am done with it .
trigger UpdateCriteria on Rule_Criteria__c (after Insert, after Update) {
Set<Id> ruleEntryIds = new Set<Id>();
for(Rule_Criteria__c rC : trigger.new){
ruleEntryIds.add(rC.Rule_Entry__c);
}
Map<Id, List<Rule_Criteria__c>> ruleMap = new Map<Id, List<Rule_Criteria__c>>();
for(Rule_Criteria__c cri : [select Id, Rule_Entry__c, Field__c, Operator__c, Value__c from Rule_Criteria__c
where Rule_Entry__c in :ruleEntryIds]){
if(!ruleMap.containsKey(cri.Rule_Entry__c)){
ruleMap.put(cri.Rule_Entry__c, new List<Rule_Criteria__c>());
}
ruleMap.get(cri.Rule_Entry__c).add(cri) ;
}
List<Rule_Entry__c> entryUpdate = new List<Rule_Entry__c>();
if(ruleMap.size() > 0){
for(Id id : ruleMap.keySet()){
Rule_Entry__c rEntry = new Rule_Entry__c(id=id);
rEntry.Criteria__c = '';
for(Rule_Criteria__c rC : ruleMap.get(id)){
if(rEntry.Criteria__c != '')
rEntry.Criteria__c += ' AND (' + rC.Field__c + ' ' +rC.Operator__c + ' ' + rC.Value__c +') ';
else
rEntry.Criteria__c += ' (' + rC.Field__c + ' ' +rC.Operator__c + ' ' + rC.Value__c +') ';
}
entryUpdate.add(rEntry);
}
}
if(entryUpdate.size() > 0)
update entryUpdate;
}
All Answers
Hi, Try this
trigger ConcatenateFields on ChildObject__c(after insert,after update){
List<ParentObject__c> parentObjList = new List<ParentObject__c>();
List<ParentObject__c> parentObjListToBeUpdated = new List<ParentObject__c>();
List<ParentObject__c> parentObjListNULL = new List<ParentObject__c>();
parentObjList = [SELECT id,Name,(SELECT ID, Field1__c,Field2__c,Field3__c FROM ChildObjects__r) FROM ParentObject__c WHERE ID IN :Trigger.new.ParentObjectID__c];
for(ParentObject__c parentObj : parentObjList){
parentObj.ParentField__c = NULL;
parentObjListNULL.add(parentObj);
}
if(parentObjListNULL.size()>0){
update parentObjListNULL;
}
for(ParentObject__c parentObj : parentObjList){
for(ParentObject__c childObj : parentObj.ChildObjects__r){
if(parentObj.ParentField__c == NULL){
parentObj.ParentField__c = '('+childObj.Field1__c + ' ' + childObj.Field2__c + ' ' + childObj.Field3__c + ')';
}
else{
parentObj.ParentField__c += ' AND ('+childObj.Field1__c + ' ' + childObj.Field2__c + ' ' + childObj.Field3__c + ')';
}
}
parentObjListToBeUpdated.add(parentObj);
}
if(parentObjListToBeUpdated.size()>0){
update parentObjListToBeUpdated;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
I appreciate your work and thanks for the reply.
I want to say that you hardcode it for 2 childs . Different parents hv diff
no. of childs and we have to concatenate field of those childs acc to that.
Thanks
Hi Ishan,
I modified the code
trigger ConcatenateFields on ChildObject__c(after insert,after update){
List<ParentObject__c> parentObjList = new List<ParentObject__c>();
List<ParentObject__c> parentObjListToBeUpdated = new List<ParentObject__c>();
List<ParentObject__c> parentObjListNULL = new List<ParentObject__c>();
public List<Id> listIds = new List<Id>();
for(ChildObject__c childObj : Trigger.new){
listIds.add(childObj.ParentObjectID__c);
}
parentObjList = [SELECT id,Name,(SELECT ID, Field1__c,Field2__c,Field3__c FROM ChildObjects__r) FROM ParentObject__c WHERE ID IN :listIds];
for(ParentObject__c parentObj : parentObjList){
parentObj.ParentField__c = NULL;
parentObjListNULL.add(parentObj);
}
if(parentObjListNULL.size()>0){
update parentObjListNULL;
}
for(ParentObject__c parentObj : parentObjList){
for(ParentObject__c childObj : parentObj.ChildObjects__r){
if(parentObj.ParentField__c == NULL){
parentObj.ParentField__c = '('+childObj.Field1__c + ' ' + childObj.Field2__c + ' ' + childObj.Field3__c + ')';
}
else{
parentObj.ParentField__c += ' AND ('+childObj.Field1__c + ' ' + childObj.Field2__c + ' ' + childObj.Field3__c + ')';
}
}
parentObjListToBeUpdated.add(parentObj);
}
if(parentObjListToBeUpdated.size()>0){
update parentObjListToBeUpdated;
}
}
As you can see, the child are not hardcoded. it is iterating within for loop for the number of childs that the parent has. It will iterate that no of times and concatenate the field values also that no of times.
NOTE: I'm fetching the childs on the basis of this query
parentObjList = [SELECT id,Name,(SELECT ID, Field1__c,Field2__c,Field3__c FROM ChildObjects__r) FROM ParentObject__c WHERE ID IN :listIds];
And this parentObjList is iterating in the loop, so it will iterate the desired no of times as many childs are present.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
this syntax is not working..and giving the error
Invalid field Field1__c for SObject Parentobject__c
as there is not any child field__c in parent object
Yes my MISS. It will be
for(ChildObject__c childObj : parentObj.ChildObjects__r)
Also I don't know your field names and object names. Thats why wrote like field1, field2 etc. You modify it according to your field names.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
List parentObjList = new List();
List parentObjListToBeUpdated = new List();
List parentObjListNULL = new List();
public List listIds = new List();
for(Rule_Criteria__c childObj : Trigger.new){
listIds.add(childObj.Rule_Entry__c);
}
parentObjList = [Select r.Name, r.Id, (Select Field__c, Operator__c,
Value__c From Rule_Criterias__r) From Rule_Entry__c r WHERE ID IN :listIds];
for(Rule_Entry__c parentObj : parentObjList){
parentObj.Criteria__c = NULL;
parentObjListNULL.add(parentObj);
}
if(parentObjListNULL.size()>0){
update parentObjListNULL;
}
for(Rule_Entry__c parentObj : parentObjList){
for(Rule_Entry__c childObj : parentObj.Rule_Criteria__r){
if(parentObj.Criteria__c == NULL){
parentObj.Criteria__c = '('+childObj.Field__c + ' ' +
childObj.Operator__c + ' ' + childObj.Value__c + ')';
}
else{
parentObj.Criteria__c += ' AND ('+childObj.Field__c + ' ' +
childObj.Operator__c + ' ' + childObj.Value__c + ')';
}
}
parentObjListToBeUpdated.add(parentObj);
}
if(parentObjListToBeUpdated.size()>0){
update parentObjListToBeUpdated;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
error::
Compile Error: Invalid field Field__c for SObject Rule_Entry__c at line 27
column 29
I mean the variable *childObj is of *Rule_Entry__c(parent) type and we
are fetching field of child(Rule_Criteria__c) by taking it as reference.
Yes, I told in the previous post that blue colored object name in the for loop should be the child object name NOT the parent object name.
Please mention there child object name and it will not throw the error.
for(Rule_Entry__c parentObj : parentObjList){
for(Rule_Entry__c childObj : parentObj.Rule_Criteria__r){
if(parentObj.Criteria__c == NULL){
parentObj.Criteria__c = '('+childObj.Field__c + ' ' +
childObj.Operator__c + ' ' + childObj.Value__c + ')';
}
else{
parentObj.Criteria__c += ' AND ('+childObj.Field__c + ' ' +
childObj.Operator__c + ' ' + childObj.Value__c + ')';
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
I am done with it .
trigger UpdateCriteria on Rule_Criteria__c (after Insert, after Update) {
Set<Id> ruleEntryIds = new Set<Id>();
for(Rule_Criteria__c rC : trigger.new){
ruleEntryIds.add(rC.Rule_Entry__c);
}
Map<Id, List<Rule_Criteria__c>> ruleMap = new Map<Id, List<Rule_Criteria__c>>();
for(Rule_Criteria__c cri : [select Id, Rule_Entry__c, Field__c, Operator__c, Value__c from Rule_Criteria__c
where Rule_Entry__c in :ruleEntryIds]){
if(!ruleMap.containsKey(cri.Rule_Entry__c)){
ruleMap.put(cri.Rule_Entry__c, new List<Rule_Criteria__c>());
}
ruleMap.get(cri.Rule_Entry__c).add(cri) ;
}
List<Rule_Entry__c> entryUpdate = new List<Rule_Entry__c>();
if(ruleMap.size() > 0){
for(Id id : ruleMap.keySet()){
Rule_Entry__c rEntry = new Rule_Entry__c(id=id);
rEntry.Criteria__c = '';
for(Rule_Criteria__c rC : ruleMap.get(id)){
if(rEntry.Criteria__c != '')
rEntry.Criteria__c += ' AND (' + rC.Field__c + ' ' +rC.Operator__c + ' ' + rC.Value__c +') ';
else
rEntry.Criteria__c += ' (' + rC.Field__c + ' ' +rC.Operator__c + ' ' + rC.Value__c +') ';
}
entryUpdate.add(rEntry);
}
}
if(entryUpdate.size() > 0)
update entryUpdate;
}
Can you help me out with slight modification in the solution?
if childObject fields are same then populate only one value..
i.e
child1_field__c ='red';
child2_field__c ='red';
Then populate only one 'red' value instead of two.
I have tried using set but i may be missing somewhere and getting errors.
Please help me.