web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi there,

    If you simply need some value from the first record - you can simply do something like below you dont need to foreach in the retrieved collection. just pick the first record from collection by doing [0].

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

                       if (collection.Entities.Count > 0)

                                   {

                                string name = "";

                                       if (collection[0].Contains("name"))

                                            name  = collection[0]["name"].ToString();

    }

  • naresh babu Profile Picture
    425 on at

    Hi Rawish Kumar,

    Thanks for you reply. I tried this also. But my requirement is not for single record. I need all subjects column as shown in image.

  • Suggested answer
    crm development Profile Picture
    870 on at

    Hi, you can create list and collect Subject column data in List. Or you can create Dictionary to collect Subject and related record id.

    Refer below code for List. Hope this helps.

    ---------------------------------------

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

    foreach (var c in result.Entities)

    {

    SubjectList.Add(c.Attributes["subject"].ToString());

    }

    --------------------------------

    Thanks,

    Anand

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Okay understood. Then its fairly simple.

    Create an array list and add value to this within your foreach loop.

    You can then access the values from the list and use them.

  • Flydancer Profile Picture
    1,332 on at

    Hi N B,

    your code looks like it does what you want. Can you explain what exactly your problem is?

  • naresh babu Profile Picture
    425 on at

    Hi Flydancer

    Actually, i need the subject column as my output as shown in image.

  • naresh babu Profile Picture
    425 on at

    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.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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
    crm development Profile Picture
    870 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans