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
kumar 420kumar 420 

Update Sequnce Number Fields On Contact Object using before update trigger

i Wanna chage this after trigger in before Trigger So we do some help.    
This is my helper class :-HelpClassForSequenceNumberTriggerT:-

public void forUpdateRecordInSequenceField( Contact contactallnewrecord , Contact contactalloldrecord ) {
        Decimal lmt=1;
       accountrecordids.add(contactallnewrecord.accountID);
        List<Contact> contactsameaccountrecord = [Select id,name,Sequence_Number__c from Contact where accountid = :accountrecordids];
        if(contactallnewrecord.Sequence_Number__c > contactalloldrecord.Sequence_Number__c) {
            lmt=contactallnewrecord.Sequence_Number__c - contactalloldrecord.Sequence_Number__c;
        }
        if(contactalloldrecord.Sequence_Number__c > contactallnewrecord.Sequence_Number__c) {
            lmt=contactalloldrecord.Sequence_Number__c - contactallnewrecord.Sequence_Number__c;
        }    
      
        for(Contact singlerecordofcontact : contactsameaccountrecord ) {
            
            if(singlerecordofcontact.Sequence_Number__c > contactalloldrecord.Sequence_Number__c  && singlerecordofcontact.Sequence_Number__c <= contactallnewrecord.Sequence_Number__c && singlerecordofcontact.id != contactallnewrecord.id)  {
                
                for( ;i<=lmt;i++) {
                    singlerecordofcontact.Sequence_Number__c= singlerecordofcontact.Sequence_Number__c-1;
                    System.debug(singlerecordofcontact.Sequence_Number__c);
                    forupdatecontactrecord.add(singlerecordofcontact);
                    break;
                }  
            }
            else if(singlerecordofcontact.Sequence_Number__c < contactalloldrecord.Sequence_Number__c  && singlerecordofcontact.Sequence_Number__c >= contactallnewrecord.Sequence_Number__c && singlerecordofcontact.id != contactallnewrecord.id)  {
                
                for( ;i<=lmt;i++){
                    singlerecordofcontact.Sequence_Number__c= singlerecordofcontact.Sequence_Number__c+1;
                    System.debug(singlerecordofcontact.Sequence_Number__c);
                    forupdatecontactrecord.add(singlerecordofcontact);
                    break;
                    
                }  
            } 
        }
       update forupdatecontactrecord;
    }
   



And This is my after update Trigger:-
if(Trigger.Isupdate)
    {
            for(Contact onenewrecord : Trigger.new )
            {
                for(Contact oneoldrecord : Trigger.old )
                {
                  
                    if(onenewrecord.Sequence_Number__c != oneoldrecord.Sequence_Number__c && onenewrecord.account.id == oneoldrecord.account.id)
                    {
                        obj.forupdateRecordInSequenceField(onenewrecord,oneoldrecord );
                    }
                 break;
  
            }

        }
    }