0

As the title suggests I just want it so that on insert/save/delete a number field on a custom object auto calculates the field so that the max value is the same as the size of the list for all records of the custom object with the same ParentId.

I thought it may of been as simple as the following but doesn't work:

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);
        }    
        Integer temp = 0;
        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){ 
            temp = temp + 1; 
            cgoal.Priority__c =  temp;

        }
        if(existingcgoal != null && existingcgoal.size() > 0){
            update existingcgoal;
        }
    }
2
  • Rather than looping through existingcgoal, can you not just set Priority__c = existingcgoal.size()? Are you just trying to set Priority__c to the size of List<Client_Personal_Goal__c>?
    – Dan Jones
    Commented Sep 12, 2016 at 8:17
  • No I am trying to get each record in the list to have a unique priority value from 1 to n, where n is the size of the list.
    – Michael
    Commented Sep 12, 2016 at 23:04

2 Answers 2

1

Try this

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 list<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;

}

3
  • I get the following error which: list must have exactly 1 type argument. It looks like its coming from line 4: Map<Id,Integer> mapClientPersonalGoaltoCount = new list<Id,Integer>();//Map of Cilent Personal Id to child count
    – Michael
    Commented Sep 12, 2016 at 22:55
  • I fixed up that line to Map<Id,Integer> mapClientPersonalGoaltoCount = new Map<Id,Integer>(); along with other issues you had but I get the following error now: Method does not exist or incorrect signature: [Map].containsKey()
    – Michael
    Commented Sep 12, 2016 at 23:20
  • //If map doesn't contain add add the id with count as 1 if(!mapClientPersonalGoaltoCount.containsKey()) Please edit the above line to if(!mapClientPersonalGoaltoCount.containsKey(cgoal.Id)) Apologies for the typo Commented Sep 15, 2016 at 12:16
0

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.

Best Regards,

Nagendra

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .