function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
suresh dupadasuresh 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...........



User-added image

User-added image





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 
Best Answer chosen by suresh dupada
RadnipRadnip
I think what you are saying is if you Check the boxes first off it will create the records but then if you check the boxes again it creates the records again when you don't want it too? IE only one child record? If thats the case and your objects are in a master-detail relationship. Create two roll-up summary field to count the number of child records for the two child objects. Then create 2 validation rules that fires if a user checks the (eg) Hostel checkbox to see if the roll-up summary field is 0, if it isn't throw a validation error. This will mean your triggers will not fire and you won't have an issue.

All Answers

RadnipRadnip
I think what you are saying is if you Check the boxes first off it will create the records but then if you check the boxes again it creates the records again when you don't want it too? IE only one child record? If thats the case and your objects are in a master-detail relationship. Create two roll-up summary field to count the number of child records for the two child objects. Then create 2 validation rules that fires if a user checks the (eg) Hostel checkbox to see if the roll-up summary field is 0, if it isn't throw a validation error. This will mean your triggers will not fire and you won't have an issue.
This was selected as the best answer
RadnipRadnip
Also I would recommend taking the code out of the trigger and creating a class (checkout: https://www.youtube.com/watch?v=D7mqMYliy3A)