+ Start a Discussion
turbo2ohturbo2oh 

simple syntax question

What is wrong with this syntax inside a before insert/before update trigger please?

 

set <id> locationIds= new set<id>{ trigger.newMap.keySet()};

 

 

I'm getting the error: Invalid initial value type SET<Id> for SET<Id> which isn't very helpful. I'm just trying to define a variable name for the trigger.newMap.keySet() so its more readable.

Naidu PothiniNaidu Pothini
set <id> locationIds= new set<id>( trigger.newMap.keySet() );

 You are using the wrong braces. 

turbo2ohturbo2oh

Thanks! Also, I realized the fundamental problem, there isn't going to be an id for a before insert trigger since its not inserted yet.

sfdcfoxsfdcfox

You can also do this:

 

set<id> locationids = trigger.newmap.keyset().clone();

But you might not even need to do that. After all, you can use Trigger.new in a query (it implements Iterable, and can be used directly).

 

for(obj__c rec:[select id from rec__c where parent__c in :trigger.new]) {
  ...
}

Which saves a bit on heap space (not that a set of IDs will abuse the heap too much...).

 

Edit: Yea, I just noticed that you had it in a before insert trigger. That recently found its way to my attention as well in my own project.