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
Michael MinnekeerMichael Minnekeer 

How can I increment field per ParentId(same way assignment rules auto calculate order)

As the title suggests I just want it so that on insert/save/delete a number field on a custom object auto calculates all records with the same parent Id  from 1 to n where n is the size of the list of records with same parentId. The best example I can give is assignment rules in Salesforce as this behaviour is the exact thing I need.

I am unsure how to refer to the previous record value in a list to keep incrementing the integer. This field needs to be updatable so autonumbering is out of the question. Also I was hoping this to be a solution that contains no error messages so that if a user is inserting 2 records with the same value it auto calculates that (the user is inserting these records via add row visualforce solution so on save it updates a list).

I am unsure if this method is on the right track because if I need to change the value of the record in trigger.new I cannot update it again in the same transaction with before or after trigger.
 
public static void checkpriority(Client_Personal_Goal__c[] cgoals){
        	list<Client_Personal_Goal__c> existingcgoal = new list<Client_Personal_Goal__c>();
            Set<Id> preclaraId = new set<Id>();
            for(Client_Personal_Goal__c cgoal : cgoals){ 
                preclaraId.add(cgoal.Related_Pre_Clarification__c);
            }    
        	

        
            existingcgoal = [select Id, Priority__c 
                             from Client_Personal_Goal__c 
                             where Related_Pre_Clarification__c in: preclaraId 
                             ORDER BY Priority__c ASC];
        	for(Client_Personal_Goal__c cgoal : existingcgoal){
                     //increment all values again?
            }
        
            try{
            if(existingcgoal != null && existingcgoal.size() > 0 ){
            	update existingcgoal;
            }
            }catch(exception e){}

    }



 
NagendraNagendra (Salesforce Developers) 
Hi Michael,

Try the below code
public static void checkpriority(Client_Personal_Goal__c[] cgoals){
List<Client_Personal_Goal__c> lstClientPersonalGoalToUpdate = new List<Client_Personal_Goal__c>();//List to update Priority Count
Map<Id,Integer> mapClientPersonalGoaltoCount = new Map<Id,Integer>(); //Map of Cilent Personal Id to child count 

Set<Id> preclaraId = new set<Id>();

for(Client_Personal_Goal__c cgoal : cgoals){ 
    preclaraId.add(cgoal.Related_Pre_Clarification__c);
}    

Integer temp = 0;
existingcgoal = [];

for(Client_Personal_Goal__c cgoal : select Id, Priority__c 
                                    from Client_Personal_Goal__c 
                                    where Related_Pre_Clarification__c in: preclaraId 
                                    ORDER BY Priority__c ASC){ 

    //If map doesn't contain add add the id with count as 1
    if(!mapClientPersonalGoaltoCount.containsKey())
        mapClientPersonalGoaltoCount.put(cgoal.Id, 1);
    //If map already had Id update the count by adding plus 1   
    else
        mapClientPersonalGoaltoCount.put(cgoal.Id, mapClientPersonalGoaltoCount.get(cgoal.Id)+1);
}

for(Id clientPersonalId : mapClientPersonalGoaltoCount.keySet()) {
    lstClientPersonalGoalToUpdate.add(new Client_Personal_Goal__c(Id=clientPersonalId,
                                                                  Priority__c =mapClientPersonalGoaltoCount.get(clientPersonalId));
}
update lstClientPersonalGoalToUpdate;
}


You are getting the above error method does not exist or invalid signature because in the below line of code
if(!mapClientPersonalGoaltoCount.containsKey())
You are not specifying the key.
So the reason it's throwing you an error method does not exist or invalid signature.

Please pass the key in the above line and compile the code you will be able to do it.

Please mark this post as solved if it helps.

Best Regards,
Nagendra.P