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 

Create custom field on Contact for sequence number. by help trigger to manage sequence of Contacts on an Account

hello,
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 delete the One 'sequence number' record 3  so my 4 number come to 3  and 5 number is come to 4.    the new 'sequence number' is 1,2,3,4  
if i will delete any record than the sequence of record number is maintan.
this is done help by 'Apex Trigger'.
help my and example with code  
 
Best Answer chosen by kumar 420
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kumar,

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.

Trigger:
trigger ConSequence on Contact (after insert, after update, after delete) {
    
    list<Contact> apptsToUpdate=new list<Contact>();
    Set<Id> parentIds = new Set<Id>();
    list<Account> parentLeadsById = 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){    
                parentLeadsById=[SELECT ID, Name, (select Id, Name, Sequence_Number__c from Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentLeadsById.size() >0){
                for(Account leadObj: parentLeadsById){
                    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){    
                parentLeadsById=[SELECT ID, Name, (SELECT Id, Name FROM Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentLeadsById.size() >0){
                for(Account leadObj: parentLeadsById){
                    Decimal sqno = 0;
                    for(Contact app: leadObj.Contacts){
                        sqno = app.Sequence_Number__c = sqno+1;
                        apptsToUpdate.add(app);      
                    }
                }
                UPDATE apptsToUpdate;
            }
        }
        ConSeqRecursive.apptSequenceNo = true; 
    }
}

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​​​​​​​

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Kumar,

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.

Trigger:
trigger ConSequence on Contact (after insert, after update, after delete) {
    
    list<Contact> apptsToUpdate=new list<Contact>();
    Set<Id> parentIds = new Set<Id>();
    list<Account> parentLeadsById = 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){    
                parentLeadsById=[SELECT ID, Name, (select Id, Name, Sequence_Number__c from Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentLeadsById.size() >0){
                for(Account leadObj: parentLeadsById){
                    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){    
                parentLeadsById=[SELECT ID, Name, (SELECT Id, Name FROM Contacts ORDER BY CreatedDate ASC ) FROM Account Where  Id IN :parentIds]; 
            }
            
            if(parentLeadsById.size() >0){
                for(Account leadObj: parentLeadsById){
                    Decimal sqno = 0;
                    for(Contact app: leadObj.Contacts){
                        sqno = app.Sequence_Number__c = sqno+1;
                        apptsToUpdate.add(app);      
                    }
                }
                UPDATE apptsToUpdate;
            }
        }
        ConSeqRecursive.apptSequenceNo = true; 
    }
}

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​​​​​​​
This was selected as the best answer
kumar 420kumar 420
thanks khan anas give your answer
kumar 420kumar 420
hello khan anas 
can you give me answer on update query on this custom field
saisurendra seshapusaisurendra seshapu
Very Big Thank to 
Khan Anas  , pls can u share update scenario also
 
PavitraPavitra
hello,
Same as above. But, Small Change in the question
If Ihave 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 delete the One 'sequence number' record 3  so my 4 number  should not come to 3  and 5 number should not come to 4. The new 'sequence number' is 1,2,4,5 . The place of 3 should not be filled. If we want add new Contact 6, then the sequence should continue like 1, 2, 4, 5, 6
If  I will delete any record than the sequence of record number is maintaned and the deleted field should not be filled.
this is done help by 'Apex Trigger'.
help my and example with code 
Mohit Sharma 403Mohit Sharma 403
But this code will give error for bulk.