web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error in plugin to create xml

(0) ShareShare
ReportReport
Posted on by

Hi all

I have a bit of issue with the below code


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];

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

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

}

It's with the section in red i feel i have missed something. if the second in red isn't there the plugin works fine if the red section is there the plugin doesn't work any ideas what is missing

Kind Regards

Dan 

*This post is locked for comments

I have the same question (0)
  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Are you getting any error message or the plugin doesn't trigger at all? Or something else is happing? Also, what is WebSalesOrderItemsWebSalesOrderItem?

  • Community Member Profile Picture
    on at

    Hi 

    There is no error and it seems to run fine but does not push the data to the third party portal we have. Oddly tho if i run the plugin using the section below of the code it works so i think it's something to do with the multiple references.  

    WebSalesOrderItemsWebSalesOrderItemTexts[] webordertext = new WebSalesOrderItemsWebSalesOrderItemTexts[1];

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

  • Community Member Profile Picture
    on at

    Hi Dan,

    It is difficult to understand which line of code is trying to push data to the 3rd party portal. The only line in red that seems to have an impact should be :

    weborderitems[i].itemtexts = webordertext;

    When you suggest the plugin works fine without the code in red, what is the difference when the code in red is included? Are you not receiving any errors at all?

    Lastly, what type of debugging are you using?

  • Verified answer
    Daniel Wikell Profile Picture
    2,360 on at

    Hi

    In this line:

    "if (salesorderitem.Contains("sb_besjobref")) webordertext[0].itemtext_value = salesorderitem["sb_besjobref"].ToString();"

    you currently check if "sb_besjobref" is a property that is available. However it looks like you do not check if the actual value of this property is null. When you invoke the ToString method, it will fail if sb_besjobref is null.

    Since your code example does not include how you retrieved the salesorderitem objects I can't say for sure if you used a retrieve-method which automatically excludes the properties when they are null or if you used a method that retrieves all properties even when null. If you used the latter then the missing null-check may indeed be the problem.

  • Community Member Profile Picture
    on at

    This was the issue if the value was null it kicked it out the code now is

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

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

    Many Thanks for your help

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans