Using CompoundUpdateRequest in Dynamics CRM 4.0


Sometime when we are integrating ms crm with another application we need to send CRM data to another application and also need to keep track if data is already sent or not. For example if you are sending sales order and sales order line items and you need to update sales order and line item after sending, you can use  compoundupdaterequest message to update sales order and order line items in one go.

 

private BusinessEntityCollection ReadAllOrderLineItems(Guid _ID, ICrmService _iService)

        {

            RetrieveMultipleResponse retrieved = null;

            try

            {

                ConditionExpression _Condition = new ConditionExpression();

                _Condition.Operator = ConditionOperator.Equal;

                _Condition.AttributeName = “salesorderid”;

                _Condition.Values = new object[] { _ID };

                 FilterExpression _Filter = new FilterExpression();

                _Filter.FilterOperator = LogicalOperator.And;

                _Filter.Conditions.AddRange(new ConditionExpression[] { _Condition });

                 QueryExpression query = new QueryExpression();

                query.ColumnSet = new AllColumns();

                query.Criteria = _Filter;

                query.EntityName = “salesorderdetail”;

                RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

                retrieve.Query = query;

                retrieve.ReturnDynamicEntities = true;

                retrieved = (RetrieveMultipleResponse)_iService.Execute(retrieve);

            }

            catch (SoapException Ex)

            {

                throw Ex;

            }

            catch (Exception Excep)

            {

                throw Excep;

            }

            return retrieved.BusinessEntityCollection;

         }

//Get all orderline items for particular salesorderid
BusinessEntityCollection _OrderLineItems = ReadAllOrderLineItems(_SalesorderID,_iCrmService); //I have written this function to fetch all order line items based on salesorder id.
 //Create Dynamic entity array to store salesorderdetails
 DynamicEntity[] _SalesorderDetailArr=new DynamicEntity[_OrderLineItems.BusinessEntities.Count];
 for(int i=0; i<_OrderLineItems.BusinessEntities.Count; i++)
            {
               //get salesorder key
                DynamicEntity _ExistingSalesorderItem = (DynamicEntity)_OrderLineItems.BusinessEntities[i];
 // create properties for sales order detail, in my example I have created a bit field in sales order and sales order detail

                DynamicEntity _Salesorderitem=new DynamicEntity();
                _Salesorderitem.Name = “salesorderdetail”;
                 //Add property to set created flag
                 CrmBooleanProperty _Created = new CrmBooleanProperty();
                _Created.Name = “new_created”;
                _Created.Value = new CrmBoolean();
                _Created.Value.Value = true;
                 _Salesorderitem.Properties.Add(_Created);
                 //set key property
                KeyProperty _SalesorderDetailKey = new KeyProperty();
                _SalesorderDetailKey.Name = “salesorderdetailid”;
                _SalesorderDetailKey.Value = new Key();
                _SalesorderDetailKey.Value.Value = ((Key)_ExistingSalesorderItem.Properties[“salesorderdetailid”]).Value;
                _Salesorderitem.Properties.Add(_SalesorderDetailKey);
                _SalesorderDetailArr[i] = _Salesorderitem;
            }

            DynamicEntity _Salesorder = new DynamicEntity();
            _Salesorder.Name = “salesorder”;
            _Salesorder.Properties.Add(new KeyProperty(“salesorderid”, new Key(_ID)));
            CrmBooleanProperty _Created = new CrmBooleanProperty();
            _Created.Name = “new_created”;
            _Created.Value = new CrmBoolean();
            _Created.Value.Value = true;
            _Salesorder.Properties.Add(_Created);
            TargetCompoundDynamic _TargetUpdate = new TargetCompoundDynamic();
            _TargetUpdate.ChildEntities = _SalesorderDetailArr;
            _TargetUpdate.Entity = _Salesorder;
            CompoundUpdateRequest _Request = new CompoundUpdateRequest();
            _Request.Target = _TargetUpdate;
            CompoundUpdateResponse _Response = (CompoundUpdateResponse)_iService.Execute(_Request);

Advertisements

2 Comments

Filed under MS CRM 4.0, Workflow & Plugin Development

2 responses to “Using CompoundUpdateRequest in Dynamics CRM 4.0

  1. Joe

    Is it possible for you to post the ReadAllOrderLineItems() function? Will be a big help.

    Regards,
    Joe

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s