How do I check if an element is in a set apex?

1.7K    Asked by Ayushigupta in Salesforce , Asked on Sep 22, 2022

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 accOwner)
{
    List npdAccTeamToInsert = new List();
    Set existingAccTeam = new Set();
    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.
Answered by Ayushi gupta

To check if an element is in set apex, please close the if condition with extra parens.


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

Your Answer

Answer (1)

In Salesforce Apex, checking if an element is in a set is straightforward using the contains method provided by the Set class. This method returns a Boolean value indicating whether the specified element exists in the set.


Example Usage

Below are detailed steps and examples to demonstrate how you can check if an element is in a set in Apex.

Step-by-Step Example:

Create a Set

First, create a set and add some elements to it.

Set fruits = new Set{'Apple', 'Banana', 'Orange'};
Check If an Element Exists in the Set
Use the contains method to check if a specific element is in the set.String fruitToCheck = 'Banana';
Boolean isPresent = fruits.contains(fruitToCheck);
if (isPresent) {
    System.debug(fruitToCheck + ' is in the set.');
} else {
    System.debug(fruitToCheck + ' is not in the set.');
}ull Example in an Apex Class
Here is a complete example within an Apex class:public class SetExample {
    public static void checkElementInSet() {
        // Create a set and add elements to it
        Set fruits = new Set{'Apple', 'Banana', 'Orange'};
        // Element to check
        String fruitToCheck = 'Banana';
        // Check if the element is in the set
        Boolean isPresent = fruits.contains(fruitToCheck);
        // Output the result
        if (isPresent) {
            System.debug(fruitToCheck + ' is in the set.');
        } else {
            System.debug(fruitToCheck + ' is not in the set.');
        }
    }
}

Testing the Code

To test the code, you can call the checkElementInSet method from the Developer Console:

SetExample.checkElementInSet();
6 Months

Interviews

Parent Categories