You need to sign in to do that
Don't have an account?
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 ;
}}}
What is the problem? What is the problem´s message?
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
Just updated in delete trigger
to
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
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.
5 to 7 values should be changed to 4 to 6 and the old 8th number should be changed to 7
or can u provode me the code for swapping. like if 5 is updated to 1 then 1 should become 5
Swaping will cause cyclic trigger so it I have stoped cyclic execution using static variable. First Create Static variable like
Here is your trigger which will swap in case of edit
let me know if any issues in it.