12
Update Parent Entity Records when Related Entities Created or Updated Under Solution add custom entities, create forms for each entity and create a relationship between custom entities. In my sample I took Contact Details, Location and Parts. I made contact Details as primary entity for Parts. Here is Contact Details Custom Entity form and I add Parts entity in sub grid of Contact Details form. 1 | Page

bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

Under Solution add custom entities, create forms for each entity and create a relationship between custom entities. In my sample I took Contact Details, Location and Parts. I made contact Details as primary entity for Parts.

Here is Contact Details Custom Entity form and I add Parts entity in sub grid of Contact Details form.

1 | P a g e

Page 2: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

Here is Parts Entity Form which consists of fields and Look up Field for Relationship with contact Details entity.

After saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below

2 | P a g e

Page 3: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

Now our task is when we are updating or creating parts entity records the resultant values has to be updated in Total Units, Total Price and Total Prices of all Parts which are Contact Details Entity Fields.

When we are clicking on sub grid existing records or “+”, it is navigated to Parts Entity Form. There we need to create or update present record and we have to select parent record from contact entity in Look up Field. Then click on save button.

After saving the record, Total field in above pic will be updated with multiplied values of price and unit. And same result will be effected in parent entity field – Total Price.

And Total units available in each records in Parts entity values will be aggregated and displayed in Total Units field in Contact Details Entity.

And Total Prices available in each records in Parts entity values will be aggregated and displayed in Total Prices of All Parts field in Contact Details Entity.

Below two pictures shows clear view of task.

3 | P a g e

Page 4: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

4 | P a g e

Page 5: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

PLUGIN CODE:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Crm.Sdk;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Messages;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Description;using Microsoft.Xrm.Sdk.Query;

namespace CalcPrice.parts{    public class evaluate : IPlugin    {        private int unit;        private int price;        private int totalprice;        private string unitfieldname = "card_unit";        private string pricefieldname = "card_price";        private string PreImage = "Image";

        IPluginExecutionContext context;        public void Execute(IServiceProvider serviceProvider)        {            context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));            IOrganizationService service = factory.CreateOrganizationService(context.UserId);            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));            Entity targetEntityImage = null;            if (context.PrimaryEntityName == "card_parts" && (context.MessageName == "Create" || context.MessageName == "Update"))            {                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)                {                    try                    {                        targetEntityImage = (Entity)context.InputParameters["Target"];                        Guid entityId = targetEntityImage.Id;

                        unit = getunit(targetEntityImage);                        price = getprice(targetEntityImage);                        totalprice = unit * price;                        targetEntityImage.Attributes["card_total"] = totalprice;                       // service.Update(targetEntityImage);

                        

                        EntityReference contactdetails = targetEntityImage.GetAttributeValue<EntityReference>("card_contactentityid");                        Entity contDet = new Entity("card_contactdetails");                        contDet.Id = contactdetails.Id;                        contDet["card_totalprice"] = totalprice;                        contDet["card_totalunits"] = getTotalUnits(entityId, service);                        contDet["card_totalpricesofallparts"] = getTotalParts(entityId, service);                        service.Update(contDet);

5 | P a g e

Page 6: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

                                                                                            }

                    catch (Exception ex)                    {                        throw new InvalidPluginExecutionException(ex.ToString());                    }                }

            }        }        private int getunit(Entity partvalue)        {            int unit1 = 0;            if (partvalue.Attributes.Contains(unitfieldname))            {                unit1 = (int)partvalue.Attributes[unitfieldname];            }            else            {                unit1 = getfieldvalue(unitfieldname, PreImage);            }            return unit1;        }        private int getprice(Entity partvalue)        {            int price1 = 0;            if (partvalue.Attributes.Contains(pricefieldname))            {                price1 = (int)partvalue.Attributes[pricefieldname];            }            else            {                price1 = getfieldvalue(pricefieldname, PreImage);            }            return price1;        }        public int getfieldvalue(string fieldname, string PreImagename)        {            int targetvalue = 0;            Entity atmcontact = (Entity)context.InputParameters["Target"];            if (atmcontact != null)            {                if (context.PreEntityImages[PreImagename] is Entity)                {                    Entity preimage = context.PreEntityImages[PreImagename] as Entity;                    targetvalue = (int)preimage.Attributes[fieldname];

                }

            }            return targetvalue;        }        protected int getfieldValue(string fieldname)        {            int targetValue = getfieldvalue(fieldname, "");

            return targetValue;        }                public int getTotalUnits(Guid entityId, IOrganizationService service)        {

6 | P a g e

Page 7: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

            QueryExpression query = new QueryExpression();            query.EntityName = "card_parts";            query.ColumnSet = new ColumnSet("card_unit");            query.Criteria.AddCondition("card_partname", ConditionOperator.NotEqual,1);

            EntityCollection results = service.RetrieveMultiple(query);                        int tc = 0;            foreach (Entity item in results.Entities)            {                

                if(item.Attributes.Contains("card_unit"))                {

                   // int unitcount =(int) (item.Attributes["card_unit"]) ;                    int cnt = item.GetAttributeValue<int>("card_unit");                    tc = tc + cnt;                    //item.Attributes["card_totalunits"] = tu + cnt ;

                    //service.Update(item);                }            }            return tc;                    }

        public int getTotalParts(Guid entityId, IOrganizationService service)        {            QueryExpression query = new QueryExpression();            query.EntityName = "card_parts";            query.ColumnSet = new ColumnSet("card_price");            query.Criteria.AddCondition("card_partname", ConditionOperator.NotEqual, 1);

            EntityCollection results = service.RetrieveMultiple(query);

            int tp = 0;            foreach (Entity item in results.Entities)            {

                if (item.Attributes.Contains("card_price"))                {

                    // int unitcount =(int) (item.Attributes["card_unit"]) ;                    int cntp = item.GetAttributeValue<int>("card_price");                    tp = tp + cntp;                    //item.Attributes["card_totalunits"] = tu + cnt ;

                    //service.Update(item);                }            }            return tp;

        }    }}Attachments area

7 | P a g e

Page 8: bhoopathigoudk.files.wordpress.com · Web viewAfter saving the contact details record Sub grid will be visible. Along with present records of Parts Entity. Shown below Now our task

Update Parent Entity Records when Related Entities Created or Updated

Plugin Registration:

8 | P a g e