9

I wrote a method last night that creates a new custom object record that is related to an Account and a User. it works great however, it breaks when there is an already existing record for a user. After research I think I need a set to compare if the user value already exists then not run the logic.

I can't get the syntax right to compile and see if it works. Example code:

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 Id, User__c, Account__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;
}

I get the error on line 10:

Unexpected token '{'

What am I doing wrong? I know that it does not need a ; at the end of the if line but I am not sure why it will not save.

1
  • 4
    Missing parentheses after the contains method call.. Also, accId is an Id so accId.User__c is likely to throw an error once you fix that. Commented Jan 10, 2017 at 16:10

1 Answer 1

17

Please close the if condition with extra parens.

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

}
2
  • 1
    Damn, I knew it had to be something simple like that. I get a different error now though. Should that be a new question? Commented Jan 10, 2017 at 16:14
  • yes, you can put a new question Commented Jan 10, 2017 at 16:15

You must log in to answer this question.

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