How do I find a single record so I can then associate it with another record? RRS feed

  • Question

  • I have the following custom entities which has these attributes (truncated to only display the relevant attributes).


    contactrelationship entity

    • contactid (datatype is of the built-in contact entity)
    • relationshipid (datatype is of custom relationship entity)
    • organizationid (datatype is of built-in account entity)

    relationship entity (works like a list of values. like values for populating a dropdown list. nothing special)

    • relationshipid (guid datatype which is the primary key)
    • description (sample values would be "staff" or "customer" or "visitor" and etc)


    What I want is:

    • to get a single "staff" record in the relationship entity (something like select * from relationship where description like 'staff%')
    • to get a single record from the account entity (something like select * from account where id=1)
    • to get all the record from the contact entity (I am using RetrieveMultiple function for this)
    • and then save a new entry in the contactrelationship entity.

    I know there's a Retrieve function in CrmService but I don't think it can do what I want (see sample SQL above).

    Can you please help? Thanks very much in advance. :)

    ps. I'm new to Microsoft Dynamics so my terminology may be incorrect. I am trying though! :)

    Wednesday, June 18, 2014 6:34 AM

All replies

  • G'day Jay and welcome to the world of CRM

    Firstly you might want to post this in the CRM Development forum as its not really a CRM Deployment question.

    Jay you should probably have a look at the CRM SDK http://msdn.microsoft.com/en-us/library/hh547453(v=crm.5).aspx

    Its an invaluable resource with all the information you could ever consider & sometimes it even makes sense.

    Have a look at the Sample Code section http://msdn.microsoft.com/en-us/library/gg309721(v=crm.5).aspx you should find enough examples to help you along.

    To retrieve a single account record:

    var acct = Service.Retrieve("account", <Guid of Account to retrieve>, <Columns to Retrieve from Account entity>)

    To retrieve a single staff record:      

    var qry = new QueryExpression("relationship") {

            PageInfo = new PagingInfo() { ReturnTotalRecordCount = true }
            ColumnSet = <Columns to Return from Relationship entity>
            Criteria = new FilterExpression(LogicalOperator.And) {

              Conditions = {
                 new ConditionExpression("description", ConditionOperator.BeginsWith, "staff") // this performs the 'staff%'

    var qryRslts = Service.RetrieveMultiple(qry); 
    var entityRelationship = qryRslts.TotalRecordCount > 0 ? qryRslts[0] : null;

    To retrieve Contacts you can use another QueryExpression with the appropriate Conditions limiting the results returned to those you want

    Then to create a new record for contactrelationship :

    var ctRel = new Entity("contactrelationship");

    ctRel.Attributes.Add("relationshipid", new EntityReference("relationship", entityRelationship .Id);

    ctRel.Attributes.Add("contactid ", new EntityReference("contact", contact.Id); // note contact comes from the Contacts QueryExpression

    ctRel.Attributes.Add("organizationid", new EntityReference("account", acct .Id);


    Hope this gives you a start & good luck

    Tuesday, July 1, 2014 3:40 AM