tollamie
tollamie

Reputation: 117

How to retrieve data from custom entity?

This code helps to retrieve the contact records based on the relation 1:N from the account entity

I want to retrieve the records of another custom entity called company from the account entity however I don't know how to call custom data entities to use it in the RetrieveContact method
- contact is a standard entity and company is a custom entity

PS: the account lookup of the company entity is called cs_accountid

BusinessEntityCollection bec = RetrieveContact(Service, context, ((Key)(Account.Properties["accountid"])).Value.ToString(), "contact", "parentcustomerid");
                            if (bec.BusinessEntities.Count > 0)
                            {
                                foreach (contact c in bec.BusinessEntities)
                                {
                                    c.parentcustomerid = new Customer();
                                    c.parentcustomerid.type = "account";
                                    c.parentcustomerid.Value = k.Value;
                                    Service.Update(c);
                                }
                            }

private BusinessEntityCollection RetrieveContact(ICrmService service, IPluginExecutionContext context, string AccountID, string FromEntityName, string FromAttributeName)
        {

            // Create the ConditionExpression.
            ConditionExpression condition = new ConditionExpression();
            condition.AttributeName = "accountid";
            condition.Operator = ConditionOperator.Equal;
            condition.Values = new string[] { AccountID };

            // Create the Link entities
            LinkEntity le = new LinkEntity();
            le.LinkFromEntityName = FromEntityName;
            le.LinkFromAttributeName = FromAttributeName;
            le.LinkToEntityName = "account";
            le.LinkToAttributeName = "accountid";

            // Create the FilterExpression.
            FilterExpression filter = new FilterExpression();

            // Set the properties of the filter.
            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(condition);
            le.LinkCriteria = filter;

            // Create the QueryExpression object.
            QueryExpression query = new QueryExpression();

            // Set the properties of the QueryExpression object.
            query.EntityName = FromEntityName;// EntityName.contact.ToString();
            query.ColumnSet = new AllColumns();// cols;
            query.LinkEntities.Add(le);
            //query.AddOrder("lastname", OrderType.Ascending);

            BusinessEntityCollection bec = service.RetrieveMultiple(query);
            return bec;
        }

private DynamicEntity RetournRecord(string entityname, Guid recordid, TargetRetrieveDynamic target, ICrmService Service)
        {
            target.EntityId = recordid;
            target.EntityName = entityname;
            RetrieveRequest retrieve = new RetrieveRequest();
            retrieve.Target = target;
            retrieve.ColumnSet = new AllColumns();
            retrieve.ReturnDynamicEntities = true;
            // Create a response reference and execute the retrieve request.
            RetrieveResponse response1 = (RetrieveResponse)Service.Execute(retrieve);
            return (DynamicEntity)response1.BusinessEntity;
        }

Thank you for your help and your time!

enter image description here

Upvotes: 0

Views: 1615

Answers (2)

Moonblade
Moonblade

Reputation: 11

I know this is old, but the problem with the createquery is due to a typo-> ["cs_accountid") It should end with a square bracket not a round one.

Upvotes: 1

Tobias Koller
Tobias Koller

Reputation: 2186

UPDATE: this solution is for CRM 2011/2013 and not for CRM 4.0:

i never used earlybinding so this is my example for the latebinding (will work for you, too).

using Microsoft.Xrm.Sdk.Client;

...

var service = OrganizationServiceFactory.CreateOrganizationService(PluginExecutionContext.UserId);
using (var context = new OrganizationServiceContext(service))
{
     List<Entity> myCompanyRecords = context.CreateQuery("cs_company").Where(o => ((EntityReference)o["cs_accountid").Id.Equals(id)).ToList();
}

the "id" is the Guid of your account-entity-record.

in your method you already have the parameter "service" and "context" so you can just use this:

List<Entity> myCompanyRecords = context.CreateQuery("cs_company").Where(o => ((EntityReference)o["cs_accountid").Id.Equals(id)).ToList();

Upvotes: 0

Related Questions