Forum Discussion

newbeetle's avatar
newbeetle
Copper Contributor
Oct 20, 2021

JSON If Statement

Hi I have an If Statement that conditionally formats the background based on a date as follows;

 

"attributes": {
"class": "=if(@currentField <= @now,'sp-field-severity--blocked', if(@currentField <= @now + 7776000000, 'sp-field-severity--warning', if(@currentfield >= @now + 78624000000, 'sp-field-severity--good','sp-field-severity--good')))"
},
 
I need to amend this to add an additional criteria based on a text value and tried adding if(@currentField == 'Failed', 'sp-field-severity--blocked' but its not working, any help on this appreciated.
  • newbeetle the simpler JSON is:

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "background-color": "=if([$Status] == 'Failed', '#7d0000', ((if(@currentField <= @now, 'red', if((@currentField >= @now && @currentField <= @now + 7776000000),'orange','green')))",
        "color": "white"
      }
    }

     

     

    Rob
    Los Gallardos
    Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

  • RobElliott's avatar
    RobElliott
    Silver Contributor

    newbeetle your logic is flawed:

     

    1) if(@currentField <= @now,'sp-field-severity--blocked', if(@currentField <= @now + 7776000000 - a date could be both of these so it wouldn't know how to apply the formatting consistently correctly.

     

    2) you can't have "Failed" in a date/time column. You would need to have the status in a different column and then use that in the JSON formula.

     

    I will post a solution shortly using more simple JSON than the class/attributes method.

     

    Rob
    Los Gallardos
    Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

    • newbeetle's avatar
      newbeetle
      Copper Contributor

      Hi Rob,

       

      I see what you mean, I'm very new to JSON but have applied your advice and have utilised an additional column and now have this working.

       

      Many thanks :smile:

      • RobElliott's avatar
        RobElliott
        Silver Contributor

        newbeetle the simpler JSON is:

        {
          "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
          "elmType": "div",
          "txtContent": "@currentField",
          "style": {
            "background-color": "=if([$Status] == 'Failed', '#7d0000', ((if(@currentField <= @now, 'red', if((@currentField >= @now && @currentField <= @now + 7776000000),'orange','green')))",
            "color": "white"
          }
        }

         

         

        Rob
        Los Gallardos
        Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

  • ChrisHois's avatar
    ChrisHois
    Copper Contributor

    SHAREPOINT 2016 on premise engine  DOES NOT support IF statements !!? :-(   bad SP ENgine.
    always SP online is the focus. Other version are only partially supported with full commands .

    whats a pitty :-)    ☹️

     

    {

      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",

      "elmType": "div",

      "txtContent": "=if(@currentField == 'http://example.com', 'Ja', 'Nein')"

    }

    This doesnt work. IF Statement never works. Code comes back to the SP column in the GUI.

Resources