Knowledge Share - Single value field in multi select (Saved Search and Workflow)
I have been working on a problem for a client where we need to compare a multiselect field to the contents of a single value field. I have run across several examples out there where people used regular expressions, and other concepts that gave me the structure for doing this, but I couldn't make them work. So I took their approaches and worked with Stephen Anderson to come up with some alternatives.
I first created a saved search using a custom formula that looks like the following:
TO_CHAR(INSTR({multiselect field}||',',{comparison field}||','))
When evaluating this - 0 means it doesn't show up, 1 or greater means it is in there.
To break down this formula and what it's doing:
it is concatenating a comma after the multiselect field because your results show up as "Value1,Value2,Value3"
Adding a comma turns it into "Value1,Value2,Value3,"
This is important because it allows my comparison field to look for strict matches.
Without that, if my comparison field contains "Value" then I'd get a positive result - even though it's not a full match.
Of course you also have to concatenate the comma after your comparison field. So that if you search for "Value" you are actually looking for "Value," which would return a zero result. So if you were looking for "Value1" you'd get result "Value1," to compare against and get a true value.
It should be noted - this wouldn't be a perfect solution if your data set included commas in the values you were searching against, because then partial string matches would become possible again resulting in false positives.
Once I came up with the approach - it was time to use it in a workflow. Unfortunately, given that this is oracle sql only - it was causing my workflow to bomb out and the record would stop loading since I needed it to be 1.0 script for a client event (before user submit)
I quickly did a comparison formula of a standard formula and figured out that it was using script to pull the comparison instead.
With a little help from Gustavo, I was able to tweak my formula into the following:
(script to get multiselect field value.concat(',')).indexOf(script to get single field value.concat(','))
In my case - I was joining to the customer record from a sales order - so I had to do a "lookupfield" for one of my results, and the other was just a get field value since it was on the current record.
One change in the javascript comparison versus the sql comparison - is that if it isn't present the value to compare is equal to -1, and if it is present it is greater than or equal to zero
Here's the final formula I used that is working. Note that a number of these came by trial and error of setting up my criteria in the workflow criteria builder and then switching to the custom formula page and extrapolating from there.
(nlapiLookupField('customer',nvl(nullIfEmpty(nlapiGetFieldValue('entity')),-1),'custentity_po_free_ship_methods',false).concat(',')).
indexOf(nlapiGetFieldValue('shipmethod').concat(','))>=0
Figured I'd share this since it was something I knew could be done in workflow - it was just a matter of incremental steps to figure it out (that and I want to be able to find it again in the future!)