1

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;
}
3
  • Which line is throwing that error?
    – Adrian Larson
    Jan 10, 2017 at 16:50
  • Line 9 the If() statement. Jan 10, 2017 at 17:05
  • you dont need if condition in for condition, check the logic at my answer Jan 10, 2017 at 17:06

2 Answers 2

4

You should be getting a compile fail from this line:

if (!existingAccTeam.contains(accId.User__c))

Your accId variable is of the Id type, and that type has no member of the name User__c. Perhaps you meant to get the corresponding Id from your map? If so, you would change it to:

if (!existingAccTeam.contains(accOwner.get(accId))

But you'd still have a compile fail, because existingAccTeam is currently a Set<NPD_Account_Team__c>, which cannot possibly contain an Id. You likely also need to change the type of existingAccTeam to Set<Id>. Then you'll just have to loop through your query results.

Set<Id> existingAccTeam = new Set<Id>();
for (NPD_Account_Team__c record : [/*query*/])
{
    existingAccTeam.add(record.User__c);
}
1

You can filter the users in the SOQL query like this. accOwner contains Map of Ids, you can use the Map.values() in the query.:

AND User__c NOT IN:accOwner.values()

Then you do not need to check with if condition in for loop.

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()
                            AND User__c NOT IN:accOwner.values()]);

    for(Id accId : accOwner.keySet())
    {
        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;
}
2
  • Is an if in for loop bad? Because I do not want to create the record if there is already one created for that user I think to carry this further I need to add if(exisitingAccTeam.isEmpty(){ create record} in the for loop. Jan 10, 2017 at 17:12
  • there are many ways to optimize the code, since you are filtering the SOQL then better to filter there Jan 10, 2017 at 17:43

You must log in to answer this question.

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