Wednesday, September 5, 2012

crm 2011 -plugin to convert an entity and related entities

protected void ExecutePostRequisitionUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

             IPluginExecutionContext context = localContext.PluginExecutionContext;
             IOrganizationService service = localContext.OrganizationService;
   


    // The InputParameters collection contains all the data passed in the message request.
             if (context.InputParameters.Contains("Target") &&
             context.InputParameters["Target"] is Entity)
             {
                 // Obtain the target entity from the input parmameters.
                 Entity entity = (Entity)context.InputParameters["Target"];
                 ColumnSet cols = new ColumnSet(true);

                 var requisitionObj = service.Retrieve(jmh_requisition.EntityLogicalName, entity.Id, cols);
                 jmh_requisition currentRequisition = (jmh_requisition)requisitionObj;
                 if (entity.Attributes.Contains("statuscode"))
                 {
                     OptionSetValue val = (OptionSetValue)entity["statuscode"];
                     if (val.Value == 170000002)
                     {
                         var requisition = currentRequisition;
                      
                         var newPurchseOrder = new jmh_purchaseorder();
                         newPurchseOrder.jmh_supplier = requisition.jmh_supplier;
                         newPurchseOrder.new_Items = requisition.jmh_item;
                         newPurchseOrder.jmh_itemscost = requisition.jmh_itemcost;
                         newPurchseOrder.jmh_totalcost = requisition.jmh_totalcost;
                         newPurchseOrder.jmh_store = requisition.jmh_storeid;
                         EntityReference currentReq = new EntityReference();
                         currentReq.Id = requisition.Id;
                         currentReq.LogicalName = jmh_requisition.EntityLogicalName;
                         newPurchseOrder.jmh_requisition = currentReq;
                         var newPoId = service.Create(newPurchseOrder);
                         var query = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
<entity name='jmh_requisitionproduct'>
<attribute name='jmh_requisitionproductid'/>
<attribute name='jmh_existingproduct'/>
<attribute name='jmh_producttype'/>

<attribute name='jmh_unitcost'/>
<attribute name='jmh_quantity'/>
<attribute name='jmh_linecost'/>

<attribute name='jmh_name'/>
<attribute name='createdon'/>
<order descending='false' attribute='jmh_name'/>
<filter type='and'>
<condition attribute='jmh_requisition' value='" + currentRequisition.Id.ToString() + @"' uiname='" + currentRequisition.jmh_name
                                                         + @"' uitype='jmh_requisition'  operator='eq'/> </filter>
</entity>
 </fetch>";
                       

                         RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                         req.Query = new FetchExpression(query);
                         RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(req);
                         EntityCollection roProducts = response.EntityCollection;
                         foreach (var roProduct in roProducts.Entities)
                         {
                             var newPOproduct = new jmh_purchaseorderproduct();
                          

                             newPOproduct.jmh_ProductType = (bool)roProduct["jmh_producttype"];
                             EntityReference createdPo = new EntityReference();
                             createdPo.Id = newPoId;
                             createdPo.LogicalName = jmh_purchaseorder.EntityLogicalName;
                             newPOproduct.jmh_purchaseorder = createdPo;

                             if (roProduct.Attributes.ContainsKey("jmh_existingproduct") && roProduct["jmh_existingproduct"] != null)
                             {
                                 newPOproduct.jmh_existingproduct = (EntityReference)roProduct["jmh_existingproduct"];
                             }
                             if (roProduct["jmh_name"] != null && roProduct.Attributes.ContainsKey("jmh_name"))
                             {
                                 newPOproduct.jmh_writeinproduct = roProduct["jmh_name"].ToString();
                             }
                             if (roProduct["jmh_linecost"] != null && roProduct.Attributes.ContainsKey("jmh_linecost"))
                             {
                                 newPOproduct.jmh_linecost = (Money)roProduct["jmh_linecost"];
                             }

                             if (roProduct["jmh_quantity"] != null && roProduct.Attributes.ContainsKey("jmh_quantity"))
                             {
                                 newPOproduct.jmh_quantity = Convert.ToInt32(roProduct["jmh_quantity"]);
                             }
                             if (roProduct["jmh_unitcost"] != null && roProduct.Attributes.ContainsKey("jmh_unitcost"))
                             {
                                 newPOproduct.jmh_unitcost = (Money)roProduct["jmh_unitcost"];
                             }
                             service.Create(newPOproduct);
                         }
                     }
                 }
             }   
            // TODO: Implement your custom Plug-in business logic.
        }

No comments:

Post a Comment