locked
CRM 2011 : Plugin to create a self reference RRS feed

  • Question

  • Hi, i am completely new to Crm -


    I want to create a self reference in my entity.

    i created a one-to-many relationship look up field on the form names it as 'Parent'.
    Now on update i want to set this field value with the current reference.

    Below is my code with fields that i want to copy -

                var pluginExecutionContext = localContext.PluginExecutionContext;

                IOrganizationService service = localContext.OrganizationService;

                abc= pluginExecutionContext.InputParameters["Target"] as Entity;

                if (pluginExecutionContext.Depth == 1)
                {
                    Guid abcId = abc.Id;
    Entity abcCopy = new Entity("mcg_abc");

    if (abc.Attributes.Contains("mcg_effectivedate"))
                    {
                        abcCopy.Attributes["mcg_effectivedate"] = isp.GetAttributeValue<DateTime>("mcg_effectivedate");
                    }

                    if (abc.Attributes.Contains("mcg_startdate"))
                    {
                        abcCopy.Attributes["mcg_startdate"] = isp.GetAttributeValue<DateTime>("mcg_startdate");
                    }

                    if (abc.Attributes.Contains("mcg_enddate"))
                    {
                        abcCopy.Attributes["mcg_enddate"] = isp.GetAttributeValue<DateTime>("mcg_enddate");
                    }

                    if (abc.Attributes.Contains("mcg_amendeddate"))
                    {
                        abcCopy.Attributes["mcg_amendeddate"] = isp.GetAttributeValue<DateTime>("mcg_amendeddate");
                    }

    abcCopy.Attributes["mcg_parentabc"] = new EntityReference("mcg_abc", abc.Id);
                     service.Create(abcCopy);
    }

    Is the code correct to set the reference??


    Friday, January 31, 2014 7:17 AM

All replies

  • Hi

    I think you need to change entity name in your entity reference

    abcCopy.Attributes["mcg_parentabc"] = new EntityReference("ParentEntityName", abc.Id);

    Hope this helps..

    Friday, January 31, 2014 7:30 AM
  • Hi,

    I think this looks reasonably good. Do you get any errors?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Friday, January 31, 2014 8:28 AM
  • Hi i get the below error -

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): ABCCopyFeature.ABCCopyFeaturePlugin: System.InvalidCastException: Specified cast is not valid.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220956</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>Unexpected exception from plug-in (Execute): ABCCopyFeature.ABCCopyFeaturePlugin: System.InvalidCastException: Specified cast is not valid.</Message>
      <Timestamp>2014-02-01T06:48:13.2854Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>

    [ABCCopyFeature: ABCCopyFeature.ABCCopyFeaturePlugin]
    [103d67ed-0b8b-e311-900b-067f90a72129: ABCCopyFeature.ABCCopyFeaturePlugin: Update of mcg_abc]

    Entered ABCCopyFeature.ABCCopyFeaturePlugin.Execute(), Correlation Id: 99cdf0d1-6b99-4ca2-8e99-7113085c273e, Initiating User: 001d87cd-072a-e311-90ab-067f90a72129

    ABCCopyFeature.ABCCopyFeaturePlugin is firing for Entity: mcg_abc, Message: Update, Correlation Id: 99cdf0d1-6b99-4ca2-8e99-7113085c273e, Initiating User: 001d87cd-072a-e311-90ab-067f90a72129
    Exiting ABCCopyFeature.ABCCopyFeaturePlugin.Execute(), Correlation Id: 99cdf0d1-6b99-4ca2-8e99-7113085c273e, Initiating User: 001d87cd-072a-e311-90ab-067f90a72129


    </TraceText>
    </OrganizationServiceFault>


    Saturday, February 1, 2014 6:52 AM
  • Hi Shradha,

        The error is following:

     System.InvalidCastException: Specified cast is not valid

        This happens when the datatype of one of the field is not valid. I can see that you are using the late binding in the code. To avoid any simple errors / typos etc. it is preferred to use early binding. Here is how to create this.

    http://msdn.microsoft.com/en-us/library/gg328499.aspx

    http://msdn.microsoft.com/en-us/library/gg334754.aspx

        Here is how to generate early bound classes.

    http://msdn.microsoft.com/en-us/library/gg327844.aspx


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Saturday, February 1, 2014 7:03 AM
  • Hi Shradha,

    Try to single out which line is causing this error, perhaps by just using one of them and adding the others as that one starts to work. I'm personally using late binding in all my code since it's more dynamic and at least has had some performance advantages.

    Good Luck


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Monday, February 3, 2014 10:27 AM