7

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.

  • 3
    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. – Antonio Manente Jan 10 '17 at 16:10
13

Please close the if condition with extra parens.

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

}
  • 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? – Dan Wooding Jan 10 '17 at 16:14
  • yes, you can put a new question – Santanu Boral Jan 10 '17 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.