You need to sign in to do that
Don't have an account?
suresh dupada
How to over come unnecessary execution of update triggers.................
Hello friends,
I have a requirement like, in parent object i am having two check boxes each check box represent one child record, for example i am having parent object like NStudent__c with two check boxes confHostel__c ( custome field) and confSport__c (custome filed) ,
In case of insert operation on parent object:
1. when i want to insert a record in to the NStudent__c, if i check confHostel__c then i want to insert record in to the NHostel__c (Object) using triggers,
2. when i want to insert a record in to the NStudent__c, if i check confSport__c then i want to insert record in to the NSport__c (Object) using triggers,
3. if i check two check boxes while inserting then i want to insert one record in each of the Objects,
Here Condition is for every parent record, it must have only one record in each child Object ( If i check check box insert record, if not then no need to insert the record in child objects)
In case of Update operation on parent object:
1. Imagine i am unchecked while inserting a new record, after that i want to insert a child record then, i am updating the parent record by checking the CheckBox, if I update by cheking the checkBox then record needs to be inserted in to the child record.............
for this scenario also i wrote triggers....................
Up to above scenarios my requirement is satisfying........... But i am facing the problem in following scenario.
In case of insert operation in child object:
1. I am inserted record by Unchecking the checkBox in the parent .........(Here no problem)... But while inserting the record in to the child object we need to select the parent record through the lookup option......... when i am selecting the parent record (Which is Unchecked checkBox) then record is inserted into the child object related to that parent object........
Problem facing: While inserting the record into the child object , In the child object insert trigger is firing and in the parent object Update Trigger is firing......
as a result two records are inserted in to the parent record............. But this is not my requirement parent record must have only one child record in each child object...........
This is my Trigger:
Insert trigger on parent object:
trigger StudentAutoinsert on NStudent__c (after insert,after update)
{
if(trigger.isinsert)
{
integer i=1;
list<NStudent__c> stlist=new list<NStudent__c>();
list<NSport__c> splist=new list<NSport__c>();
list<NHostel__c> hslist=new list<NHostel__c>();
for(NStudent__c st:trigger.new)
{
if(st.ConfHostel__c) // if Checkbox is checked then it return true
{
NHostel__c h=new NHostel__c();
h.name='FirstNewHostel :'+i;
h.NStudent__c=st.id;
hslist.add(h);
}
if(st.ConfSport__c) // if Checkbox is checked then it return true
{
NSport__c s=new NSport__c();
s.name='FirstNewSport :'+i;
s.NStudent__c=st.id;
splist.add(s);
}
}
if(hslist.size()>0&&hslist!=null)
{
insert hslist;
}
if(splist.size()>0&&splist!=null)
{
insert splist;
}
}
}
Update trigger on parent object:
trigger Updateinsert on NStudent__c (after update)
{
if(trigger.isupdate)
{
integer i=1,j=1;
set<id> studIdSet=new set<id>();
list<NSport__c> spdelist=new list<NSport__c>();
list<NHostel__c> hsdelist=new list<NHostel__c>();
list<NStudent__c> stuplist=new list<NStudent__c>();
list<NSport__c> spuplist=new list<NSport__c>();
list<NHostel__c> hsuplist=new list<NHostel__c>();
for(NStudent__c st:trigger.new)
{
//-----------------START: Process for Deletion of UnCheck records--------------------------------
if(st.ConfHostel__c==false) //if CheckBox is not check then store that studentId in to the Set for delete
{
studIdSet.add(st.id);
}
if(st.ConfSport__c==false)
{
if(studIdSet.contains(st.id)!=true) // If Id already! Exists in Set then no need to process again
{
studIdSet.add(st.id);
}
}
if(studIdSet.size()>0 && studIdSet!=null)
{
spdelist=[select id,name from NSport__c where NStudent__c in:studIdSet];
delete spdelist;
hsdelist=[select id,name from NHostel__c where NStudent__c in:studIdSet];
delete hsdelist;
}
//-----------------------END: Process of Deletion when Uncheck ---------------------------------------
list<NSport__c> sportlist=[select id,name from NSport__c where NStudent__c=:st.id];
list<NHostel__c> hostellist=[select id,name from NHostel__c where NStudent__c=:st.id];
if(hostellist.size()==0) // To Avoid Multiple related list in the Parent object if size=0 then it allows Insertion
{
if(st.ConfHostel__c)
{
NHostel__c h=new NHostel__c();
h.name='UpdateHostelInsert :'+i++;
h.NStudent__c=st.id;
hsuplist.add(h);
}
}
if(sportlist.size()==0) // // To Avoid Multiple related list in the Parent object if size=0 then it allows Insertion
{
if(st.ConfSport__c)
{
NSport__c h=new NSport__c();
h.name='UpdateSportInsert :'+j++;
h.NStudent__c=st.id;
spuplist.add(h);
}
}
}
if(hsuplist.size()>0 && hsuplist!=null)
{
insert hsuplist;
}
if(spuplist.size()>0 && spuplist!=null)
{
insert spuplist;
}
}
}
----------------------------------------------------
Insert trigger on my child object: NSport
trigger AutoCheckParent on NSport__c (before insert)
{
if(trigger.isinsert)
{
for(NSport__c sp:trigger.new)
{
list<NStudent__c> stlist=new list<NStudent__c>();
list<NStudent__c> studlist=[select id,name,ConfSport__c from NStudent__c where id=:sp.NStudent__c];
for(NStudent__c stud:studlist)
{
if(stud.ConfSport__c==false)
{
stud.ConfSport__c=true;
stlist.add(stud);
}
update stlist;
}
}
}
}
I will accept any suggession
I have a requirement like, in parent object i am having two check boxes each check box represent one child record, for example i am having parent object like NStudent__c with two check boxes confHostel__c ( custome field) and confSport__c (custome filed) ,
In case of insert operation on parent object:
1. when i want to insert a record in to the NStudent__c, if i check confHostel__c then i want to insert record in to the NHostel__c (Object) using triggers,
2. when i want to insert a record in to the NStudent__c, if i check confSport__c then i want to insert record in to the NSport__c (Object) using triggers,
3. if i check two check boxes while inserting then i want to insert one record in each of the Objects,
Here Condition is for every parent record, it must have only one record in each child Object ( If i check check box insert record, if not then no need to insert the record in child objects)
In case of Update operation on parent object:
1. Imagine i am unchecked while inserting a new record, after that i want to insert a child record then, i am updating the parent record by checking the CheckBox, if I update by cheking the checkBox then record needs to be inserted in to the child record.............
for this scenario also i wrote triggers....................
Up to above scenarios my requirement is satisfying........... But i am facing the problem in following scenario.
In case of insert operation in child object:
1. I am inserted record by Unchecking the checkBox in the parent .........(Here no problem)... But while inserting the record in to the child object we need to select the parent record through the lookup option......... when i am selecting the parent record (Which is Unchecked checkBox) then record is inserted into the child object related to that parent object........
Problem facing: While inserting the record into the child object , In the child object insert trigger is firing and in the parent object Update Trigger is firing......
as a result two records are inserted in to the parent record............. But this is not my requirement parent record must have only one child record in each child object...........
This is my Trigger:
Insert trigger on parent object:
trigger StudentAutoinsert on NStudent__c (after insert,after update)
{
if(trigger.isinsert)
{
integer i=1;
list<NStudent__c> stlist=new list<NStudent__c>();
list<NSport__c> splist=new list<NSport__c>();
list<NHostel__c> hslist=new list<NHostel__c>();
for(NStudent__c st:trigger.new)
{
if(st.ConfHostel__c) // if Checkbox is checked then it return true
{
NHostel__c h=new NHostel__c();
h.name='FirstNewHostel :'+i;
h.NStudent__c=st.id;
hslist.add(h);
}
if(st.ConfSport__c) // if Checkbox is checked then it return true
{
NSport__c s=new NSport__c();
s.name='FirstNewSport :'+i;
s.NStudent__c=st.id;
splist.add(s);
}
}
if(hslist.size()>0&&hslist!=null)
{
insert hslist;
}
if(splist.size()>0&&splist!=null)
{
insert splist;
}
}
}
Update trigger on parent object:
trigger Updateinsert on NStudent__c (after update)
{
if(trigger.isupdate)
{
integer i=1,j=1;
set<id> studIdSet=new set<id>();
list<NSport__c> spdelist=new list<NSport__c>();
list<NHostel__c> hsdelist=new list<NHostel__c>();
list<NStudent__c> stuplist=new list<NStudent__c>();
list<NSport__c> spuplist=new list<NSport__c>();
list<NHostel__c> hsuplist=new list<NHostel__c>();
for(NStudent__c st:trigger.new)
{
//-----------------START: Process for Deletion of UnCheck records--------------------------------
if(st.ConfHostel__c==false) //if CheckBox is not check then store that studentId in to the Set for delete
{
studIdSet.add(st.id);
}
if(st.ConfSport__c==false)
{
if(studIdSet.contains(st.id)!=true) // If Id already! Exists in Set then no need to process again
{
studIdSet.add(st.id);
}
}
if(studIdSet.size()>0 && studIdSet!=null)
{
spdelist=[select id,name from NSport__c where NStudent__c in:studIdSet];
delete spdelist;
hsdelist=[select id,name from NHostel__c where NStudent__c in:studIdSet];
delete hsdelist;
}
//-----------------------END: Process of Deletion when Uncheck ---------------------------------------
list<NSport__c> sportlist=[select id,name from NSport__c where NStudent__c=:st.id];
list<NHostel__c> hostellist=[select id,name from NHostel__c where NStudent__c=:st.id];
if(hostellist.size()==0) // To Avoid Multiple related list in the Parent object if size=0 then it allows Insertion
{
if(st.ConfHostel__c)
{
NHostel__c h=new NHostel__c();
h.name='UpdateHostelInsert :'+i++;
h.NStudent__c=st.id;
hsuplist.add(h);
}
}
if(sportlist.size()==0) // // To Avoid Multiple related list in the Parent object if size=0 then it allows Insertion
{
if(st.ConfSport__c)
{
NSport__c h=new NSport__c();
h.name='UpdateSportInsert :'+j++;
h.NStudent__c=st.id;
spuplist.add(h);
}
}
}
if(hsuplist.size()>0 && hsuplist!=null)
{
insert hsuplist;
}
if(spuplist.size()>0 && spuplist!=null)
{
insert spuplist;
}
}
}
----------------------------------------------------
Insert trigger on my child object: NSport
trigger AutoCheckParent on NSport__c (before insert)
{
if(trigger.isinsert)
{
for(NSport__c sp:trigger.new)
{
list<NStudent__c> stlist=new list<NStudent__c>();
list<NStudent__c> studlist=[select id,name,ConfSport__c from NStudent__c where id=:sp.NStudent__c];
for(NStudent__c stud:studlist)
{
if(stud.ConfSport__c==false)
{
stud.ConfSport__c=true;
stlist.add(stud);
}
update stlist;
}
}
}
}
I will accept any suggession
All Answers