Setting tags using IOrderItem interface

Discussions related to custom development with Select.
Post Reply
chris.brady
Posts: 105
Joined: Wed Oct 17, 2012 4:20 pm

Setting tags using IOrderItem interface

Post by chris.brady »

Are there any restrictions to adding tags to IOrderItem objects?

I'm running into an issue with adding a tag on an item of type "AdditionalTax".

Code: Select all

IEnumerable<dynamic> SpTaxes = DOrder.Properties[0].Taxes;
dynamic ac = Order.CreateNew("AdditionalTax");
ac.Description = "A Tax";
((IOrderItem)ac).SetTag("TaxTag", "AdditionalTax");
((IList)SpTaxes).Add(ac);
Setting this tag appears to work fine. I'm also able to retrieve the tag fine with GetTag().
The problem comes when I try to delete the additional tax. If I try removing it through the API:

Code: Select all

((IList)SpTaxes).Remove(theTax);
I get a "Cannot perform runtime binding on a null reference" error. The same error happens when deleting the tax through the UI. The tax does get removed, but not without throwing this error.

Taxes without manually added tags can be deleted just fine, both UI and API.

Am I doing something wrong? Note that I've tried saving the order after adding the tag, but it still throws this error when removing.
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Setting tags using IOrderItem interface

Post by BobRichards »

  • I'm not sure what the problem was since you didn't include what "theTax" is. However, tags have nothing to do with it.
  • In general, whenever you add an object to a collection, you should "new" the item then immediately add it to the collection BEFORE modifying any of its properties. Usually it doesn't matter but if an internal order rule requires knowledge of a parent or some other order property, it cannot reliably get them until they are added to the collection.

    Code: Select all

    // Get first property's taxes collection.
    IList properties = (IList)order.GetProperty("Properties");
    IOrderItem property = (IOrderItem)properties[0];
    IList taxes = (IList)property.GetProperty("Taxes");
    
    // Create new tax and add it to taxes collection.
    IOrderItem pt = order.CreateNew("AdditionalTax");
    taxes.Add(pt);
    
    // Set new tax's properties.
    pt.SetProperty("Description", "A tax: " + DateTime.Now.ToShortTimeString());
    pt.SetTag("TaxTag", "AdditionalTax");
    
    // Save all order changes.
    os.ApplyChanges(order);
  • As so far as removing the Additional Tax, you have to pass the object you want to drop to the Remove method. Your are not allow to simply pass an integer index for the item to remove.

    Code: Select all

    // Find the item in the taxes collection.
    IOrderItem item = null;
    foreach (IOrderItem tax in taxes)
    {
        if ((string)tax.GetProperty("Description") == "Sally")
        {
            item = tax;
            break;
        }
    }
    
    // If we found it then remove it.
    if (item != null)
    {
        // Remove it.
        taxes.Remove(item);
    
        // Save all order changes.
        os.ApplyChanges(order);
    }
Bob Richards, Senior Software Developer, SoftPro
Post Reply