SalesLine item dont save for SalesTable

Hi,

I am working on CRMAX integration project. (CRM 2011 and AX 2009)

I had written plugin on CRM Order entity to create SalesTable in AX.

And i write another plugin on CRM Order Products to update the SalesTable with SalesId.

It runs successfully. Debugged in. No error.

But the record is not inserted in SalesLine of that SalesTable in AX.

My code is as below for reference.

 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity orderproductEntity = (Entity)context.InputParameters["Target"];
                Guid orderproductId = (Guid)((Entity)context.InputParameters["Target"]).Id;

                // Verify that the target entity represents an salesorder.
                // If not, this plug-in was not registered correctly.
                if (orderproductEntity.LogicalName == "salesorderdetail")
                {

                    Guid orderId = ((EntityReference)orderproductEntity.Attributes["salesorderid"]).Id;
                    Entity _Order = _service.Retrieve("salesorder", orderId, new ColumnSet(new string[] { "new_pricegroup" }));

                    // Create an instance of the document class.
                    ax.AxdSalesOrder salesOrder = new ax.AxdSalesOrder();

                    // Create instances of the entities that are used in the service and
                    // set the needed fields on those entities.
                    ax.AxdEntity_SalesTable salesTable = new ax.AxdEntity_SalesTable();
                    salesTable.LanguageId = "en-in";
                    salesTable.SalesStatus = ax.AxdEnum_SalesStatus.Backorder;
                    salesTable.SalesStatusSpecified = true;
                    salesTable.SalesType = ax.AxdEnum_SalesType.Sales;
                    salesTable.SalesTypeSpecified = true;
                    salesTable.PurchOrderFormNum = "PO";
                    //ax.AxdEntity_SalesLine salesLine = new ax.AxdEntity_SalesLine();

                    ////Entity salesproduct = _service.Retrieve("salesorder", orderproductId, new ColumnSet("new_pricegroup"));

                    //salesLine.SalesPrice = 10;
                    //salesLine.SalesPriceSpecified = true;

                    //ax.AxdEntity_InventDim inventDim = new ax.AxdEntity_InventDim();
                    //inventDim.configId = "-";            //default value
                    //inventDim.InventColorId = "0";       //default value
                    //inventDim.InventSizeId = "NVIDIA";   //default value

                    //// Add the sub-entity instances to their parent entities as an array
                    //// of the sub-entity type.
                    //salesLine.InventDim = new ax.AxdEntity_InventDim[1] { inventDim };

                    //////******//////
                    //////******//////
                    ax.CriteriaElement[] criteriaElements = new ax.CriteriaElement[1];

                    criteriaElements[0] = new ax.CriteriaElement();

                    criteriaElements[0].DataSourceName = "SalesTable";

                    criteriaElements[0].FieldName = "SalesId";

                    criteriaElements[0].Value1 = _Order.Attributes["new_pricegroup"].ToString();

                    ax.QueryCriteria queryCriteria = new ax.QueryCriteria();

                    queryCriteria.CriteriaElement = criteriaElements;
                    //////*****//////
                    //////*****//////

                    try
                    {
                        ax.AxdSalesOrder foundSalesOrder = null;
                        foundSalesOrder = sc.find(queryCriteria);

                        // Call the create method on the service passing in the document.
                        //ax.EntityKey[] returnedSalesOrderEntityKey;
                        //ax.KeyField salesorderId = new ax.KeyField();
                        //salesorderId.Field = "SalesId";
                        //salesorderId.Value = _Order.Attributes["new_pricegroup"].ToString();
                        //ax.EntityKey myEntityKey = new ax.EntityKey();
                        //myEntityKey.KeyData = new ax.KeyField[1] { salesorderId };
                        //returnedSalesOrderEntityKey = new ax.EntityKey[1] { myEntityKey };

                        ax.EntityKey[] entityKeyList = new ax.EntityKey[1];
                        ax.EntityKey entityKey = new ax.EntityKey();
                        ax.KeyField[] keyDataList = new ax.KeyField[1];
                        ax.KeyField keyField = new ax.KeyField();

                        keyField.Field = "SalesId";
                        keyField.Value = _Order.Attributes["new_pricegroup"].ToString();
                        keyDataList[0] = keyField;
                        entityKey.KeyData = keyDataList;
                        entityKeyList[0] = entityKey;
                        //////*****//////
                        //////*****//////
                        ax.AxdEntity_SalesLine findsalesLine = null;
                        ax.AxdEntity_InventDim findinventDim = null;

                        foreach (ax.AxdEntity_SalesTable findsalesTable in foundSalesOrder.SalesTable)
                        {
                            findsalesLine = new ax.AxdEntity_SalesLine();
                            findinventDim = new ax.AxdEntity_InventDim();
                            findsalesTable._DocumentHash = findsalesTable._DocumentHash;
                            //findsalesTable.RecId = salesTable.RecId;
                            //findsalesTable.RecVersion = salesTable.RecVersion;

                            if (orderproductEntity.Attributes.Contains("productid"))
                            {
                                Guid productId = ((EntityReference)orderproductEntity.Attributes["productid"]).Id;
                                Entity product = _service.Retrieve("product", productId, new ColumnSet("productnumber","name"));
                                findsalesLine.ItemId = product.Attributes["productnumber"].ToString();
                                findsalesLine.Name = product.Attributes["name"].ToString();
                            }

                            if (_Order.Attributes.Contains("new_pricegroup"))
                            {
                                string salesIdonProduct = _Order.Attributes["new_pricegroup"].ToString();
                                findsalesLine.SalesId = salesIdonProduct;
                            }
                            if (orderproductEntity.Attributes.Contains("quantity"))
                            {
                                findsalesLine.SalesQty = Convert.ToDecimal(orderproductEntity.Attributes["quantity"]);
                            }
                            if (orderproductEntity.Attributes.Contains("priceperunit"))
                            {
                                findsalesLine.SalesPrice = Convert.ToDecimal(orderproductEntity.Attributes["priceperunit"]);
                                findsalesLine.SalesPriceSpecified = true;
                            }

                            findsalesLine.SalesUnit = "NOS";
                            findinventDim.InventSiteId = "GJ-STOCK";
                            findinventDim.InventLocationId = "WH-SARKHEJ";
                            findinventDim.configId = "-";            //default value
                            findinventDim.InventColorId = "0";       //default value
                            findinventDim.InventSizeId = "NVIDIA";   //default value

                            findsalesLine.InventDim = new ax.AxdEntity_InventDim[1] { findinventDim };
                            salesTable.SalesLine = new ax.AxdEntity_SalesLine[1] { findsalesLine };
                            salesOrder.SalesTable = new ax.AxdEntity_SalesTable[1] { findsalesTable };
                        }

                        //////*****//////
                        //////*****//////

                        sc.update(entityKeyList, foundSalesOrder);

                        // The create method returns an EntityKey which contains the ID of the sales order.

                        //ax.EntityKey key = returnedSalesOrderEntityKey[0];
                        //ax.KeyField kfld = key.KeyData[0];
                        //orderproductEntity.Attributes["new_pricegroup"] = kfld.Value;
                    }
                    catch (Exception e)
                    {

                    }
                }
            }

Do anyone encountered this situation ?