Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a string field says ServiceSegment = 'A' and a multiselect picklist Service_segment__c ='A;B;C'. I want to write a SOQL query which can compate string field to multiselect picklist. I tried this and getting error:

list<User> UsersInContext = [Select firstname, lastname, Related_BDE__c, Related_Team__c,Service_Segment__c, id 
                                                            from 
                                                            User 
                                                            where 
                                                            isactive = true 
                                                            AND
                                                            Service_Segment__c.split(';') =: serviceSegment
                                                            AND
                                                            On_Leave__c = false
                                                            order by 
                                                            Related_Team__c asc];

getting error unexpected token (;). please help me to resolve.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Use this syntax:

Service_Segment__c includes (:serviceSegment)

See the Querying Multi-Select Picklists documentation for the detail.

share|improve this answer
    
Thanks a lot @guy your solution with little correction applies perfectly. Short, crisp and correct. Cheers. –  Rohit Jun 26 at 9:41
add comment

You can't use .split() in a SOQL query. I can't see a way to elegantly do this with a single SOQL query, so I think you'll need to loop through the records and check them individually.

You may need to tweak the syntax a little as I've just quickly typed this up, but this should be the general idea I think:

List<User> UsersInContext = [
    SELECT 
        firstname, 
        lastname, 
        Related_BDE__c, 
        Related_Team__c,
        Service_Segment__c, 
        id 
    FROM 
        User 
    WHERE 
        isactive = true 
    AND
        On_Leave__c = false
    ORDER BY 
        Related_Team__c ASC
];

for (User u : UsersInContext) {

    Set<String> serviceSegmentValues = new Set<String>(u.Service_Segment__c.split(';'));

    for (String str : serviceSegmentValues) {

        if (str == serviceSegment) {

            // Matching value found - do something with this User
        }
    }
}
share|improve this answer
1  
I will try your answer later as it is very legitimate to use .split in a way you use. But answer provided by@guy is short and absolute (with little correction). Thanks anyways for your time. Cheers. –  Rohit Jun 26 at 9:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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