You need to sign in to do that
Don't have an account?
Why can't I have a Map<Id, Integer> ?
According to SFC documentation "values can be a primitive". An Integer is a primitive.
I am building a trigger that requires me to do a check on how many Circuits have already been defined. If the user is defining the first circuit, all fields are required. I had the count in a for loop and it works fine until I break the bulk trigger rules with Too many SOQL queries
Integer mycount = [select count() from Circuit__c where Device__c = :circ.Device__c];
So I thought I would put the count in a Map and correlate it to an Id.
Map<Id, Integer> circCountMap = new Map<Id, Integer> ([SELECT count() FROM Circuit__c WHERE Device__c in:devIds]);
But when I try to save, I get an error
Error: Compile Error: Invalid initial type Integer for MAP:Id,String at line 13 column 35 |
Why can't I do this? And does anybody have a suggestion for how I can get the counts so I can check each Circuit being inserted?
Thank you
According to this documentation http://www.salesforce.com/us/developer/docs/apexcode/index.htm you can only define maps where the first parameter, the key must be of the type Id or String, and each one must be unique and distinct from others
I have a feeling that you might want to use an AggregateResult. Can you post the rest of the trigger so we can see it in context?
You may consider using an Aggregate function:
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm
Thank you for all the responses. Below is the original trigger and the count() query is inside the for loop and causes problems on bulks.
A few things.
1. You can have a Map<Id, Integer>, the problem is that the auto map constructor from SOQL statements is always Id -> record type.
2 your select count() query will only ever return one row, so this is not generating the data set you think it is.
As others as suggested, you'll need to use an aggreage query, and build the map yourself, something like
Map<Id, Integer> counts = new Map<Id, Integer>();
for (AggregateResult r : [select device__c, count(device__c) cnt from circuit__c where device__c in :devIds group by device__c]) {
counts.put(r.get('device__c'), r.get('cnt'));
}
Hi Simon,
That looked really promising but throws a compilation error
Error: Compile Error: Invalid type: AggregateResult at line 14 column 4
I then tried a variation of that as documented in Apex guide but got similar results. Here was the other way I tried
it
Make sure your Apex Code is set to v18.