0

I am using SharePoint online. I have a choice column and a text column, If the choice "phone" is selected from the choice column, I want the text column to change color. For some reason it wont work - both columns are in view, any help would be greatly appreciated!

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "color": "=if([$ContactMethod] == 'Phone', '#66ff33', '')"
  }
}
5
  • does the ContactMethod column allow multiple choices, or just a single choice? – willman Feb 24 '20 at 18:34
  • Multiple, phone, text, email. – B4N3 Feb 24 '20 at 18:46
  • No, I mean is the column configured to let the user make multiple choices, or limited to single choice -- i.e. can the user choose both "Phone" and "Email" in the same item? – willman Feb 24 '20 at 18:56
  • This may just be a typo in your comment, but I noticed in your JSON you are comparing against the string 'Phone', but in your comment, you listed 'phone'. The == equals comparison is case sensitive, so if your choice is in lower case, but you are comparing with an uppercase 'P', then it will never match. – willman Feb 24 '20 at 18:58
  • Just a typo in my comment - case sensitivity is correct in the code, and yes users make multiple choices. – B4N3 Feb 24 '20 at 19:46
1

If the source column allows multiple value selections, SharePoint stores those selections as an array of values inside the field. Therefore, matching on a single string value will never succeed against a field that allows multiple values, even if the user only chose one value, the value is still in an array. In the example you have posted above, as long as the column is configured to allow multiple values, even if a user only chose the "Phone" option, your IF check is trying to compare the string 'Phone' to a single item array containing the word 'Phone'.

So, instead of ==, you need to use the indexOf function. Your JSON should look something like this:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "color": "=if(indexOf([$ContactMethod], 'Phone') != -1, '#66ff33', '')"
  }
}
1
  • Thank you! That makes total sense. Much appreciated! – B4N3 Feb 24 '20 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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