Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get the attributes columns data from Fetch XML through Custom workflow

(0) ShareShare
ReportReport
Posted on by 425

Hi Guys,

My requirement is to get attribute fields data from Fetch XML through custom workflow.
I have approach retrievemultiple with fetchxml query.
I got the count of the records through fetchxml and now i want to get the first column data of fetchxml through custom workflow.
 
In the below image, i want to get the "Subject" column data.

123Untitled.png

Below logic:

string query2 = @"<fetch version='1.0' mapping='logical' distinct='false' >
<entity name='task'>
<attribute name='subject' alias='task_count' />
<order attribute='subject' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
</entity>
</fetch>";

EntityCollection result = service.RetrieveMultiple(new FetchExpression(query2));

foreach (var c in result.Entities)
{
string aggregate = c.Attributes["subject"].ToString();
}
}

//string contactField2 = contact.GetAttributeValue<string>("firstname");- is working for only first record.

By this logic i got only first value. Can any suggest me the solution to get the data first column through fetchxml.
 

*This post is locked for comments

  • naresh babu Profile Picture
    naresh babu 425 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Hi Flydancer,

    My fetch XML query filter is "subjects" should be not null.

  • Suggested answer
    Flydancer Profile Picture
    Flydancer 1,332 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Hi N B,

    it looks like some of your subjects are empty. Try using c.GetAttributeValue<string>("subject") instead of accessing the Attributes Array.

    At the end you have to concatenate the array values to a single string and use it as the output parameter: var aggregate = String.Join(",", SubjectList);

    I hope this helps.

  • Verified answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    That's a different story.  You were getting one value out of the aggregated fetch xml . I .e "4". You can easily set it to the output parameter,

    but you cant set a series of numbers such as {9,8,9,7,3,3} because your workflow wouldnt know what to do with that.

    hope this makes sense.

  • naresh babu Profile Picture
    naresh babu 425 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    By using Collection and output Parameters . I got count of fetch XML in custom workflow.

  • Verified answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    In custom workflow output parameter you cannot set a List or collection of records.

    You can send just one subject and that's it.

    Other option is you send email within your code itself if you need to utilize all subjects and if its dynamic.

    here are supported out put parameters : docs.microsoft.com/.../gg327984(v=crm.8)  

  • naresh babu Profile Picture
    naresh babu 425 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Thanks Rawish. I am using input and output parameters to pass data into mail in workflow.

    Below is my code:

    string query2 = @"<fetch version='1.0' mapping='logical' distinct='false'  >

                                       <entity name='task'>

                                       <attribute name='subject'  />

                                        <order attribute='subject' descending='false' />  

                                           <filter type='and'>

                                            <condition attribute='statecode' operator='eq' value='0' />

                                            </filter>  

                                        </entity>

                                       </fetch>";

               EntityCollection result = service.RetrieveMultiple(new FetchExpression(query2));

               List<string> SubjectList = new List<string>();

               foreach (var c in result.Entities)

               {

                   //SubjectList.Add(c.Attributes["subject"].ToString());  //i tried this but error occurred.

                   string aggregate9c = c.Attributes["subject"].ToString(); // with this i am able to get only one record.

                   this.subject1.Set(context, aggregate9c);

               }

           }

           [Input("FullName")]

           [ReferenceTarget("systemuser")]

           public InArgument<EntityReference> systemuser { get; set; }

          [Output("subject")]

           public OutArgument<string> subject1 { get; set; }

       }

  • Suggested answer
    crm development Profile Picture
    crm development 870 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Yup. I agree with Rawish. Code mentioned should work as it is. Please check you have written correct attribute name and debug the code if possible.

    Thanks,

    Anand

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Anand's code is perfectly fine and i could see its working:
    0804.1.png

    0804.1.png

    var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='activitypointer'>
    <attribute name='activitytypecode' />
    <attribute name='subject' />
    <attribute name='statecode' />
    <attribute name='prioritycode' />
    <attribute name='modifiedon' />
    <attribute name='activityid' />
    <attribute name='instancetypecode' />
    <attribute name='community' />
    <order attribute='modifiedon' descending='false' />
    <filter type='and'>
    <condition attribute='subject' operator='not-null' />
    <condition attribute='activitytypecode' operator='eq' value='4212' />
    </filter>
    </entity>
    </fetch>";

    EntityCollection collection = crmService.RetrieveMultiple(new FetchExpression(fetch));

    List<string> SubjectList = new List<string>();
    foreach (var sub in collection.Entities)
    {
    SubjectList.Add(sub.Attributes["subject"].ToString());
    }
    SubjectList.ForEach(i => Console.Write("{0}\n", i));
    Console.ReadKey();



  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    This error generally occurs when you try to access a key ( field) which is not prresent in the collection.

    could you please debug your code and see on which line its coming.

  • naresh babu Profile Picture
    naresh babu 425 on at
    RE: Get the attributes columns data from Fetch XML through Custom workflow

    Hi Anand Wani, Thanks for your reply

    I have tried your script. But it was not working. error occurred like :-

    An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute):  -----deployed Solution Name---  : System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans