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
New_DeveloperNew_Developer 

Trigger Help

Hi Created  below  trigger to update the order before insert and before delete.it is working fine for the before insert ,but  it is not working before delete . can any one please help to resolve the issue..

 

 trigger Recordsorder on St__c (before insert,before delete ){
if(Trigger.isInsert) {   Decimal recordOrder = Trigger.New[0].Ob__c;   

ID currentRecord =  Trigger.New[0].Id; 

 List<St__c> stt = new List<St__c>([Select Id,Or__c from St__c where  Ob__c  >=: recordOrder and Id<>: currentRecord  order by Ob__c ]); 

 Decimal order; 

 for(St_c s : stt)   {
    s.Ob__c = recordOrder + 1;       

   recordOrder  = recordOrder  + 1;   } 

 update stt ;
}   
else 
{
If (Trigger.isDelete){   Decimal recordOrder_old = Trigger.Old[0].Ob__c;   

ID currentRecord_old =  Trigger.Old[0].Id; 

 List<St__c> stt_old = new List<St__c>([Select Id,Ob__c from St__c where  Ob__c  >=: recordOrder_old and Id<>: currentRecord_old1  order by Ob__c ]); 

 Decimal order;   for(St__c s : stt_old) 

 {
           s.Ob__c = recordOrder_old -1;   

       recordOrder  = recordOrder_old+1;   

 update stt_old ; 

 }}}

rgdcrgdc

What is the problem? What is the problem´s message?

Shashikant SharmaShashikant Sharma

I think you are changing order number kind of thing whenever a item inserted or deleted, it is like an item comes in a array or removed from array.

 

Change your code to

 

 trigger Recordsorder on St__c (before insert,before delete )
 {
    if(Trigger.isInsert) 
    {   
     Decimal recordOrder = Trigger.New[0].Ob__c;   
     ID currentRecord =  Trigger.New[0].Id; 
     List<St__c> stt = new List<St__c>([Select Id,Or__c from St__c where  Ob__c  >=: recordOrder and Id<>: currentRecord  order by Ob__c ]); 
     Decimal order; 
     for(St_c s : stt)   
     {
        s.Ob__c = recordOrder + 1;       
       recordOrder  = recordOrder  + 1;   
     } 
     update stt ;
    }   
    else 
    {
        If (Trigger.isDelete)
        {   
          Decimal recordOrder_old = Trigger.Old[0].Ob__c;   
          ID currentRecord_old =  Trigger.Old[0].Id; 
          List<St__c> stt_old = new List<St__c>([Select Id,Ob__c from St__c where  Ob__c  >=: recordOrder_old and Id <>: currentRecord_old1  order by Ob__c ]); 
          Decimal order;   
          for(St__c s : stt_old) 
          {
              s.Ob__c = recordOrder_old;   
              recordOrder  = recordOrder_old + 1;   
          } 
          update stt_old; 
        }
    }
 
}

 Just updated in delete trigger 

 

s.Ob__c = recordOrder_old - 1; 

 

to 

s.Ob__c = recordOrder_old;  

 

 

New_DeveloperNew_Developer

How do we write the code for editing the order of records

suppose if the user wants to edit the order number of 4th record and change it to 8 and save it then the record should go to 8th place and the 8th record should become 9 and 9 to 10 etc etc and the order numbers from 4 to 8 should also change accordingly

Shashikant SharmaShashikant Sharma

I did not get your point,

 

If you move from 4 to 8 then your first part of code that you wrote for insert , if you do it for update as well it will manage 8th and 9th,10th .... but I did not understand what change do you want in this case in

5 to 7 and what value whould be filled in 4 the place. Just clear this question then we can write code for that also.

New_DeveloperNew_Developer

5 to 7 values should be changed to 4 to 6 and the old 8th number should be changed to 7

New_DeveloperNew_Developer

or can  u provode me the code for swapping. like if 5 is updated to 1 then 1 should become 5

Shashikant SharmaShashikant Sharma

Swaping will cause cyclic trigger so it I have stoped cyclic execution using static variable. First Create Static variable like

 

public class Constants
    {
         //Constant to block execution of trigger
         public static Boolean runTrigger = true;
    }

 

 

 

Here is your trigger which will swap in case of edit

trigger Recordsorder on St__c (before insert,before delete, before update )
 {
    if(Trigger.isInsert) 
    {   
     Decimal recordOrder = Trigger.New[0].Ob__c;   
     ID currentRecord =  Trigger.New[0].Id; 
     List<St__c> stt = new List<St__c>([Select Id,Or__c from St__c where  Ob__c  >=: recordOrder and Id<>: currentRecord  order by Ob__c ]); 
     Decimal order; 
     for(St_c s : stt)   
     {
        s.Ob__c = recordOrder + 1;       
       recordOrder  = recordOrder  + 1;   
     } 
     update stt ;
    }   
    else 
    {
        If (Trigger.isDelete)
        {   
          Decimal recordOrder_old = Trigger.Old[0].Ob__c;   
          ID currentRecord_old =  Trigger.Old[0].Id; 
          List<St__c> stt_old = new List<St__c>([Select Id,Ob__c from St__c where  Ob__c  >=: recordOrder_old and Id <>: currentRecord_old1  order by Ob__c ]); 
          Decimal order;   
          for(St__c s : stt_old) 
          {
              s.Ob__c = recordOrder_old;   
              recordOrder  = recordOrder_old + 1;   
          } 
          update stt_old; 
        }
        else if(Trigger.isUpdate)
        {
          if(Constants.runTrigger)  
            {
                    Decimal recordOrder_old = Trigger.Old[0].Ob__c;   
                    Decimal recordOrder_new = Trigger.New[0].Ob__c;   
                    ID currentRecord_old =  Trigger.Old[0].Id; 
                    List<St__c> stt_SwapItem = new List<St__c>();
                    List<St__c> stt_old = new List<St__c>([Select Id,Ob__c from St__c where  Ob__c  =: recordOrder_new  and Id <>: currentRecord_old1  order by Ob__c ]); 
                    Decimal order;   
                    for(St__c s : stt_old) 
                    {
                         s.Ob__c = recordOrder_old;   
                    }  
                    Constants.runTrigger = false;
                    update stt_old; 
            }
        }
    }
 
}

 let me know if any issues in it.