[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1120
  • Last Modified:

CRM Plug-in

I have a form in CRM called "Manual Purchase". By using a plug - in I want to retrieve the columns on that form and read the data out of that. Can someone tell me what kind of code I will need in order to retrieve the entity and then go through the controls of the entity.
I have attached a piece of code which does the same kind of stuff with "Opportunity" screen.
The problem I have is that I do not have somethng similiar to "TargetRetrieveOpportunity" for Manual Purchase,something like "TargetRetrieveManualPurchase" or similiar.

TargetRetrieveOpportunity target = new TargetRetrieveOpportunity();
                            target.EntityId = opportunityID;
                            RetrieveRequest retrieve = new RetrieveRequest();
                            retrieve.Target = target;
                            retrieve.ColumnSet = new AllColumns();
                            retrieve.ReturnDynamicEntities = true;
                            RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);

Open in new window

0
pratikshahse
Asked:
pratikshahse
1 Solution
 
rakeshAgarwalCommented:
You have to use dynamic entity.  You have to go through sdk to learn this.  I will pass on the code if i get one

regards
Rakesh Agarwal
0
 
Biffster007Commented:
Hi,

If this is a Post Plug-in you will receive only what has changed in the Plug-in execution context.  If you want to look at all fields of the form you'll have to get the id from the plug-in execution context and retrieve the full entity using all columns.  After this has been retrieved you can look at all the fields.

Depending on the plug-in message will depend on how you retrieve the entity id.  Let's assume it's a post update plug-in then you would do it something like the below:

public void Execute(IPluginExecutionContext context)
        {
            switch(context.PrimaryEntityName.ToLower())
            {
                case "account":
                    ProcessAccounts(context);
                    break;
                case "new_manualpurchase":
                    ProcessManualPurchases(context);
                    break;
            }
        }


private void ProcessManualPurchases(IPluginExecutionContext context)
        {
          if (context.Stage.ToString() == "50")
            {
                if (context.MessageName.ToLower() == "update")
                {
                    if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
                    {
                        DynamicEntity manualPurchase = (DynamicEntity)context.InputParameters.Properties["Target"];
                        Key manualPurchaseKey = (Key) manualPurchase.Properties["new_manualpurchaseid"];
                        DynamicEntity fullManualPurchase = GetFullDynamic(manualPurchaseKey.Value.ToString(), "new_manualpurchaseid", "new_manualpurchase");
                        //Do something with the dynamic entity
                                         
                    }
                }
             }
         }

private DynamicEntity GetFullDynamic(string entityId, string fieldName, string entityName)
        {
            ConditionExpression con1 = new ConditionExpression();
            con1.AttributeName = fieldName;
            con1.Operator = ConditionOperator.Equal;
            con1.Values = new string[] { entityId };
            FilterExpression filter = new FilterExpression();
            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(con1);
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.Criteria = filter;
            query.EntityName = entityName;

            RetrieveMultipleRequest request = new RetrieveMultipleRequest();
            request.Query = query;
            request.ReturnDynamicEntities = true;

            try
            {
                RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmServ.Execute(request);
                if (response != null && response.BusinessEntityCollection.BusinessEntities.Count == 1)
                    return (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
                //Null or more than one entity found so return null
                return null;
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                string errorMessage = ex.Message.ToString();
                return null;
            }
        }


Now I've guessed at the names obviously and changed the code outside of VS so there may be a couple of syntax errors but you get the idea...

Use the DynamicEntity.Properties.Contains method to check if your fields exists and go from there.

Cheers,
MH
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now