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
Sainath VenkatSainath Venkat 

trigger for auto number based on parent record salesforce

Is it possible to have a trigger to auto name child records based on Parent Record.
I have two objects which are having lookup relationship.
1) BuildCycle__c
2)ActionItem__c

Each build cycle can have multiple action items and I want to name all action items for a particular build cycle in sequence.

For example,
BUILDCYCLE Record1 has 2 action items and sequence should be
 1) ActionItem1
 2) ActionItem2
BUILDCYCLE Record1 has 3 action items and sequence should be
 1) ActionItem1
 2) ActionItem2
 3) ActionItem3

Can anyone guide me on this how to achieve
Somya TiwariSomya Tiwari
It is quite simple, and it can be ahieved with the help of a Trigger on your ActionItem1. Please find here with the code to do the same.
trigger Rename on Car__c (before insert) {
    for(Car__c car : Trigger.new )
    {
        List<Car__c> blg = [SELECT ID FROM Car__c where Blog__c = :car.Blog__c];
     	car.Name = 'Car '+String.valueOf(blg.size() + 1);
    }
}


Please mark the answer as best answer in case this worked for you :)
Sainath VenkatSainath Venkat
Hello Somya Tiwari,

Thanks for your reply and fr helping me out, actually I knew this but I am more concerned about bulkification,

Lets say I am inserting 50 records using data loader of which 25 records are related to 1 BULDCYCLE1 and 25 related to BUILDCYCLE2, in that case the code which you provided will not work actually.
Deepali KulshresthaDeepali Kulshrestha
Hi Sainath,

Greetings to you!

Use the following code. It may helpful for you
Trigger

trigger Rename on ActionItem__c (before insert) {
    if(trigger.isInsert && trigger.isBefore){
        RenameActionItem.renameActionItemRecord(trigger.new);
    }
}

Apex Controller

public class RenameActionItem {
    public static void renameActionItemRecord(List<ActionItem__c> actionItemList){
        try{
            integer length1 = actionItemList.size();
            
            Set<Id> buildCycleId = new Set<Id>();
            for(ActionItem__c actionObj : actionItemList) {
                buildCycleId.add(actionObj.BuildCycle__c);   
            }  
            
            List<ActionItem__c> actionItemList2 = new List<ActionItem__c>();
            actionItemList2 = [SELECT Id FROM ActionItem__c WHERE BuildCycle__r.Id IN :buildCycleId]; 
            
            List<BuildCycle__c> buildCycleList = new List<BuildCycle__c>();
            buildCycleList = [SELECT Name, (SELECT Id FROM ActionItems__r) FROM BuildCycle__c WHERE Id IN: buildCycleId];
            
            for(BuildCycle__c bObj : buildCycleList){
                Integer length2 = bObj.ActionItems__r.size();
                
                for(ActionItem__c cObj : actionItemList){
                    if(bObj.Id == cObj.BuildCycle__c){
                        
                        if(length2 < length2+length1){
                            cObj.Name = 'ActionItem'+length2;
                            
                        } 
                        length2++;
                        
                    }
                }
            }
            
        }catch(Exception e){
            System.debug('Exception in code'+e.getCause() + 'Exception in Line number'+e.getLineNumber());
        }
    }
}
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com  
Sainath VenkatSainath Venkat
Hello Deepali Kulshrestha,

Hope you doing great and thanks a lot for helpng me out in this issue, your code working good but lets say for one build cycle I have 4 action items
BUILD CYCLE:1
1) Actionitem1
2) Actionitem2
3) Actionitem3
4) Actionitem4

now count is 4 and if I delete Actionitem3 and creates Actionitem5 then name should be Actionitem5 but now its creating Actionitem4 only, can you help me out in this issue here.

Here is my code
 
public class RenameActionItem {
    public static void renameActionItemRecord(List<Action_Item__c> actionItemList){
        try{
            integer length1 = actionItemList.size();
            
            Set<Id> buildCycleId = new Set<Id>();
            for(Action_Item__c actionObj : actionItemList) {
                buildCycleId.add(actionObj.DCS_Build_Cycle__c);   
            }  
            
            List<Action_Item__c> actionItemList2 = new List<Action_Item__c>();
            actionItemList2 = [SELECT Id FROM Action_Item__c WHERE DCS_Build_Cycle__r.Id IN :buildCycleId]; 
            
            List<Build_Cycle__c> buildCycleList = new List<Build_Cycle__c>();
            buildCycleList = [SELECT Name, (SELECT Id FROM Action_Items__r) FROM Build_Cycle__c WHERE Id IN: buildCycleId];
            
            for(Build_Cycle__c bObj : buildCycleList){
                Integer length2 = bObj.Action_Items__r.size();
                
                for(Action_Item__c cObj : actionItemList){
                    if(bObj.Id == cObj.DCS_Build_Cycle__c){
                        
                        if(length2 < length2+length1){
                            cObj.Auto_Number__c = length2;
                            
                        } 
                        length2++;
                        
                    }
                }
            }
            
        }catch(Exception e){
            System.debug('Exception in code'+e.getCause() + 'Exception in Line number'+e.getLineNumber());
        }
    }
}