<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>unable to use if else in Expression rule</title><link>https://community.appian.com/discussions/f/general/20089/unable-to-use-if-else-in-expression-rule</link><description>this works fine if local!reassigndetail finds some data from DB using a!queryentity but when it doesn&amp;#39;t return any data from db then it gives following error 
 
 Expression evaluation error at function a!forEach [line 47]: Invalid index: Cannot index</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78584?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 15:39:45 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:7783b1bd-e8fc-41aa-acbb-fa97cb1160a0</guid><dc:creator>Mike Schmitt</dc:creator><description>[quote userid="26843" url="~/discussions/f/general/20089/unable-to-use-if-else-in-expression-rule/78583#78583"]What does flatten() do to a list of dictionary?&amp;nbsp;[/quote]
&lt;p&gt;AFAIK it just makes sure it&amp;#39;s a flatt array of dictionary, instead of a list of nested lists.&amp;nbsp; I believe it&amp;#39;s being used redundantly (or at least, unnecessarily) in the above code sample.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78583?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 15:35:37 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:c9c8e279-2255-4c1b-9df7-61d5d6b66ce8</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;Flatten takes : {{1,2,3},{4},{},{5,6,7,8},{{9},{10,11}},12} and outputs {1,2,3,4,5,6,7,8,9,10,11,12}.&amp;nbsp; It makes a nested array of arrays into just an array.&amp;nbsp; Do you even need that operation to be performed if you have a list of dictionary?&lt;/p&gt;
&lt;p&gt;What does flatten() do to a list of dictionary?&amp;nbsp; Would it similarly squash the internal structure so you couldn&amp;#39;t see the individual fields anymore?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78506?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 16:54:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:b703748a-ed59-4bf5-8ea9-0e0f53a2b4b9</guid><dc:creator>arshbirs0001</dc:creator><description>&lt;p&gt;List of &lt;span&gt;dictionary&amp;nbsp;&lt;/span&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78505?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 16:53:04 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:61691f5e-9e96-4441-9d5f-db844379cb8f</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;List of what / what is the structure of the list returned?&amp;nbsp; Because your subsequent code assumes that &lt;em&gt;local!fabDetails&lt;/em&gt; is a dictionary containing a property named &lt;em&gt;fabName&lt;/em&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78504?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 16:48:25 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:593fc3f1-90ab-4205-839d-3514a95ac79f</guid><dc:creator>arshbirs0001</dc:creator><description>&lt;p&gt;it returns list&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78501?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:25:27 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3e2b3da7-61df-4eee-a6a1-4b8d07227d91</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;Also, please for your own sake always explicitly NAME items: and expression: parameters of a!forEach.&amp;nbsp; That will save your own head quite a lot of aching.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78500?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:23:39 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:c1e1d13b-e484-4e1b-9175-72c767e2f55b</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;I see what you&amp;#39;re doing.&lt;/p&gt;
&lt;p&gt;You&amp;#39;re doing a!forEach(&lt;/p&gt;
&lt;p&gt;items: a!flatten(list).property,&lt;/p&gt;
&lt;p&gt;expression: fv!item&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;There&amp;#39;s absolutely 0 point to using a!forEach.&amp;nbsp; The flatten function already takes a list.&amp;nbsp; It flattens a nested list into a single list, destroying the more complex structure.&amp;nbsp; The expression only returns the items.&amp;nbsp; Well, if a!flatten(list).property worked, you&amp;#39;d already have a list of results without even using a!forEach.&lt;/p&gt;
&lt;p&gt;My advice, make a separate rule that only goes up to that point then returns nothing but a!flatten(your list) and see what that does.&amp;nbsp; Then, see if you can actually use the dot notation to get the property you want out of it.&amp;nbsp; Then, see if you need a!forEach.&amp;nbsp; I don&amp;#39;t think you will.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78499?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:23:24 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0b6474c3-77ac-4da6-9503-8bd2506002be</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;Also what is the output of &lt;em&gt;rule!MPPER_getFabsnamefromsamaccid()&lt;/em&gt;?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78498?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:21:39 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:091ee02b-be5f-49c3-8dc9-4fced34189d4</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;What is the purpose behind calling &lt;em&gt;&lt;strong&gt;a!flatten&lt;/strong&gt;&lt;/em&gt; here?&lt;br /&gt;&lt;img src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/11/pastedimage1608045748676v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78497?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:21:16 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:d82fc6b5-b7de-420e-85d0-05de31215818</guid><dc:creator>arshbirs0001</dc:creator><description>&lt;p&gt;done&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78495?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:19:31 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:4bd0d161-17d0-4b72-960d-c588e4da8dbb</guid><dc:creator>arshbirs0001</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  
  local!reassigndetail: a!queryEntity(
    entity: cons!MPPCONSDSE_MPPTABSAM_task,
    query: a!query(
      selection: a!querySelection(
        columns: {
          a!queryColumn(
            field: &amp;quot;ReassignEmail&amp;quot;
          ),
          a!queryColumn(
            field: &amp;quot;samaccid&amp;quot;
          )
        }
      ),
      logicalExpression: a!queryLogicalExpression(
        operator: &amp;quot;AND&amp;quot;,
        filters: {
          a!queryFilter(
            field: &amp;quot;Reassign&amp;quot;,
            operator: &amp;quot;=&amp;quot;,
            value: &amp;quot;Yes&amp;quot;
          ),
          a!queryFilter(
            field: &amp;quot;annoucncementid&amp;quot;,
            operator: &amp;quot;=&amp;quot;,
            value: ri!Announcementid
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: -1
      )
    ),
    fetchTotalCount: false
  ).data,
  local!result:null,
  local!fabdetails,local!fabdetails2,local!samdata,local!samemails,
  if(length(local!reassigndetail)==0,
    local!result,
    
    {
      local!fabdetails:
      a!forEach(
        items:local!reassigndetail,
        expression:rule!MPPER_getFabsnamefromsamaccid(fv!item.samaccid)

      ),
      local!fabdetails2: a!forEach(a!flatten( local!fabdetails).fabname,fv!item),
      local!samemails: a!forEach(a!flatten( local!reassigndetail).ReassignEmail,fv!item)
      ,


      local!samdata : a!queryEntity(
        entity: cons!MPPCONSCDT_SAMAccountability,
        query: a!query(
          logicalExpression: a!queryLogicalExpression(
            operator: &amp;quot;AND&amp;quot;,
            filters: {
              a!queryFilter(
                field: &amp;quot;samemail&amp;quot;,
                operator: &amp;quot;in&amp;quot;,
                value:local!samemails
              ),
              a!queryFilter(
                field: &amp;quot;fabname&amp;quot;,
                operator: &amp;quot;in&amp;quot;,
                value:local!fabdetails2
              )
            },
            ignoreFiltersWithEmptyValues: true
          ),
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 50
          )
        ),
        fetchTotalCount: false
      ).data,

      local!result:
      a!forEach(
        items:local!samdata,
        expression: cast(typeof(&amp;#39;type!{urn:com:appian:types}MPPSAMAccoumtability&amp;#39;()),fv!item)
      ),
      local!result
      
      
    }
    
  
  )
 

  
  
)
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;When I use if else,&lt;/p&gt;
&lt;p&gt;when 0 rows are return it shows &amp;#39;null&amp;#39;, when more are returned then below error:&lt;/p&gt;
&lt;p&gt;&lt;img src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/11/pastedimage1608045617450v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78494?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:17:01 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:529adf8d-d2ce-425a-bacb-35ef7592342b</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;That&amp;#39;s better - though it might be good to copy the code out of Appian again since the code you posted still doesn&amp;#39;t have indentation (making it still pretty hard to read).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78492?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:13:33 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:ecc7e596-ee0e-410f-85bc-8d96bd302a59</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;When the database returns 0 rows, it takes the form of &amp;quot;{}&amp;quot;, which has the type &amp;quot;List of Variant&amp;quot;.&amp;nbsp; You need to return at least one row with data in it before Appian can be aware of what the fields are even supposed to be.&amp;nbsp; You don&amp;#39;t even get the structure without returning at least a row of data.&lt;/p&gt;
&lt;p&gt;So, the solution is to wrap your logic inside an if function, which contains three parameters:&lt;/p&gt;
&lt;p&gt;The first parameter uses APN_isEmpty() or length() = 0 to determine if you returned 0 rows,&lt;/p&gt;
&lt;p&gt;The second parameter returns whatever you want to return in the case of 0 rows of data being returned by the query, could be as simple as &amp;quot;{}&amp;quot;,&lt;/p&gt;
&lt;p&gt;The third parameter is what you were doing before, which only works when you have data to work on.&lt;/p&gt;
&lt;p&gt;Over time, you&amp;#39;ll learn to wrap practically everything you write in &amp;quot;if is empty&amp;quot; or &amp;quot;if is blank&amp;quot;.&amp;nbsp; A great many of us call it &amp;quot;null safety.&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78491?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:13:19 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:ef916136-1f3d-447d-81c6-d66f482e763f</guid><dc:creator>arshbirs0001</dc:creator><description>&lt;p&gt;Done&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: unable to use if else in Expression rule</title><link>https://community.appian.com/thread/78490?ContentTypeID=1</link><pubDate>Tue, 15 Dec 2020 15:11:40 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:86cce068-ce91-4d96-8d9e-0a4ac2f30730</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;Your code is unnecessarily difficult to read when pasted straight into your post as plaintext as all formatting and indentation is lost.&amp;nbsp; I would encourage you to edit your post and insert it instead into a Code Box (&amp;quot;Insert&amp;quot; --&amp;gt; &amp;quot;Insert Code&amp;quot; in the editor menu) so it&amp;#39;s actually manageable.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>