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
umair ayazumair ayaz 

need help in selecting the correct records

Hello salesforce experts. I need help in order to update a custom field with apex trigger. Basically i have a table that shows all the accounts and i am using a custom field named order to sort the table according to the order field. Below is the picture of the table
User-added image

Now i am trying to update the other record's order numbers automatically when an order number is changed. E.g:- If i change order number "2" to "5", the record which alrady had the order number "5" should change its order number to "4" and old order number "4" should be changed to "3" and old order number "3" should be changed to "2". I have written a trigger for it and i am very close to the completed solution but the only problem i am facing is that when i  change order number "2" to "5" then there are 2 records with order number "5" and both of them change to order number "4". Please if anyone got some time then please check my codes and tell me the ways i could improve it. and if you are still not clear about the problem do let me know. My Trigger is atached below
 
trigger trigger1 on Account ( before update) {
    public List <Account> account=new List <Account>();
    public Integer currentRecordId {get;set;}
    public Integer num {get;set;}
    public Integer num1 {get;set;}
    account = [SELECT Id, Name  FROM Account WHERE Id IN :Trigger.New]; 
    public List <Integer> oldOrder=new List <Integer>();
    public List <Integer> newOrder=new List <Integer>();
    
    System.debug('run 1');
    
    for (Account acc: Trigger.new) {
        Account accOld = Trigger.oldMap.get(acc.id);
        
        system.debug('Old order number'+accOld.order__c);
        system.debug('new order number'+acc.order__c);
        
        try{
            
            if(acc.order__c != accOld.order__c) {                             
                // System.debug('---------Old order---------->'+accOld.order__c);
                // System.debug('---------new greaqter than old---------->'+acc.order__c);
                num = integer.valueOf(accOld.order__c);
                num1 = integer.valueOf(acc.order__c);
                oldOrder.add(num);
                newOrder.add(num1);            
            }
            
            
            if(acc.order__c > oldOrder[0] && acc.order__c <= newOrder[0]  ){
                
                System.debug('---------numbers in between---------->'+acc.Name);
                acc.order__c=acc.order__c-1;
                if(accOld.order__c == acc.order__c){
                    System.debug('---------Record with same order number---------->'+acc.Name);
                
            }

                
            }
           
            else if(acc.order__c < oldOrder[0] && acc.order__c > newOrder[0]){
                System.debug('---------numbers in between---------->'+acc.Name);
                Account a = new Account(id = acc.Id);
                acc.order__c=acc.order__c+1;
                
               
            }
            
        }
        catch(Exception e) {}
    }
    
}

 
Alain CabonAlain Cabon
Hi,

For this kind of need, we prefer to use big numbers for the numbering ( 10, 20, 30, 40, ... or 100, 200, 300, 400 instead of 1,2,3,4) 
So when you need to change the order, you will find a free location more easily (most of the time, until the saturation but it is easy to renumber all the values whatever happens).
Your sample is very theoretical. In the real world, we choose rules based on fields to compare the objects and we implement the comparable interface or/and we use a numeric field for a fixed order like you with big intervals because the work for reordering the values is heavy as you have noticed.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm

Regards