You need to sign in to do that
Don't have an account?
mw6
Trigger to concantenate the text field values from multiple related records into a master record.
Need help to write a trigger on Class_Allocation__c to concantenate Level_Code__c from multiple related records into the 'Allocated_Levels__c' on Student__c seperated by semi colon. The total number of Class_Allocation__c records related to the Student__c is mentioned in Number_of_Confirmed_Class__c on Student__c object. (Need to remove duplicate values as well)
Student__c (Master)
Allocated_Levels__c (Text Field)
Number_of_Confirmed_Class__c (Numeric Field)
Student_Name__c
Class_Alloction__c (Related to Student v/a master relationship)
Level_Code__c (P1, P2, P3 etc)
Student_Name__c
Below is my trigger, currently it show only one value. I am not sure on how to get the value into Student__c field as P1;P2;P3 etc
trigger UpdateStudent on Class_Allocation__c (after insert, after Update)
{
set<id> noteIds = new SET<id>();
for(Class_Allocation__c note : Trigger.new )
{
noteIds.add(note.Student_Name__c);
}
List<Student__c> bigList = [select id, Allocated_Levels__c, Number_of_Confirmed_Class__c from Student__c where Id In: noteIds];
for(Class_Allocation__c note : Trigger.new )
{
for(Student__c big : bigList)
{
if(note.Student_Name__c == big.id)
{
big.Allocated_Levels__c = note.Level_Code__c + ';';
}
}
}
if(bigList.Size() > 0)
{
update bigList;
}
}
Student__c (Master)
Allocated_Levels__c (Text Field)
Number_of_Confirmed_Class__c (Numeric Field)
Student_Name__c
Class_Alloction__c (Related to Student v/a master relationship)
Level_Code__c (P1, P2, P3 etc)
Student_Name__c
Below is my trigger, currently it show only one value. I am not sure on how to get the value into Student__c field as P1;P2;P3 etc
trigger UpdateStudent on Class_Allocation__c (after insert, after Update)
{
set<id> noteIds = new SET<id>();
for(Class_Allocation__c note : Trigger.new )
{
noteIds.add(note.Student_Name__c);
}
List<Student__c> bigList = [select id, Allocated_Levels__c, Number_of_Confirmed_Class__c from Student__c where Id In: noteIds];
for(Class_Allocation__c note : Trigger.new )
{
for(Student__c big : bigList)
{
if(note.Student_Name__c == big.id)
{
big.Allocated_Levels__c = note.Level_Code__c + ';';
}
}
}
if(bigList.Size() > 0)
{
update bigList;
}
}
Here is your trigger code, try it out:
Please mark this question as solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv
All Answers
Please try the below code :
Let me know if this helps !
Thanks,
Apoorv
{
for(Student__c big : bigList)
{
if(note.Student_Name__c == big.id)
{
big.Allocated_Levels__c = note.Level_Code__c + ';';
}
}
}
This code only works on the records based on the number of Class_Allocaton__C records that enter trigger. If you test it with one record ,as Trigger.new now contains only record, same value will be stamped on the corresponding student . If you need all the Class_Allocation__c for a student , you can use a inner query inside Student__C query . some thing like [select id ,(select id from class_allocation__r) from student__c. This way you get all the class_Allocation__c records for the corresponding student.
Hi Chaitanya, I tried, but not able to figure out.
There is a difference at line 15, check it out
big.Allocated_Levels__c + = note.Level_Code__c + ';';
but this will only work after this logic is implemented.
So let's say Allocated_Levels__c in Student currently has value as blank, now if you'll enter Class_Allocation__c with Level_Code__c as P1, the value of Allocated_Levels__c will be set to P1, then again if you'll enter Class_Allocation__c with Level_Code__c as P2, the value of Allocated_Levels__c will be updated to P1;P2.
Please try and let me know.
If you want a better solution try using sub-query, that will work best in this case.
Here is your trigger code, try it out:
Please mark this question as solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv
The logic behind is a student can enroll into multiple subjects for same level (P1, P2 etc) or the same child may enroll to different level and different subject like English, Maths, Science. Each enrollment will have a class_allocation record. If the student enroll for P1 (English) and P2(Maths), the trigger should display. Look at the below screenshot, you will see 2 enrollment in P4 and 1 in P5. so the
So the Allocated_Levels__c on Student__c object should display as "P4;P5". These records (Class Allocation) will not get updated, so only Create
Thanks, it worked, only one thing left, it shows like 'P4;P4;P5, the value is repeated, is there any way, to display each value only once. For example this scenario, the student is enrolled 2 subjects for P4 and one for P5, is it possible to display P4;P5, instead of P4;P4;P5 (just curious)