Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer Service forum

Portal Web template - Fetch XML add dynamic conditions

(0) ShareShare
ReportReport
Posted on by 425

Hi,

I have a web template with Fetchxml to retrieve records from Dynamics, i want to add dynamic conditions based on the parameters passed to that. Below is that code, i tried to add, but it is not working as expected.

Any thoughts please?

{% assign customerid = request.params['customerid'] %}
{% assign addressid = request.params['addressid'] %}
{% assign cacheString = request.params['cacheString'] %}
{% if customerid %}
{% assign conditionCustomer = " %}
    { { conditionCustomer } }
    {% endif %}
    {% if addressid %}
    {% assign conditionAddress = " %}
        { { conditionAddress } }
        {% endif %}
        {% fetchxml customerProperties_query %}
        
            
                
                
                
                
                
                
                    {{ conditionAddress }}
                    {{ conditionCustomer }}
                    
                    
                
            
        
        {% endfetchxml %}
        {
            "totalcount": { { customerProperties_query.results.total_record_count } },
            "customerProperties" :
            [
                {% for result in customerProperties_query.results.entities %}
        {
            "lace_customerpropertiesid" : "{{ result.lace_customerpropertiesid }}",
                "lace_address" : "{{ result.lace_name }}"
        }
        {% unless forloop.last %}, {% endunless %}
        {% endfor %}
  ]
    }

Categories:
  • rules Profile Picture
    rules 51 on at
    RE: Portal Web template - Fetch XML add dynamic conditions

    Btw one very important thing, when u are reading request.params variables like this, always ensures that the data is html encoded (use liquid escape tag to encode it). If u don't do that, u will have a XSS vulnerability as anyone can pass in script as a parameter and u will execute it

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Portal Web template - Fetch XML add dynamic conditions

    What do you mean by it's not working as expected -are you not seeing the expected records, or are you seeing an exception?

    some things that might help - 

    I suggest you initialize the variables conditionCustomer and conditionAddress to empty strings.  the Liquid processor behaves oddly with uninitialized variables. 

    assign does not look correct. i would use a capture instead. 

    and the evaluation of the lace_customerpropertiesid - assuming that is a lookup, i think you need to evaluate it differently.

    something like this might work... 


    {% assign customerid = request.params['customerid'] %}
    {% assign addressid = request.params['addressid'] %}
    {% assign cacheString = request.params['cacheString'] %}

    {% assign conditionCustomer = "" %}
    {% assign conditionAddress = "" %}

    {% if customerid %}
        {%- capture conditionCustomer -%} 
            <condition attribute="lace_customer" operator="eq" value="{{customerid}}" />
        {%- endcapture -%}
    {% endif %}
    {% if addressid %}
        {%- capture conditionAddress -%} 
            <condition attribute="lace_address" operator="eq" value="{{addressid}}" /> 
        {%- endcapture -%}
    {% endif %}
    {% fetchxml customerProperties_query %}
            <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" returntotalrecordcount="true">
                <entity name="lace_customerproperties">
                    <attribute name="lace_customerpropertiesid" />
                    <attribute name="lace_name" />
                    <attribute name="createdon" />
                    <attribute name="lace_address" />
                    <order attribute="lace_name" descending="false" />
                    <filter type="and">
                        {{ conditionAddress }}
                        {{ conditionCustomer }}
                        <condition attribute="lace_name" operator="ne" value="{{ cacheString }}" />
                        <condition attribute="statecode" operator="eq" value="0" />
                    </filter>
                </entity>
            </fetch>
    {% endfetchxml %}
    {
        "totalcount": {{ customerProperties_query.results.total_record_count }},
        "customerProperties" :
        [{%- for result in customerProperties_query.results.entities -%}{
        "lace_customerpropertiesid" : "{{ result['lace_customerpropertiesid'].id }}",
            "lace_address" : "{{ result.lace_name }}"
    }
    {% unless forloop.last %}, {% endunless %}
    {% endfor %}]
    }

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans