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 

sequence number field on contact object than i want to update the sequnce number of this fields

hello,
i am new in salesforce so please help
i have two standard object Account(parent) and Contact(child) .so i wanna create a custom field  'sequence number' on Contact .
suppose my Account(parent) Object have 5 Contact(child) the 'sequence number' record is 1,2,3,4,5      Than i will update the 3 'sequence number' in update 5  than  new 'sequence number' is 1,2,5,4,3  
this is done help by 'Apex Trigger'.
help my and example with code  
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

I have created a custom field on the Contact object with the API name Sequence_Number__c
 
trigger ConSequence on Contact (after insert, after update, after delete) {
    
    list<Contact> apptsToUpdate=new list<Contact>();
    Set<Id> parentIds = new Set<Id>();
    list<Account> parentAccById = new list<Account>();
    
    if(trigger.isdelete){
        if(ConSeqRecursive.apptSequenceNo == true){ // global flag to stop recursive trigger
            ConSeqRecursive.apptSequenceNo = false;
            for(Contact con: Trigger.old){
                parentIds.add(con.AccountId);
            }
            
            if(parentIds.size() >0){    
                parentAccById=[SELECT ID, Name, (select Id, Name, Sequence_Number__c from Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentAccById.size() >0){
                for(Account leadObj: parentAccById){
                    for(Contact app: leadObj.Contacts){
                        Contact cc = [SELECT Id, Name, Sequence_Number__c FROM Contact WHERE Id IN:trigger.old ALL ROWS];
                        if(app.Sequence_Number__c > cc.Sequence_Number__c){
                        	app.Sequence_Number__c = app.Sequence_Number__c -1;
                        }
                        apptsToUpdate.add(app);      
                    }
                }
                UPDATE apptsToUpdate;
            }
        }
        ConSeqRecursive.apptSequenceNo = true;
    }
    
    if(trigger.isinsert || trigger.isinsert){
        if(ConSeqRecursive.apptSequenceNo == true){ // global flag to stop recursive trigger
            ConSeqRecursive.apptSequenceNo = false;
            for(Contact fa: Trigger.New){
                parentIds.add(fa.AccountId);
            }
            
            if(parentIds.size() >0){    
                parentAccById=[SELECT ID, Name, (SELECT Id, Name FROM Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentAccById.size() >0){
                for(Account leadObj: parentAccById){
                    Decimal sqno = 0;
                    for(Contact app: leadObj.Contacts){
                        sqno = app.Sequence_Number__c = sqno+1;
                        apptsToUpdate.add(app);      
                    }
                }
                UPDATE apptsToUpdate;
            }
        }
        ConSeqRecursive.apptSequenceNo = true; 
    }
}

Create a class to prevent recursive trigger:
public class ConSeqRecursive {

    public static Boolean apptSequenceNo = true;
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas​​​​​​​
kumar 420kumar 420
sir this is not work when we update the sequnce number
this is work when we update sequnce number and after i will insert the new record in this contact object
kumar 420kumar 420
so give me answer that work on update .  if i update one Sequnce number than only chage two values that mean 
i have sequnce number 1  2   3  4   5    and i will update the 3 hence 5   than   the sequnce is   1   2   5  4   3  only chage that 3  and 5 and not chage another sequnce number
please give me answer
thanks