Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Double XML Creation

(0) ShareShare
ReportReport
Posted on by

Hi All

I'm having a bit of an odd issue. I have a plugin that on create order sends the order via xml to our accounts package. This has been working fine until yesterday. A while ago the plugin started to create the xml twice but this was never an issue but now it creates it twice but the second set of xml data has the item prices as 0. The first set of xml data has the correct prices as you can see by the outputs below. I have looked through my code and can't see any reason why it is duplicating the xml and on the second xml the item cost is 0. Any ideas would be welcome. I have put the xml 1 and 2 and the code below. Just to confirm the 2 set of data over writes the first set.

Kind Regards


Dan

XML Outputs

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfWebSalesOrder _tmplitem="8"  xmlns:xsd="www.w3.org/.../XMLSchema" xmlns:xsi="www.w3.org/.../XMLSchema-instance">
  <WebSalesOrder _tmplitem="8" >
    <currency_code _tmplitem="8" >GBP</currency_code>
    <order_date _tmplitem="8" >2019-02-25T15:33:20</order_date>
    <total _tmplitem="8" >766.46</total>
    <lv_ordertype _tmplitem="8" >Order</lv_ordertype>
    <references _tmplitem="8" >
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >7</reference_index>
      </WebReference>
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >8</reference_index>
      </WebReference>
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >11</reference_index>
      </WebReference>
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >16</reference_index>
      </WebReference>
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >17</reference_index>
      </WebReference>
      <WebReference _tmplitem="8" >
        <reference_index _tmplitem="8" >3</reference_index>
      </WebReference>
    </references>
    <flags _tmplitem="8" >
      <WebFlagString _tmplitem="8" >
        <flagstring_index _tmplitem="8" >5</flagstring_index>
      </WebFlagString>
    </flags>
    <texts _tmplitem="8" >
      <WebText _tmplitem="8" >
        <text_index _tmplitem="8" >1</text_index>
      </WebText>
      <WebText _tmplitem="8" >
        <text_index _tmplitem="8" >3</text_index>
      </WebText>
      <WebText _tmplitem="8" >
        <text_index _tmplitem="8" >2</text_index>
      </WebText>
      <WebText _tmplitem="8" >
        <text_index _tmplitem="8" >4</text_index>
      </WebText>
    </texts>
    <customer _tmplitem="8"  ></customer>
    <delivery _tmplitem="8"  ></delivery>
    <items _tmplitem="8" >
      <WebSalesOrderItem _tmplitem="8" >
        <goods _tmplitem="8" >451.9600</goods>
        <product_code _tmplitem="8" >MR1800T-S</product_code>
        <quantity _tmplitem="8" >2</quantity>
        <total _tmplitem="8" >451.9600</total>
        <unit _tmplitem="8"  ></unit>
        <itemtexts _tmplitem="8" >
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >3</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >4</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="8" >
        <goods _tmplitem="8" >24.5000</goods>
        <product_code _tmplitem="8" >EX3068425</product_code>
        <quantity _tmplitem="8" >1</quantity>
        <total _tmplitem="8" >24.5000</total>
        <unit _tmplitem="8"  ></unit>
        <itemtexts _tmplitem="8" >
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >3</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >4</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="8" >
        <goods _tmplitem="8" >90.0000</goods>
        <product_code _tmplitem="8" >EX3068350</product_code>
        <quantity _tmplitem="8" >18</quantity>
        <total _tmplitem="8" >90.0000</total>
        <unit _tmplitem="8"  ></unit>
        <itemtexts _tmplitem="8" >
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >3</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >4</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="8" >
        <goods _tmplitem="8" >200.0000</goods>
        <product_code _tmplitem="8" >/PL/14-24 VKM</product_code>
        <quantity _tmplitem="8" >1</quantity>
        <total _tmplitem="8" >200.0000</total>
        <unit _tmplitem="8"  ></unit>
        <itemtexts _tmplitem="8" >
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >3</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="8" >
            <itemtext_index _tmplitem="8" >4</itemtext_index>
            <itemtext_value _tmplitem="8"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
    </items>
  </WebSalesOrder>
</ArrayOfWebSalesOrder>

Number 2
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfWebSalesOrder _tmplitem="9"  xmlns:xsd="www.w3.org/.../XMLSchema" xmlns:xsi="www.w3.org/.../XMLSchema-instance">
  <WebSalesOrder _tmplitem="9" >
    <currency_code _tmplitem="9" >GBP</currency_code>
    <customer_reference _tmplitem="9" >ORD-05666-C7T8G8</customer_reference>
    <delivery_instructions _tmplitem="9" > </delivery_instructions>
    <order_date _tmplitem="9" >2019-02-25T15:33:17</order_date>
    <delivery_date _tmplitem="9" >2019-02-27T00:00:00</delivery_date>
    <total _tmplitem="9" >0.00</total>
    <web_id _tmplitem="9" >KCD837/MA/000854</web_id>
    <lv_ordertype _tmplitem="9" >Order</lv_ordertype>
    <references _tmplitem="9" >
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >7</reference_index>
        <reference_value _tmplitem="9" >TW</reference_value>
      </WebReference>
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >8</reference_index>
        <reference_value _tmplitem="9" >47</reference_value>
      </WebReference>
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >11</reference_index>
        <reference_value _tmplitem="9" >Sara </reference_value>
      </WebReference>
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >16</reference_index>
        <reference_value _tmplitem="9"  ></reference_value>
      </WebReference>
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >17</reference_index>
        <reference_value _tmplitem="9"  ></reference_value>
      </WebReference>
      <WebReference _tmplitem="9" >
        <reference_index _tmplitem="9" >3</reference_index>
        <reference_value _tmplitem="9" >QUO-06557-F3Y6C9</reference_value>
      </WebReference>
    </references>
    <flags _tmplitem="9" >
      <WebFlagString _tmplitem="9" >
        <flagstring_index _tmplitem="9" >5</flagstring_index>
        <flagstring_value _tmplitem="9" >0</flagstring_value>
      </WebFlagString>
    </flags>
    <texts _tmplitem="9" >
      <WebText _tmplitem="9" >
        <text_index _tmplitem="9" >1</text_index>
        <text_value _tmplitem="9" > </text_value>
      </WebText>
      <WebText _tmplitem="9" >
        <text_index _tmplitem="9" >3</text_index>
      </WebText>
      <WebText _tmplitem="9" >
        <text_index _tmplitem="9" >2</text_index>
        <text_value _tmplitem="9" > </text_value>
      </WebText>
      <WebText _tmplitem="9" >
        <text_index _tmplitem="9" >4</text_index>
        <text_value _tmplitem="9"  ></text_value>
      </WebText>
    </texts>
    <customer _tmplitem="9" >
      <customer_code _tmplitem="9" >504700</customer_code>
    </customer>
    <delivery _tmplitem="9" >
      <address_line_1 _tmplitem="9" >Site Offices</address_line_1>
      <address_line_2 _tmplitem="9" >Grove </address_line_2>
      <address_line_3 _tmplitem="9"  ></address_line_3>
      <address_line_4 _tmplitem="9" >Wantage</address_line_4>
      <address_line_5 _tmplitem="9" >OX</address_line_5>
      <delivery_code _tmplitem="9" >C7T8G8</delivery_code>
      <postcode _tmplitem="9"  ></postcode>
    </delivery>
    <items _tmplitem="9" >
      <WebSalesOrderItem _tmplitem="9" >
        <goods _tmplitem="9" >0.0000</goods>
        <product_code _tmplitem="9" >MR1800T-S</product_code>
        <quantity _tmplitem="9" >2</quantity>
        <total _tmplitem="9" >0.0000</total>
        <unit _tmplitem="9"  ></unit>
        <itemtexts _tmplitem="9" >
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >3</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >4</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="9" >
        <goods _tmplitem="9" >0.0000</goods>
        <product_code _tmplitem="9" >EX3068425</product_code>
        <quantity _tmplitem="9" >1</quantity>
        <total _tmplitem="9" >0.0000</total>
        <unit _tmplitem="9"  ></unit>
        <itemtexts _tmplitem="9" >
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >3</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >4</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="9" >
        <goods _tmplitem="9" >0.0000</goods>
        <product_code _tmplitem="9" >EX3068350</product_code>
        <quantity _tmplitem="9" >18</quantity>
        <total _tmplitem="9" >0.0000</total>
        <unit _tmplitem="9"  ></unit>
        <itemtexts _tmplitem="9" >
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >3</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >4</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
      <WebSalesOrderItem _tmplitem="9" >
        <goods _tmplitem="9" >0.0000</goods>
        <product_code _tmplitem="9" >/PL/14-24 VKM</product_code>
        <quantity _tmplitem="9" >1</quantity>
        <total _tmplitem="9" >0.0000</total>
        <unit _tmplitem="9"  ></unit>
        <itemtexts _tmplitem="9" >
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >3</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
          <WebItemText _tmplitem="9" >
            <itemtext_index _tmplitem="9" >4</itemtext_index>
            <itemtext_value _tmplitem="9"  ></itemtext_value>
          </WebItemText>
        </itemtexts>
      </WebSalesOrderItem>
    </items>
  </WebSalesOrder>
</ArrayOfWebSalesOrder>

My Code
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;
using Fullscope.Plugin;

namespace Zero2Ten.Plugin
{
    public class LVWebServiceConnector : IPlugin
    {
        internal IOrganizationService organizationService = null;
        public LVWebServiceConnector()
        {
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
            IPluginExecutionContext service = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            ITracingService extension = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (service.InputParameters.Contains("Target") && service.InputParameters["Target"] is Entity)
            {
                try
                {

                    IOrganizationServiceFactory organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    organizationService = organizationServiceFactory.CreateOrganizationService(new Guid?(service.UserId));
                    Entity order = service.InputParameters.Contains("Target") ? (Entity)service.InputParameters["Target"] : null;

                    // using (WebClient client = new WebClient())
                    // {

                    //     string response = Encoding.UTF8.GetString(responseBytes);

                    //     // For demonstration purposes, throw an exception so that the response
                    //     // is shown in the trace dialog of the Microsoft Dynamics CRM user interface.
                    //     throw new InvalidPluginExecutionException("WebClientPlugin completed successfully.");
                    // }

                    //APIConnector testconnector = new APIConnector();
                    //HttpWebResponse response = testconnector.GetAPI("link to account portal");
                    //throw new InvalidPluginExecutionException(response.StatusCode + "::" + response.StatusDescription);

                    if (service.PostEntityImages.Contains("postimage"))
                    {
                        order = service.PostEntityImages["postimage"];
                    }

                    //check if the order has already been processed
                    if (!order.Contains("sb_lvordernumber") || string.IsNullOrEmpty(order["sb_lvordernumber"].ToString()))
                    {
                        //transform the order to an LV Order Array
                        string xml = TransformOrder(order);
                        APIConnector connector = new APIConnector();

                        //write the response to a note on Order xml details
                        Entity note = new Entity("annotation");
                        note.Attributes.Add("subject", "LV Web Response");
                        note.Attributes.Add("notetext", xml);
                        note.Attributes.Add("objectid", new EntityReference("salesorder", order.Id));

                       organizationService.Create(note);


                        //call the web service
                        string webresponse = connector.postXMLData("https://linktoaccountportal", xml);

                       // Entity note = new Entity("annotation");
                       // note.Attributes.Add("subject", "LV Web Response");
                       // note.Attributes.Add("notetext", webresponse);
                       // note.Attributes.Add("objectid", new EntityReference("salesorder", order.Id));
                       // organizationService.Create(note);

                        //find the LV Order number in the response
                        int location = webresponse.LastIndexOf("LV order '");
                        if (location > 0)
                        {
                            string orderNumber = webresponse.Substring(location + 10, 6);
                            Entity updatedOrder = new Entity("salesorder");
                            updatedOrder.Id = order.Id;
                            updatedOrder["sb_lvordernumber"] = orderNumber;
                            organizationService.Update(updatedOrder);
                        }

                        
                    }
                }
                //   catch (Exception ex)
                //{
                // Exception exception = ex;
                // throw new InvalidPluginExecutionException("An error occurred in the LVWebServiceConnector.Execute plug-in: " + exception.Message, exception);
                // }

                catch (InvalidPluginExecutionException ex)
                {
                    extension.Trace("InvalidPluginExecutionException: " + ex.ToString(), new object[0]);
                    throw;
                }

                catch (Exception ex)
                {
                    
                    extension.Trace("Unhandled exception: " + ex.ToString(), new object[0]);
                    throw new InvalidPluginExecutionException(ex.ToString());
                }
               


                //   catch (System.Security.SecurityException secEx)
                //    {
                //        throw new InvalidPluginExecutionException(String.Format("An error occurred in the plug-in: {0} \n {1}.", secEx.Message, secEx.StackTrace));
                //    }


                //    catch (Exception ex)
                //     {
                //      if (ex.InnerException != null);
                //       throw new InvalidPluginExecutionException(String.Format("An error occurred in the plug-in: {0}", ex.Message));
                //   }


            }


        }

        private static string TransformCustomer(Entity order)
        {
            WebCustomer[] customers = new WebCustomer[1] { new WebCustomer() };
            if (order.Contains("billto_line1")) customers[0].address_line_1 = order["billto_line1"].ToString();
            if (order.Contains("billto_line2")) customers[0].address_line_1 = order["billto_line2"].ToString();
            if (order.Contains("billto_line3")) customers[0].address_line_1 = order["billto_line3"].ToString();
            if (order.Contains("billto_name")) customers[0].company_name = order["billto_name"].ToString();
            if (order.Contains("sb_billtoemail")) customers[0].contact_email = order["sb_billtoemail"].ToString();
            if (order.Contains("billto_contactname")) customers[0].contact_name = order["billto_contactname"].ToString();
            if (order.Contains("billto_telephone")) customers[0].contact_phone = order["billto_telephone"].ToString();
            if (order.Contains("sb_lvacccode")) customers[0].customer_code = order["sb_lvacccode"].ToString();
            if (order.Contains("billto_postalcode")) customers[0].postcode = order["billto_postalcode"].ToString();

            using (StringWriter textWriter = new StringWriter())
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(customers.GetType());
                serializer.Serialize(textWriter, customers);
                return textWriter.ToString();
            }

        }

        private string TransformOrder(Entity order)
        
        {


            //sales order
            WebSalesOrder websalesorder = new WebSalesOrder();
            if (order.Contains("sb_customerreference")) websalesorder.web_id = order["sb_customerreference"].ToString();
           // if (order.Contains("ordernumber")) websalesorder.web_id = order["ordernumber"].ToString();
            websalesorder.order_date = String.Format("{0:s}", DateTime.Now);
            websalesorder.currency_code = "GBP";
            websalesorder.lv_ordertype = "Order";
            if (order.Contains("totalamount")) websalesorder.total = Math.Round(((Money)order["totalamount"]).Value, 2).ToString();
            //if (order.Contains("name")) websalesorder.customer_reference = order["name"].ToString();
            if (order.Contains("ordernumber")) websalesorder.customer_reference = order["ordernumber"].ToString();
            if (order.Contains("sb_delinsc")) websalesorder.delivery_instructions = order["sb_delinsc"].ToString();
            // if (order.Contains("name")) websalesorder.customer_reference = order["name"].ToString();
            if (order.Contains("sb_reqdeldate")) websalesorder.delivery_date = String.Format("{0:s}", (DateTime)order["sb_reqdeldate"]);

            // References
    
            WebSalesOrderreferences[] websalesorderreferences = new WebSalesOrderreferences[6];
            websalesorderreferences[0] = new WebSalesOrderreferences();
           // websalesorder.references = websalesorderreferences;
           websalesorderreferences[0].reference_index ="7";
            if (order.Contains("sb_wateraut"))websalesorderreferences[0].reference_value = order["sb_wateraut"].ToString();
            websalesorderreferences[1] = new WebSalesOrderreferences();
              websalesorderreferences[1].reference_index = "8";
            if (order.Contains("sb_countycode")) websalesorderreferences[1].reference_value = order["sb_countycode"].ToString();
            websalesorderreferences[2] = new WebSalesOrderreferences();
            websalesorderreferences[2].reference_index = "11";
            if (order.Contains("sb_intsales")) websalesorderreferences[2].reference_value = order["sb_intsales"].ToString();
            websalesorderreferences[3] = new WebSalesOrderreferences();
            websalesorderreferences[3].reference_index = "16";
            if (order.Contains("sb_contaccname")) websalesorderreferences[3].reference_value = order["sb_contaccname"].ToString();
            websalesorderreferences[4] = new WebSalesOrderreferences();
            websalesorderreferences[4].reference_index = "17";
            if (order.Contains("sb_conaccno")) websalesorderreferences[4].reference_value = order["sb_conaccno"].ToString();
            websalesorderreferences[5] = new WebSalesOrderreferences();
            websalesorderreferences[5].reference_index = "3";
            if (order.Contains("sb_quoteno")) websalesorderreferences[5].reference_value = order["sb_quoteno"].ToString();


            websalesorder.references = websalesorderreferences;


            // Flags

            WebSalesOrderFlags[] websalesorderflags = new WebSalesOrderFlags[1];
            websalesorderflags[0] = new WebSalesOrderFlags();
            // websalesorder.references = websalesorderreferences;
            websalesorderflags[0].flagstring_index = "5";
            if (order.Contains("sb_isfpa")) websalesorderflags[0].flagstring_value = order["sb_isfpa"].ToString();
            websalesorder.flags = websalesorderflags;



            // WebSalesOrderreferencesWebReference[] webreferences = new WebSalesOrderreferencesWebReference[1];
            // webreferences[0].reference_index = "7";
            // webreferences[0].reference_value = "ST";
            //  websalesorderreferences[0].webreferences = webreferences;

            // websalesorder.references = websalesorderreferences;

            // text References

            WebSalesOrdertexts[] websalesordertexts = new WebSalesOrdertexts[4];
            websalesordertexts[0] = new WebSalesOrdertexts();
            websalesordertexts[0].text_index = "1";
            if (order.Contains("sb_delinsc")) websalesordertexts[0].text_value = order["sb_delinsc"].ToString();
            websalesordertexts[1] = new WebSalesOrdertexts();
            websalesordertexts[1].text_index = "3";
            if (order.Contains("shipto_contactname")) websalesordertexts[1].text_value = order["shipto_contactname"].ToString();
            websalesordertexts[2] = new WebSalesOrdertexts();
            websalesordertexts[2].text_index = "2";
            if (order.Contains("sb_orderinstructions")) websalesordertexts[2].text_value = order["sb_orderinstructions"].ToString();
            websalesordertexts[3] = new WebSalesOrdertexts();
            websalesordertexts[3].text_index = "4";
            if (order.Contains("sb_orcontre")) websalesordertexts[3].text_value = order["sb_orcontre"].ToString();


            websalesorder.texts = websalesordertexts;



            //customer
            WebSalesOrderCustomer[] websalesordercustomer = new WebSalesOrderCustomer[1];
            websalesordercustomer[0] = new WebSalesOrderCustomer();
            if (order.Contains("billto_line1")) websalesordercustomer[0].address_line_1 = order["billto_line1"].ToString();
            if (order.Contains("billto_line2")) websalesordercustomer[0].address_line_2 = order["billto_line2"].ToString();
            if (order.Contains("billto_line3")) websalesordercustomer[0].address_line_3 = order["billto_line3"].ToString();
            if (order.Contains("billto_name")) websalesordercustomer[0].company_name = order["billto_name"].ToString();
            if (order.Contains("sb_billtoemail")) websalesordercustomer[0].contact_email = order["sb_billtoemail"].ToString();
            if (order.Contains("billto_contactname")) websalesordercustomer[0].contact_name = order["billto_contactname"].ToString();
            if (order.Contains("billto_telephone")) websalesordercustomer[0].contact_phone = order["billto_telephone"].ToString();
            if (order.Contains("sb_lvacccode")) websalesordercustomer[0].customer_code = order["sb_lvacccode"].ToString();
            if (order.Contains("billto_postalcode")) websalesordercustomer[0].postcode = order["billto_postalcode"].ToString();

            websalesorder.customer = websalesordercustomer;

            //delivery
            WebSalesOrderDelivery[] websalesorderdelivery = new WebSalesOrderDelivery[1];
            websalesorderdelivery[0] = new WebSalesOrderDelivery();
            if (order.Contains("sb_del1")) websalesorderdelivery[0].address_line_1 = order["sb_del1"].ToString();
            if (order.Contains("sb_del2")) websalesorderdelivery[0].address_line_2 = order["sb_del2"].ToString();
            if (order.Contains("sb_del3")) websalesorderdelivery[0].address_line_3 = order["sb_del3"].ToString();
            if (order.Contains("sb_delcity")) websalesorderdelivery[0].address_line_4 = order["sb_delcity"].ToString();
            if (order.Contains("sb_delsta")) websalesorderdelivery[0].address_line_5 = order["sb_delsta"].ToString();
            if (order.Contains("shipto_name")) websalesorderdelivery[0].company_name = order["shipto_name"].ToString();

            if (order.Contains("shipto_contactname")) websalesorderdelivery[0].contact_name = order["shipto_contactname"].ToString();
            if (order.Contains("shipto_telephone")) websalesorderdelivery[0].contact_phone = order["shipto_telephone"].ToString();
            if (order.Contains("ordernumber") && order["ordernumber"].ToString().Length > 6) websalesorderdelivery[0].delivery_code = order["ordernumber"].ToString().Substring(order["ordernumber"].ToString().Length - 6);
            if (order.Contains("sb_delpost")) websalesorderdelivery[0].postcode = order["sb_delpost"].ToString();

            websalesorder.delivery = websalesorderdelivery;


            //order items

            EntityCollection orderitems = GetOrderItems(order.Id);
            WebSalesOrderItemsWebSalesOrderItem[] weborderitems = new WebSalesOrderItemsWebSalesOrderItem[orderitems.Entities.Count];
            

            int i = 0;
            foreach (Entity salesorderitem in orderitems.Entities)
            {
                weborderitems[i] = new WebSalesOrderItemsWebSalesOrderItem();
                

                if (salesorderitem.Contains("lineitemnumber")) weborderitems[i].lv_itemnumber = Convert.ToInt32(((decimal)salesorderitem["lineitemnumber"])).ToString();
                if (salesorderitem.Contains("sb_productnumber")) weborderitems[i].product_code = salesorderitem["sb_productnumber"].ToString();
                if (salesorderitem.Contains("quantity")) weborderitems[i].quantity = Convert.ToInt32(((decimal)salesorderitem["quantity"])).ToString();
              //  if (salesorderitem.Contains("discountpercentage")) weborderitems[i].discount_percentage = Convert.ToInt32(((decimal)salesorderitem["discountpercentage"])).ToString();
                if (salesorderitem.Contains("extendedamount")) weborderitems[i].total = ((Money)salesorderitem["extendedamount"]).Value.ToString();
                if (salesorderitem.Contains("extendedamount")) weborderitems[i].goods = ((Money)salesorderitem["extendedamount"]).Value.ToString();

                WebSalesOrderItemsWebSalesOrderItemTexts[] webordertext = new WebSalesOrderItemsWebSalesOrderItemTexts[2];


                /*
                 *  LV's XML parser must cancel looping through web order item texts when one doesn't contain a value, hence putting the declaration of new web order item texts &
                 *  its index and value inside the if statement. Should speed up XML parsing on LV's side - Michael
                 *  
                 */

                if (salesorderitem.Contains("sb_besjobref")) webordertext[0] = new WebSalesOrderItemsWebSalesOrderItemTexts(); webordertext[0].itemtext_index = "3"; webordertext[0].itemtext_value = salesorderitem["sb_besjobref"].ToString();
                     
                if (salesorderitem.Contains("sb_besprodref")) webordertext[1] = new WebSalesOrderItemsWebSalesOrderItemTexts(); webordertext[1].itemtext_index = "4"; webordertext[1].itemtext_value = "" + salesorderitem["sb_besprodref"].ToString();

                weborderitems[i].itemtexts = webordertext;
               

                //units
                WebSalesOrderItemsWebSalesOrderItemUnit[] units = new WebSalesOrderItemsWebSalesOrderItemUnit[1];
                units[0] = new WebSalesOrderItemsWebSalesOrderItemUnit();
                weborderitems[i].unit = units;
                i++;

            }

            websalesorder.items = weborderitems;
            WebSalesOrder[] websalesorders = new WebSalesOrder[1] { websalesorder };

            using (StringWriter textWriter = new StringWriter())
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(websalesorders.GetType());
                serializer.Serialize(textWriter, websalesorders);
                return textWriter.ToString();
            }
        }

        private EntityCollection GetOrderItems(Guid orderid)
        {
            string fetchXML = "<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'><entity name='salesorderdetail'><filter type='and'><condition attribute='salesorderid' operator='eq' value='{0}' /></filter></entity></fetch>";
            fetchXML = string.Format(fetchXML, orderid.ToString());

            return organizationService.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(fetchXML));
        }
    }
}


*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Double XML Creation

    Hi

    Does anyone have any ideas why this would be duplicating the xml on running the plugin

    Many Thanks

    Dan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/forums/thread/details/?threadid=fd3020f1-24b2-ef11-b8e8-00224828633c&tabName=tabPublished/_api/mspwrplt_cmty_fileattachments(053120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(073120f1-24b2-ef11-b8e8-00224828StartDocument:/_api/mspwrplt_cmty_fileattachments(073120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(063120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(053120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(073120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(053120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value/_api/mspwrplt_cmty_fileattachments(063120f1-24b2-ef11-b8e8-00224828633c)/mspwrplt_cmty_attachment/$value" - Not enough )'s.