Friday, 20 November 2015

SharePoint Designer - Test if List Item Exists

One of the most important aspects of programming and workflows is error checking, which is why SharePoint's lack of transparent support for "checks" can be so damn frustrating. Being able to compare a known value to a field in a list item is great. But if that check fails, the workflow will error out.

An Error Occurred...

Try it yourself by attempting to update an item with an invalid ID (or some other comparison that doesn't exist).

SharePoint will not allow you to directly check for the desired value using an if/else branch - and neither will it provide any more information beyond informing you that "An Error occurred".

So What's The Solution?

Aside from using a third party product which will perform such simple comparisons, or building a custom workflow action (ugh!), there is only one sure-fire method I have found.

When you try to test for a specific field value that doesn't exist, you will receive an error. However, if you test for the existence of a specific field value within a compound string, the comparison does not produce an error and the workflow continues on its merry way. This same trick will also work when checking for the existence of a List.

To achieve this:

  1. Create a new Local Variable called, say, TargetItemCheck.
  2. Add a Set Workflow Variable action.
  3. Set the value for the variable by clicking the ellipsis (…) to open the string builder window.
  4. Add some meaningful text such as "Target Item Lookup: " and then add a lookup to the field in the target item.
  5. You can now perform a safe check for the item by adding an If any value equals any value condition and checking if our variable contains the value we want to check for.

Voila! Another workaround found for a common problem.

No comments: