You need to sign in to do that
Don't have an account?

Compare Multiple Values In a Trigger
Hi All, Please help!
I have the following scenario of at least four email address field on one object called Students
The objective is to create a trigger that will loop through the number of days and find the lowest and then populate the Primary email address with a corresponding address. This lowest number of days will mean the email is current therefore populate the primary address. However the logic should also be able to take into consideration when one or more number of days fields are blank. Please note they is always one or more addresses all the time. The logic should also exempt users who manually update the Primary email address field.
Many Thanks
I have the following scenario of at least four email address field on one object called Students
- Email1
- Email2
- Email3
- Email4
- Email1Date
- Email2Date
- Email3Date
- Email4Date
- Email1Days
- Email2Days
- Email3Days
- Email4Days
- PrimaryEmail
The objective is to create a trigger that will loop through the number of days and find the lowest and then populate the Primary email address with a corresponding address. This lowest number of days will mean the email is current therefore populate the primary address. However the logic should also be able to take into consideration when one or more number of days fields are blank. Please note they is always one or more addresses all the time. The logic should also exempt users who manually update the Primary email address field.
Many Thanks
Trigger
Class
Test Class
Let me know your thoughts
All Answers
If this is the case, you may use Process Builder, to update PrimaryEmail as required.
If it is, then we can do it without calculating lowest number of days. We can use ISCHNAGED function in Process Builder to determine if any of our four emails is changed, if it does then update Primary Email with that value.
The Primary Email is not the only reason.
Background
The email addresses get values from external systems through an over night batch. With that in mind a process wont be ideal.
Another issue is sometimes all Email addreses can be updated at the same time providing us with multiple options.The logic should cater for this type of change by only populating Primary Email Address according to prioirty of the source ie Email1 will take priority over all the other email addresses. Subsequently Email2 over the remaining addreses except Email1 etc.
Please review my code .
I did take onboard what you advised. I am also unsure if I would need to creat a custom sort list instead of using the standard list sort method. Please note I have decided to work with dates only. I am open to correction and critisim please feel free to correct the code.
trigger EmailSorting on test_object__c (before update, before insert) {
List<datetime> studentList = new List<datetime>();
for(test_object__c student : trigger.new) {
if( (trigger.isBefore &&
trigger.isInsert) ||
(trigger.isBefore &&
trigger.isUpdate )&&(
student.Email1 != trigger.oldMap.get(student.Id).Email1 && student.Email1!=Null ||
student.Email2 != trigger.oldMap.get(student.Id).Email2 && student.Emal2!=Null ||
student.Email3 != trigger.oldMap.get(student.Id).Email3 && student.Email3 !=Null||
student.Email4!= trigger.oldMap.get(student.Id).Email4 && student.Email4!=Null))
{
studentList.add(student.Email1Date);
studentList.add(student.Email2Date);
studentList.add(student.Email3Date);
studentList.add(student.Email4Date);
system.debug(studentList.size());
// Compare list check if all values are equal
if (student.Email1Date == student.Email2Date&& student.Email1== student.Email2Date&&
student.Email1Date==student.Email3Date && student.Email1Date==student.Email4Date ){
student.PrimaryEmail= student.Email1;
}
else if ( studentList != null && !studentList.isEmpty()){
studentList.sort();
system.debug(studentList.size());
system.debug(studentList.get(3));
//Compare list look for the smallest value
if(student.Email1Date==studentList.get(3)){
student.PrimaryEmail=student.Email1;}
else if(student.Email2Date==studentList.get(3)){
student.PrimayEmail=Email2;}
else if(student.Email3Date==studentList.get(3)){
student.PrimaryEmail=student.Email3;}
else if(student.Email4Date==studentList.get(3)){
student.PrimaryEmail=student.Email4;}
}
}
}
}
Did you apply my code?
What issue you found with my code?
Trigger
Class
Test Class
Let me know your thoughts