You need to sign in to do that
Don't have an account?
Linda 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.
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.
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
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
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:
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
I can't quite be clear on whether there is a second custom object in the mix,
However, a trigger example might be:
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
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.
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