HUD: Get the next line in a section

Discussions related to custom development with Select.
Post Reply
BobRichards
Posts: 982
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

HUD: Get the next line in a section

Post by BobRichards » Sun May 24, 2020 5:24 pm

If you want to add a line to a HUD-1 settlement, it takes two steps - if you can't find a free line among the existing ones, you need to create one. This is the procedure to follow. Also, be aware that certain sections only allow certain number of lines. If you exceed the limit, an exception will be thrown. This example can be used on sections: 100, 200, 400, 500, 700, 800, 900, 1000, 1100, 1200, 1300, and 1500.

Code: Select all

IOrder order = ...

// Get the first HUD.
IList HUDs = (IList)order.GetProperty("HUDs");
IOrderItem hud = (IOrderItem)HUDs[0];

// Get next line for the section 1100.
int sectionNumBase = 1100;
IOrderItem nextLine = GetNextAvailableHudSectionLine(hud, sectionNumBase);

// Make sure we got a line since there is a maximum number the section can hold.
if (nextLine == null)
{
	// We can't add the line. Do something else!
	return;
}

//Set description on first charge.Line will inherit the description if only one charge.
IList charges = (IList)nextLine.GetProperty("Charges");
IOrderItem firstCharge = (IOrderItem)charges[0];
firstCharge.SetProperty("Description", "Our charge");

...

// Save the order.
os.ApplyChanges(order);
os.CloseOrder(order);
Helper routine to find a free line. And if it can't find one, it will create one as long as the maximum number of lines per section is not exceeded. You need to adjust the maximum number based on the section!

Code: Select all

 /// <summary>
/// Search the current section lines and return the first one without a line description
/// and with a line amount of $0. If can't find one, return a new one.
/// </summary>
/// <param name="hud">The HUD object</param>
/// <param name="section">Name of section to get line from</param>
/// <returns></returns>
private IOrderItem GetNextAvailableHudSectionLine(IOrderItem hud, int sectionBaseNumber)
{
    // Get the section.
    IDictionary lines = (IDictionary)hud.GetProperty($"Section{sectionBaseNumber}");

    // Walk the current lines to find an empty one.
    foreach (IOrderItem item in lines.Values)
    {
        if (String.IsNullOrWhiteSpace((string)item.GetProperty("Description"))
            && (decimal)item.GetProperty("Amount") <= 0m)
        {
            // No description and amount is zero. Pick this one.
            return item;
        }
    }

    // No free lines. Make sure we are allowed to create a new one.
    //  Change the max count to the one needed for your section!
    if (lines.Count > 99)
    {
        // Nope. Return null as flag to caller.
        return null;
    }

    // We can safely add a new line to the section. We need to calculate the object name for HUD.
    IOrderItem newLine = (IOrderItem)hud.Order.CreateNew($"HUD{sectionBaseNumber}CommonLine");
    IList sectionLines = (IList)hud.GetProperty($"Section{sectionBaseNumber}");
    sectionLines.Add(newLine);

    return newLine;
}
Bob Richards, Software Developer, SoftPro

Post Reply