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
Phani Pydimarry 4Phani Pydimarry 4 

Salesforce - Create index for child records

I have an account and I have multiple child records for this Account.
I would like to create a custom field and then update 1 for the first record created and if another record is created I want a field to populate 2 on the second and so on.
I want to develop this functionality because I want to track first record, second record for our analytics.
Please let me know if this is possible
Abhishek BansalAbhishek Bansal
Hi Phani,

So you want to keep the number of child records in that custom field. If this is what your requirement is, then you can simply do this with the help of trigger.Please let me know if you need any additional inofrmation on this.

Thanks,,
Abhishek Bansal.
Phani Pydimarry 4Phani Pydimarry 4
Hello Abhishek,

I would explain the scenario again. I need a field on each reacord of child object which would have a Rank as 1 if the record is 1 and Rank 2 if the record is 2. 

This field would be helpful for us to track the first sale which was done by the Account holder. I hope I am clear


Thanks ,
Vineeth Pydimarry.
 
Abhishek BansalAbhishek Bansal
Hi Vineeth,

Yes, now your requirement is clear and I have written the trigger for the same.
trigger assignRank on CustomObject__c(before insert){
	Set<Id> accIds = new Set<Id>();
	
	for(CustomObject__c cusObj : trigger.new) {
		if(cusObj.Account__c != null) {
			accIds.add(cusObj.Account__c);
		}
	}
	
	if(accIds.size() > 0) {
		Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select (Select id from CustomObjects__r) from Account]);
		
		for(CustomObject__c cusObj : trigger.new) {
			if(cusObj.Account__c != null && mapOfAccounts.containsKey(cusObj.Account__c)) {
				cusObj.Rank__c = 0;
				cusObj.Rank__c = mapOfAccounts.get(cusObj.Account__c).CustomObjects__r.size() + 1;
			}
		}
	}
}

//Replace CustomObject__c on Line #1,4 with API name of your child object
//Replace Account__c on Line #5,6,14,16 with API name of Account field on the child object
//Replace CustomObjects__r on line #11,16 with the child relationship name of the child object
//Replace Rank__c on line #15,16 with the API name of the custom field in which you want to hold the rank of the child.
Please take care of the API names and syntax errors and let me know if you face any other issue with this.

Thanks,
Abhishek Bansal.
Phani Pydimarry 4Phani Pydimarry 4
Thanks Abhishek, I would let you know if I am struck but this Idea is awesome
Phani Pydimarry 4Phani Pydimarry 4
Hello Abhishek,

I have received the null point exception so I have limited my query with 10000 records. Now the record on the object - Order saves but there is no rank which is generated by this process.
 
trigger assignRank on Order__C(before insert){
        Set<Id> accIds = new Set<Id>();
        
        for(Order__c Ord : trigger.new) {
               if(Ord.Distributor__c != null) {
                       accIds.add(Ord.Distributor__c);
               }
        }
        
        if(accIds.size() > 0) {
               Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select (Select id from Orders__r) from Account limit 10000]);
               
               for(Order__c Ord : trigger.new) {
                       if(Ord.Distributor__c != null && mapOfAccounts.containsKey(Ord.Distributor__c)) {
                               Ord.Rank__c = 0;
                               Ord.Rank__c = mapOfAccounts.get(Ord.Distributor__c).Orders__r.size() + 1;
                       }
               }
        }
}

 
Abhishek BansalAbhishek Bansal
Hi Phani,

You are not filtering the account that you need for setting up the Rank field. I have modifed your code :
trigger assignRank on Order__C(before insert){
        Set<Id> accIds = new Set<Id>();
        
        for(Order__c Ord : trigger.new) {
               if(Ord.Distributor__c != null) {
                       accIds.add(Ord.Distributor__c);
               }
        }
        
        if(accIds.size() > 0) {
               Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select (Select id from Orders__r) from Account where Id IN :accIds]);
               
               for(Order__c Ord : trigger.new) {
                       if(Ord.Distributor__c != null && mapOfAccounts.containsKey(Ord.Distributor__c)) {
                               Ord.Rank__c = 0;
                               Ord.Rank__c = mapOfAccounts.get(Ord.Distributor__c).Orders__r.size() + 1;
                       }
               }
        }
}
I assume that the Distributor__c field holds the Account record. Please try with the updated code and let me know if there is any issue.

Thanks,
Abhishek Bansal.