locked
How to update the fields of Parent entity using Plugin in MSCRM 2013.(Parent fields to be updated are not associated in the child entity. RRS feed

  • Question

  • Hi All,

                I am new to CRM customization. I need to update the Parent entity fields (attributes) which are not associated with the child entity.

    I have a Parent entity named Products ( Fields: productname,price,total,productsavialable)

    Child entity : Contact . In Contact entity  productname is lookup field of productname which is of the Products entity  named productselected.( N-1 Relationship is created by default on Contacts Entity)

    My requirement is as follows. When the Contact entity is updated or new  Contact is created after selecting the productselected lookup field added to the Form  it should decrease the productsavaialbe by 1  and increase the total  fields by 1  in the Products entity.

    But these two fields are not mapped or visible in the Contacts Form. I have read that we can achieve this by using Query Expression  by getting  the fields of the Product entity by using the productsselected guid on the Contacts Form.

    I have found that we need to use PostEntityImages instead of Target in Plugins but I dont have any idea on this concepts.I didn't attach any image to the Products entity as well.

    Can anyone please help me in this requirement as I have spent much time on javascript resources with no results. I need to complete this requirement using Plugins only.

    Any help is greatly appreciated.

    Thanks & Regards,

    Fayaz.


    FayazSyed

    Tuesday, March 3, 2015 2:11 PM

All replies

  • Hi Everyone,

                I need to complete this requirement as soon as possible. Can anyone please help me .


    FayazSyed

    Wednesday, March 4, 2015 6:02 AM
  • Hi, i didn't understand, what exactly do you want to do, so i place some code and describe it:

    IOrganizationService service = localContext.OrganizationService;
    
    Entity CurEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"]; //this will contain a current entity, which is called a plugin
    if (CurEntity.Contains("my_contactId")) //this field condains accountID
    {
        guid accountID = CurEntity.GetAttributeValue<guid>("my_contactId");
    
        Entity CurAccount = service.Retrieve("account", accountID, new ColumnSet(new string[] { "fullname", "name", "my_field1"}));//This will contain an accont entity with fields fullname, name and my_field1
        ...//Make some work here
        service.Update(CurAccount);//This will update CurAccount entity. 
    
    }

    This code look a "my_contactId" field, which is present in my updated entity, retrives some fields of account with id "my_contactId" and update it.

    In order to write a normal working code of plugin, you should place some additional verifications before you can use "accountID" vafiable: check, if accountID will be null or not, and exists accountID or not.

    If you want to read more data of your current entity, you can use "service.Retrieve" as well:

    Entity CurEntityFromDB = service.Retrieve(CurEntity.LogicalName, CurEntity.Id, new ColumnSet(new string[] { "my_fild1", "my_field2", "my_field3"}));//This will contain your current entity with needed fields

    • Edited by xjomanx Wednesday, March 4, 2015 2:52 PM
    Wednesday, March 4, 2015 2:38 PM
  • Hi Xjomanx,

          Thanks a lot for the reply.

    My requirement is that I need to update the Parent entity records which are not available on the child entity.

    I have 2 entities  Parent: Products (custom entity) and Child Entity : Contact.

    There is N:1 relation from Contacts to Products.

    ProductName is the lookup field in the Contacts Form which is the primary field  of Products entity.

    Now when I select the ProductName from the lookup in the Contacts Form .. we need to update few fields of parent entity Product which are not visible on the Contacts Form for Ex: TotalQty field needs to be incremented by 1 each time a new Product is assigned to Contact.

    How can I get the field TotalQty of Parent entity from the child entity i.e Contacts and update it by using Plugin.

    I need to fetch the  various other fields of the Parent entity by using Plugin invoked from Child entity i.e from Contact entity. If you need more clarification please get back to me.

     


    FayazSyed

    Wednesday, March 4, 2015 4:13 PM
  • Hi, as a can understand, you have this situation:

    So, when you a choosing "my_ProductId" in a contact, plugin should update "my_field1" in product.

    It it write, my code above (with small correction) is doing what you need:

    IOrganizationService service = localContext.OrganizationService;
    
    Entity CurEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"]; //this will contain a current contact
    if (CurEntity.Contains("my_productId")) //if field "my_productId" has been changed
    {
    		guid ProductID = CurEntity.GetAttributeValue<guid>("my_productId");
    		if (ProductID != null)//If my_productId changed on a not null value
    		{
    			Entity CurProduct = service.Retrieve("product", ProductID, new ColumnSet(new string[] {"my_field1", "my_field1"}));//This will contain an product entity with fields my_field1 and my_field1
    			int count = CurProduct.GetAttributeValue<int>("my_field1");
    			if (count != null)
    			{
    				count++;
    			}
    			else
    			{
    				System.Collections.Generic.KeyValuePair<string, object> AddAtr = new System.Collections.Generic.KeyValuePair<string, object>("my_field1", 1);
    				CurProduct.Attributes.AddRange(AddAtr);
    			}
    		}
    		else //If my_productId changed on a null value
    		{
    			...//some logic is here
    		}
    		service.Update(CurProduct);//This will update CurProduct entity.}
    }


    • Edited by xjomanx Thursday, March 5, 2015 6:50 AM
    Thursday, March 5, 2015 6:48 AM