I'm trying to see if a user ID is contained in a set. However, I cannot get the class to compile. In my code example, I have the if statement as follows which gives the error:
Initial term of field expression must be a concrete Sobject: Id
public static void createNPDAccountTeam(Map<Id, Id> accOwner)
{
List<NPD_Account_Team__c> npdAccTeamToInsert = new List<NPD_Account_Team__c>();
Set<NPD_Account_Team__c> existingAccTeam = new Set<NPD_Account_Team__c>();
existingAccTeam.addAll([SELECT User__c FROM NPD_Account_Team__c WHERE Account__c IN: accOwner.keySet()]);
for(Id accId : accOwner.keySet())
{
if(!existingAccTeam.contains(accId.User__c))
{
npdAccTeamToInsert.add(new NPD_Account_Team__c(
Account__c = accId,
User__c = accOwner.get(accId),
Role__c = 'Custom',
Team_Member_Status__c = 'Active'
));
}
}
insert npdAccTeamToInsert;
}
based on previous answers on stackexchange I tried to change the if line to this:
if(!existingAccTeam.contains(accId.get(accId).User__c))
I get the error:
Method does not exist or incorrect signature: [Id].get(Id)
How can I check to see if the user__c lookup value is in my select query?
EDIT:
Final Working code after suggestions:
public static void createNPDAccountTeam(Map<Id, Id> accOwner)
{
List<NPD_Account_Team__c> npdAccTeamToInsert = new List<NPD_Account_Team__c>();
Set<Id> existingAccTeam = new Set<Id>();
// Check if there is an existing NPD Account Team record for the user. If so add to the set.
for( NPD_Account_Team__c record :[SELECT User__c FROM NPD_Account_Team__c
WHERE Account__c IN: accOwner.keySet()
AND User__c IN: accOwner.values()])
{
existingAccTeam.add(record.User__c);
}
// If the set is empty meaning that there is not an existing NPD Account Team record for that user, insert a new NPD Account Team Record
for(Id accId : accOwner.keySet())
{
if(existingAccTeam.isEmpty())
{
npdAccTeamToInsert.add(new NPD_Account_Team__c(
Account__c = accId,
User__c = accOwner.get(accId),
Role__c = 'Custom',
Team_Member_Status__c = 'Active'
));
}
}
insert npdAccTeamToInsert;
}