+ Start a Discussion
Linda 98Linda 98 

Trigger help with multiselect picklist values

My use case- Custom object linkedcontacts__c with Master detail relations on contact and customobject2__c objects.
Having two multi-select picklist fields on both the objects, Named as Roles.
I am having a trigger on customobject2 which fires when a field is changed, then I am trying to get those multi-select field values(roles) and check against contact roles, and link the related contacts.
My major issue is as they are multi-select values if multiple roles are chosen then how can I achieve this??

For instance, if I am having a CB2 record with roles director; manager, I should be able to query those contacts whose roles are director or manager or director and manager(if any) and should relate by inserting record in the linkedcontact__c object. 
Please guide me.Thank you.
 
Best Answer chosen by Linda 98
Andrew GAndrew G
Hi Linda

I have responed in the linked post.  Finally had time to do a full test in one of my dev orgs.

Hopefully that should get you across the line.

Regards
Andrew

All Answers

Andrew GAndrew G
A multi-select picklist should be a semi-colon separated string in Apex, therefore something like:
 
String[] roles= customObject2.Roles__c.split(';');
 
should work.

Then you could test using  contains
contains(listElement)
Returns true if the list contains the specified element.

Boolean result = roles.contains('Manager');
if (result) {
    //do some processing
}

Regards

AndrewG
Linda 98Linda 98
Thank you Andrew. But what about dynamic values? Like if extra picklist values are added from frontend?? Also,if there are 20-25 values, should I consider including each value like you mentioned?? (Contains manager)
Andrew GAndrew G
If i read that again:

You have a multi-select picklist which you want to let people add to dynamically?  oh , the horror.

Ok, firstly, how are you permitting values in the list that are not defined in the picklist?  I don't usually use multi-picklists, but a quick test in my test environment shows that the list is defined such when editing:
snippet showing a multi pick list
And i don't seem to be able to type any values into the picklist.  This in both classic and lightning.

Now, if you are saying that you have a way for someone to enter a different value e.g. "Banana Tester" and then you want to be able to use that in a query?  I would question what you are trying to do with the query.

But to answer the queston -  if we don't want to write the if statement above multiple times for each value in the list and get dynamic role names.

String[] roles= customObject2.Roles__c.split(';');

If the idea is to get some records - a soql query like  [SELECT Id,Name,Role FROM Contact WHERE Role IN :roles]


HTH
Andrew G
Linda 98Linda 98
Ok...may be my question is not clear. Let me explain it more detailed. I am having a custom object with a checkbox named as find. When the checkbox is set to true,I want to check roles field (multi-select picklist) ,and query against contact object to find out contact with those roles(multi-select picklist). Tricky part is,there might have multiple roles for a contact.In that case,we should check those contact,and link with another custom object, creating a link to first custom object.I am aware of creating records but my concern is how can I get those values, check them,and find contact with those roles. Hope this is clear. Thank you
Andrew GAndrew G
its still a little unclear , but such is the joy of forums and the written word.
I can't quite be clear on whether there is a second custom object in the mix, 
However, a trigger example might be:
 
trigger getContacts on MyCustomObject__c (after insert ) {

    List<String> roles = new List<String>();
    for (MyCustomObject__c my : trigger.new ) {
        if (my.checkbox ) {
            if (roles.size = 0 ) {
                roles = my.Roles__c.split(';');
            } else {
                roles.addall(my.Roles__c.split(';'));
            }
        }
    }

    List<Contact> contacts = new List<Contact>();
    contacts = [SELECT Id, Name, Role FROM Contact WHERE Role IN :roles];

    for( Contact contact : contacts ) {
        contact.myCustomObject__c = myCustomObject__c.Id;
    }
    update contacts;
}
In this case it would associated the contacts with the customobject being created.

It is not fully bulkified, but if this works on a single record, bulkification wouldn't be too hard with the use of a Map to identify the original customobject and only link the relevant contacts to that record.

Regards
Andrew
 
Linda 98Linda 98
Thank you, Andrew.

When I use query contacts=[select id, name, role from contact where Role IN: roles]
Then its just querying first contact.
if roles have values like [manager, employee, Analyst]

then it is only resulting in contacts with Manager role only.
Its ignoring contacts with Manager,Employee
Its ignoring contacts with role employee and architect.
Also ignoring contact if they have multiple roles. But as they are multi-select picklist fields, I cant limit to one value.

more exact explanation in below post.If you could help me with what am I missing, that would be great.
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000II1fQAG

Thank you.

 
Andrew GAndrew G
Hi Linda

I have responed in the linked post.  Finally had time to do a full test in one of my dev orgs.

Hopefully that should get you across the line.

Regards
Andrew
This was selected as the best answer