locked
Get parent entity info RRS feed

  • Question

  • Hello All,

    I am new to CRM development. My requirement is to write a custom plug-in to auto generate numbers.
    The entity structure (hierarchy) is as follows:

    1. Oppurtunity
     
       1.1 Resource Request

             1.1.1 Resource Assignment

    The numbering for these entities should be as follows:

    Oppurtunity - Oppr0001
    Resource Request - Oppr0001 - 001
    Resource Assignment - Oppr0001 - 001 - 01

    I am able to generate number for Oppurtunity. But, for resource request, I dont know how to get the value of its parent (oppurtunity) entity id i.e. Oppr0001 (in the above case).

    Do we need to use any web services to get that info?

    Could you please guide me?

    Thanks,
    Naga
    Wednesday, December 16, 2009 7:04 PM

Answers

  • You will have to retrieve the opportunity record that is associated to the resource request record. The following function will help you retrieve the record from CRM (you should also add in try and catch to the function below):

    private DynamicEntity GetEntity(ICrmService crm, Guid recordid, string entityname, string[] attributes)
            {            
                    TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                    target.EntityId = recordid;
                    target.EntityName = entityname;
    
                    RetrieveRequest request = new RetrieveRequest();
                    request.ColumnSet = new ColumnSet(attributes);
                    request.ReturnDynamicEntities = true;
                    request.Target = target;
    
                    RetrieveResponse response = (RetrieveResponse)crm.Execute(request);
    
                    return (DynamicEntity)response.BusinessEntity;
           }
    Shafraz

    http://crmdude.wordpress.com/
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 7:21 PM
  • That's basically what I'm getting at.  If the child entity is established as a DynamicEntity, then you call it thusly:

    Guid guid = ((Lookup)childEntity.Properties["something_opportunityid"]).Value;


    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 10:00 PM
    Moderator
  • The entity name can be "hard coded" with the directive:  EntityName.opportunity.ToString();  //This is an enumeration provided by the SDK.
    As for the recordid, you gather this from the relationship attribute on the child entity.  Probably something like "new_opportunityid".  You'll have to cast this attribute as a Lookup from the Properties collection and then use the Value member to reference the GUID (if you're using a DynamicEntity), or reference the member attribute directly from the strong entity type (e.g. childEntity.new_opportunityid.Value).


    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 8:17 PM
    Moderator
  • It should.  You need to make sure you know exactly what Lookup attribute on the child entity that contains the reference to the parent.  That field name belongs in the quotes, and the code should work.

    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Friday, December 18, 2009 7:57 PM
    Moderator

All replies

  • You will have to retrieve the opportunity record that is associated to the resource request record. The following function will help you retrieve the record from CRM (you should also add in try and catch to the function below):

    private DynamicEntity GetEntity(ICrmService crm, Guid recordid, string entityname, string[] attributes)
            {            
                    TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                    target.EntityId = recordid;
                    target.EntityName = entityname;
    
                    RetrieveRequest request = new RetrieveRequest();
                    request.ColumnSet = new ColumnSet(attributes);
                    request.ReturnDynamicEntities = true;
                    request.Target = target;
    
                    RetrieveResponse response = (RetrieveResponse)crm.Execute(request);
    
                    return (DynamicEntity)response.BusinessEntity;
           }
    Shafraz

    http://crmdude.wordpress.com/
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 7:21 PM
  • Thanks shafraz,

    Where can I get the recordid and entityname? I can hardcode the entity name to "Oppurtunity" but, where can I get the entity Id?


    Thanks,
    Naga
    Wednesday, December 16, 2009 7:32 PM
  • The entity name can be "hard coded" with the directive:  EntityName.opportunity.ToString();  //This is an enumeration provided by the SDK.
    As for the recordid, you gather this from the relationship attribute on the child entity.  Probably something like "new_opportunityid".  You'll have to cast this attribute as a Lookup from the Properties collection and then use the Value member to reference the GUID (if you're using a DynamicEntity), or reference the member attribute directly from the strong entity type (e.g. childEntity.new_opportunityid.Value).


    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 8:17 PM
    Moderator
  • David, thanks for your reply. I am using Dynamic Entity and I am not quite sure if I followed what you said. Appologies for my lack of knowledge on this, its been 2 days since I am into this custom plug-in development.

    Could you please elaborate on it, probably a sample code?

    Would it look something like this?

    Guid guid = ((Lookup) {something}.Value;

    Thanks,

    Naga


    Wednesday, December 16, 2009 9:41 PM
  • That's basically what I'm getting at.  If the child entity is established as a DynamicEntity, then you call it thusly:

    Guid guid = ((Lookup)childEntity.Properties["something_opportunityid"]).Value;


    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Wednesday, December 16, 2009 10:00 PM
    Moderator
  • Hello Dave,

    I get this error when I add the above line of code - "Unable to cast oobject of type 'System.String' to type 'Microsoft.Crm.Sdk.Lookup'"

    Moreover, the id filed for opportunity and Resource Request are the same, (i.e. xxx_id). So, when we specify

    Guid guid = ((Lookup)childEntity.Properties["xxx_id"]).Value; would it be able to get the oppurtunity entity guid?


    Thanks,
    Naga
    Friday, December 18, 2009 3:42 PM
  • It should.  You need to make sure you know exactly what Lookup attribute on the child entity that contains the reference to the parent.  That field name belongs in the quotes, and the code should work.

    Dave Berry
    • Marked as answer by drifters85 Monday, December 21, 2009 3:17 PM
    Friday, December 18, 2009 7:57 PM
    Moderator
  • Thanks Dave, it worked.
    Monday, December 21, 2009 3:17 PM