please anybody know how to fetch data from entity in CRM using C# plugins.
your quick help would be highly appreciated.
please anybody know how to fetch data from entity in CRM using C# plugins.
your quick help would be highly appreciated.
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!"); }
André Arnaud de Cal...
292,516
Super User 2025 Season 1
Martin Dráb
231,432
Most Valuable Professional
nmaenpaa
101,156