Skip to main content

Notifications

Dynamics 365 general forum
Suggested answer

C# - How to get field value from EntityCollection

Posted on by 725

Hi all,

I am trying to create a custom workflow that generates a list of resource account id and e-mail address (custom field added to bookableresource entity),  then send out custom html e-mail using this list.  This is for Dynamic 365 V8.2 cloud version.  I created an EntityCollection based on fetchxml but I get error message in visual studio when I tried to get field value in each row from the EntityCollection, I read through below MSDN tutorial:

msdn.microsoft.com/.../gg328149.aspx

and tried below so far:

string accountid = bookedResource.Attribute["accountid"];  (Microsoft.xrm.sdk.Entity does not contain definition for attribute...)

string accountid = bookedResource["accountid"];  (cannot implicit convert object to string...)

Below is my code, can anyone tell me how to do this?

            //retrieve resource account ID and e-mail that has open work order and has e-mail data stored under bookableresource entity
             const string fetchVendor = @"
            <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' >
              <entity name='bookableresource' >
                <attribute name='accountid' />
                <attribute name='bookableresourceid' />
                <attribute name='jt_bookingemail' />
                <filter>
                  <condition attribute='jt_bookingemail' operator='not-null' />
                </filter>
                <link-entity name='bookableresourcebooking' from='resource' to='bookableresourceid' >
                  <link-entity name='msdyn_workorder' from='msdyn_workorderid' to='msdyn_workorder' >
                    <filter>
                      <condition attribute='msdyn_substatusname' operator='eq' value='OPEN' />
                    </filter>
                  </link-entity>
                </link-entity>
              </entity>
            </fetch>
            ";

             EntityCollection vendorCollection = service.RetrieveMultiple(new FetchExpression(fetchVendor)); 
            
            foreach (Entity bookedResource in vendorCollection.Entities)
             {
                string accountid = bookedResource["accountid"];  
                string jt_bookingmail = bookedResource.GetAttributeValue['jt_bookingmail'];
                //pass account id and e-mail string into method that create and send out e-mails
             };


  • Suggested answer
    Asmaa Bouhmidi Profile Picture
    Asmaa Bouhmidi 135 on at
    RE: C# - How to get field value from EntityCollection

    Hello,

    Try using this one

    Guid accountid = bookedResource.GetAttributeValue<EntityReference>("accountid").Id;

    string jt_bookingmail =bookedResource.GetAttributeValue<string>("jt_bookingmail");

    if this work mark my answer as verified

  • Sasuke2690 Profile Picture
    Sasuke2690 5 on at
    RE: C# - How to get field value from EntityCollection

    A pretty good share on How to get field value from EntityCollection. Thanks for the useful knowledge for beginners like me Spider Solitaire 2 Suit

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,107 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,954 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans