Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin: Obtaining information from related entity

Posted on by 210

Hi experts - I am writing a plugin.  We have a 1:N relationship between contract (one) to contract change (many)

Scenario: on create of a contract change I need to get information from the related contract in order to populate a field on the contract change.

As I am writing my first plugin I'm unsure how to proceed. How do I get a reference to the related contract entity so that I might obtain the value of the "documenturl" attribute on the contract entity?

All the samples I'm finding via google are creating the "N" side of the relationship whereas I am trying to obtain reference to the 1: side of the relationship.

Any guidance would be greatly appreciated.

Diane

*This post is locked for comments

  • migsbeja Profile Picture
    migsbeja 655 on at
    RE: Plugin: Obtaining information from related entity

    [quote user="Tim Dutcher"]

    We really need a queue feature in the forums!  :-)

    [/quote]

    Yeah...a queue for everything...!

  • Verified answer
    Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Plugin: Obtaining information from related entity

    That variable references the CRM Organization Service. In the link I provided to the sample plug-in template, see this line:

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    So, both service and crmService are variables that store the same thing: the organization service that you can use to create, update, delete, etc. in CRM from within the plug-in.  I used the name "service" in one case and "crmService" in another but they both reference the same thing.

  • DianeF1 Profile Picture
    DianeF1 210 on at
    RE: Plugin: Obtaining information from related entity

    Tim - what is the  crmService?

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Plugin: Obtaining information from related entity

    We really need a queue feature in the forums!  :-)

  • Verified answer
    Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Plugin: Obtaining information from related entity

    Here's some code and comments that should help.

    // Get the Entity that is the target of the Create event. This should be your contract change entity.

    Entity targetEntity = (Entity)context.InputParameters["Target"];

    // Assuming that contract is a required field on the contract change record, get the contract id

    Guid contractId = targetEntity.GetAttributeValue<EntityReference>("contractid").Id;

    // Now, you can query the contract for the details you need

    Entity contractEntity = crmService.Retrieve("contract", contractId, new ColumnSet("documenturl");

    From here, you can get the values from the contract entity and carry on with your other plug-in logic.

    There's a basic structure of a plug-in here that might also help: mscrmrocks.wikispaces.com/CRM+Plug-ins

  • Suggested answer
    migsbeja Profile Picture
    migsbeja 655 on at
    RE: Plugin: Obtaining information from related entity

    Hi DianeF1

    To get the reference of the contract you need to get the EntityReference of the field Contract (relation).

    I don't know if you are with early or late bound, but here goes an example:

    Entity entity = (Entity)context.InputParameters["Target"]; <- Get the contect entity
    
    string documentUrl = "";
    
    if(entity.Attributes.Contains("contractid"))
    {
    	EntityReference contract = ((EntityReference)entity.Attributes["contractid"]);
    
    	string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    			<entity name='contract'>
    			<attribute name='documenturl' />
    			<filter type='and'>
    			<condition attribute='contractid' operator='eq' value='" + contract.Id + @"' />
    			</filter>
    			</entity>
    			</fetch>";
    
    	EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch));
    
    	if (result != null & result.Entities != null)
    
    	if (result.Entities.Count > 0)
    		if (result.Entities.Count == 1)
    			documentUrl  = ((EntityReference)result.Entities[0].Attributes["documenturl"]);
    }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans