You need to sign in to do that
Don't have an account?
Girbson Bijou
Creating multiple child record in on trigger
How can you help me please?
I have two objects: Employee and Leave. Leave is a child of Employee.
I want write a trigger to assign leave type to each employee depending on some specific criteria.
for example:
if the gender of an employee is Female && his status is fulltime , the trigger should create the following the record on the leave object:
- Maternity Leave
- Annual Leave
- Personal Leave
- unpaid leave
if the gender of an employee is Male && his status is fulltime , the trigger should create the 3 following the record on the leave object:
- Paternity Leave
- Annual Leave
- Personal Leave
- Unpaid Leave
if the gender of an employee is Male || Female and his status is part time , the trigger should create the following the record on the leave object:
- Unpaid Leave
NB: I have already write a confitional formula to assign the Number of day for each type of Leave.
Here the sample of code that I have. I create a separate trigger for each trigger, but i can not create multiple childs record with specific criteria in one trigger.
trigger CreateLeave3 on Employee__c (after insert ) {
List<Leave__c> Leaves = new List<Leave__c>();
for(Employee__c l : trigger.new)
{
if(l.Status__c =='Emplyee' && l.Gender__c =='Female' ){
Leave__c Child = new Leave__c ();
Child.Employee__c = l.id;
Child.Leave_Type__c = 'Maternity Leaves';
Leaves.add(Child);
}
}
insert Leaves;
}
I have two objects: Employee and Leave. Leave is a child of Employee.
I want write a trigger to assign leave type to each employee depending on some specific criteria.
for example:
if the gender of an employee is Female && his status is fulltime , the trigger should create the following the record on the leave object:
- Maternity Leave
- Annual Leave
- Personal Leave
- unpaid leave
if the gender of an employee is Male && his status is fulltime , the trigger should create the 3 following the record on the leave object:
- Paternity Leave
- Annual Leave
- Personal Leave
- Unpaid Leave
if the gender of an employee is Male || Female and his status is part time , the trigger should create the following the record on the leave object:
- Unpaid Leave
NB: I have already write a confitional formula to assign the Number of day for each type of Leave.
Here the sample of code that I have. I create a separate trigger for each trigger, but i can not create multiple childs record with specific criteria in one trigger.
trigger CreateLeave3 on Employee__c (after insert ) {
List<Leave__c> Leaves = new List<Leave__c>();
for(Employee__c l : trigger.new)
{
if(l.Status__c =='Emplyee' && l.Gender__c =='Female' ){
Leave__c Child = new Leave__c ();
Child.Employee__c = l.id;
Child.Leave_Type__c = 'Maternity Leaves';
Leaves.add(Child);
}
}
insert Leaves;
}
It is easy to implement using combination of Utility class and Triggers together.
try this code but please look out for any typos and recursion events.
Hi Girbson,
Please follow the below simple code snippet -:
trigger CreateLeave3 on Employee__c (after insert,after update) {
List<Leave__c> lst_Leave = new List<Leave__c>();
for(Employee__c emp : Trigger.new)
{
if(emp.Status__c=='fulltime' && emp.Gender__c =='Female')
{
Leave__c FemaleLeave = new Leave__c();
FemaleLeave.Leave_Type__c = 'Maternity Leave';
FemaleLeave.Employee__c = emp.id;
lst_Leave.add(FemaleLeave);
Leave__c FemaleLeave1 = new Leave__c();
FemaleLeave1.Leave_Type__c = 'Annual Leave';
FemaleLeave1.Employee__c = emp.id;
lst_Leave.add(FemaleLeave1);
Leave__c FemaleLeave2 = new Leave__c();
FemaleLeave2.Leave_Type__c = 'Personal Leave';
FemaleLeave2.Employee__c = emp.id;
lst_Leave.add(FemaleLeave2);
Leave__c FemaleLeave3 = new Leave__c();
FemaleLeave3.Leave_Type__c = 'unpaid leave';
FemaleLeave3.Employee__c = emp.id;
lst_Leave.add(FemaleLeave3);
}
if(emp.Status__c=='fulltime' && emp.Gender__c == 'Male')
{
Leave__c MaleLeave = new Leave__c();
MaleLeave.Leave_Type__c = 'Paternity Leave';
MaleLeave.Employee__c = emp.id;
lst_Leave.add(MaleLeave);
Leave__c MaleLeave1 = new Leave__c();
MaleLeave1.Leave_Type__c = 'Annual Leave';
MaleLeave1.Employee__c = emp.id;
lst_Leave.add(MaleLeave1);
Leave__c MaleLeave2 = new Leave__c();
MaleLeave2.Leave_Type__c = 'Personal Leave';
MaleLeave2.Employee__c = emp.id;
lst_Leave.add(MaleLeave2);
Leave__c MaleLeave3 = new Leave__c();
MaleLeave3.Leave_Type__c = 'unpaid leave';
MaleLeave3.Employee__c = emp.id;
lst_Leave.add(MaleLeave3);
}
if(emp.Status__c=='parttime' && (emp.Gender__c == 'Male' || emp.Gender__c == 'Female'))
{
Leave__c PartTimeLeave = new Leave__c();
PartTimeLeave.Leave_Type__c = 'unpaid leave';
PartTimeLeave.Employee__c = emp.id;
lst_Leave.add(PartTimeLeave);
}
}
if(lst_Leave!=null && lst_Leave.size()>0)
insert lst_Leave;
}
Please let me know if it works or not :)
Thanks
Rajat Maheshwari