locked
Plugin in CRM 2011 RRS feed

  • Question

  • Hi,

    my plugin work on create of a Account Record and have to retrieve the Account Record which is being created.

    below is my code..

                       

                    QueryExpression query = new QueryExpression("account");
                    ColumnSet column = new ColumnSet();
                    column.AllColumns = true;
                    query.ColumnSet = column;

                    try
                    {
                        Guid accId;
                        accId = new Guid();
                        accId = (Guid)((EntityReference)entity["accountid"]).Id;
                        Entity RecordAttributes = Service.Retrieve("account", accId, query.ColumnSet);//Retrieving the record from Account Entity
                    }
                    catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occured.", ex); }

    ....................................................................................................................

    the above code is Show error message on " accId = (Guid)((EntityReference)entity["accountid"]).Id; "

    i.e Unable to cast object of type 'System.Guid' to type 'Microsoft.Xrm.sdk.Entityreference'.

    Please help........


    CRMAbhi

    Tuesday, February 28, 2012 7:39 AM

Answers

  • The accountid attribute is the primary key of the account entity, and is returned as a Guid, not an EntityReference. So, you can use the following instead:

    accId = (Guid) entity["accountid"];

    Another option is to directly use the Id property of the entity:

    accId = entity.Id;


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Tuesday, February 28, 2012 8:55 AM
    Moderator
  • Just create a new entity record:

    var entity = new Entity("new_studentorwhatever");

    Then repeat for each attribute you need to copy. Remember to set correct Type:

    entity.Attributes.Add("new_attributename", RecordAttributes.GetAttribute<type>("attributename");

    When you're finished, create the student entity:

    Service.Create(entity);

    • Marked as answer by Crm.Abhi Tuesday, February 28, 2012 11:18 AM
    Tuesday, February 28, 2012 10:10 AM

All replies

  • The accountid attribute is the primary key of the account entity, and is returned as a Guid, not an EntityReference. So, you can use the following instead:

    accId = (Guid) entity["accountid"];

    Another option is to directly use the Id property of the entity:

    accId = entity.Id;


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Tuesday, February 28, 2012 8:55 AM
    Moderator
  • Hi david,

    Thanks for Answering.

    I also need to copy this retrieved Account Record to a custom entity name "Student".

    It'll be very helpful if you'll give me a example piece of code or link for it.


    CRMAbhi


    • Edited by Crm.Abhi Tuesday, February 28, 2012 9:57 AM
    Tuesday, February 28, 2012 9:56 AM
  • Just create a new entity record:

    var entity = new Entity("new_studentorwhatever");

    Then repeat for each attribute you need to copy. Remember to set correct Type:

    entity.Attributes.Add("new_attributename", RecordAttributes.GetAttribute<type>("attributename");

    When you're finished, create the student entity:

    Service.Create(entity);

    • Marked as answer by Crm.Abhi Tuesday, February 28, 2012 11:18 AM
    Tuesday, February 28, 2012 10:10 AM