Search code examples
dynamics-crmfetchxml

If statement to get different attributes - fetchXML


Using SQL as example, is it possible to have a "if select" statement in FetchXML to get different attributes depending on a condition?

For example, if @value=1 , fetch attributes "a" and "b", else, fetch attributes "c" and "d".

I am new to this.


Solution

  • Whatever it is, SQL or fetchXML in js/C# or any other technology/software, the only way to achieve this requirement is - Dynamic query technique. Compose the query in string the way we want & execute the string query in runtime.

    For example:

    string query =  @"
       <fetch mapping='logical'>
         <entity name='contact'>";
    
    if (value == 1)
    {
            query+= "<attribute name='title'/> 
                     <attribute name='fullname'/>";
    }
    else
    {
            query+= "<attribute name='firstname'/> 
                     <attribute name='lastname'/>";
    }
    
         query+= "</entity> 
                </fetch>";