Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Field Service forum / how to fetch data from...
Field Service forum

how to fetch data from table in CRM C# plugins

(0) ShareShare
ReportReport
Posted on by 10,274

please anybody know how to fetch data from entity in CRM using C# plugins.

your quick help would be highly appreciated.

Categories:
  • Suggested answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: how to fetch data from table in CRM C# plugins

    Hi Codehunter,

    Below is an example of how to run a fetch the data and set the values on a form

    private void GetAgreementInvoiceDateDetails(ITracingService tracer, IOrganizationService service, IPluginExecutionContext context)
            {
                //Current Entity ID
                Entity entity = (Entity)context.InputParameters["Target"];
                //Get the current Invoice GUID
                Guid InvoiceId = entity.Id;
                tracer.Trace("Invoice: " + InvoiceId);
    
                //Q-----------------------------------------------------------------------------------------------------------------Q
                //********* Query Fetch the related Agreement Invoice Date ********* 
                tracer.Trace("Query Executing...");
    
                string fetchxml = "<fetch version='1.0' top='1' distinct='true' >" +
                                "  <entity name='invoice' >" +
                                "    <attribute name='invoiceid' />" +
                                "    <filter>" +
                                "      <condition attribute='invoiceid' operator='eq' value='" + InvoiceId + "' />" +
                                "    </filter>" +
                                "    <link-entity name='invoicedetail' from='invoiceid' to='invoiceid' alias='as' >" +
                                "      <link-entity name='f1_agreementinvoiceproduct' from='f1_agreementinvoiceproductid' to='f1_agreementinvoiceproduct' alias='at' >" +
                                "        <link-entity name='f1_agreementinvoicesetup' from='f1_agreementinvoicesetupid' to='f1_agreementinvoicesetup' alias='au' >" +
                                "          <link-entity name='f1_agreementinvoicedate' from='f1_invoicesetup' to='f1_agreementinvoicesetupid' alias='ay' >" +
                                "            <attribute name='f1_invoicedate' />" +
                                "            <attribute name='f1_invoicesetup' />" +
                                "            <attribute name='f1_agreementinvoicedateid' />" +
                                "            <filter type='and' >" +
                                "               <condition attribute='f1_invoicestatus' operator='eq' value='690970001' />" +
                                "            </filter>" +
                                "            <order attribute='f1_invoicedate' descending='true' />" +
                                "          </link-entity>" +
                                "        </link-entity>" +
                                "      </link-entity>" +
                                "    </link-entity>" +
                                "  </entity>" +
                                "</fetch>";
    
                EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));
    
                tracer.Trace("There are {0} entities found", result.Entities.Count);
    
                foreach (var c in result.Entities)
                {
                    tracer.Trace("Query Retrieved Attributes:");
    
                    agreementinvoicedateid = (Guid)((AliasedValue)c["ay.f1_agreementinvoicedateid"]).Value;
                    tracer.Trace("- Query agreementinvoicedateid: " + agreementinvoicedateid);
    
                    agreementinvoicedate = (DateTime)((AliasedValue)c["ay.f1_invoicedate"]).Value;
                    tracer.Trace("- Query agreementinvoicedate: " + agreementinvoicedate);
    
                    object oVal = ((AliasedValue)c["ay.f1_invoicesetup"]).Value;
                    agreementinvoicesetupid = ((EntityReference)oVal).Id;
                    tracer.Trace("- Query agreementinvoicesetupid: " + agreementinvoicesetupid);
    
                }
    
                tracer.Trace("Updating Invoice...");
                //Update Agreement Invoice Date fields on the Invoice
                Entity updateInvoice = new Entity(entity.LogicalName, context.PrimaryEntityId);
                updateInvoice.Attributes[AgreementInvoiceDateLookup] = new EntityReference("f1_agreementinvoicedate", agreementinvoicedateid);
                updateInvoice.Attributes[AgreementInvoiceDate] = agreementinvoicedate;
                updateInvoice.Attributes[PSDAgreementInvoiceSetup] = new EntityReference("f1_agreementinvoicesetup", agreementinvoicesetupid);
                service.Update(updateInvoice);
    
                tracer.Trace("Invoice Updated!");
            }


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,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans