GeorgeM
GeorgeM

Reputation: 81

Set default value of a field in the SOLine grid

I created a custom field as a combobox on the Stock Item page, InventoryItem.UsrLeadTime, as follows:

[PXDBString(10)]
[PXUIField(DisplayName="Lead Time")]
[PXStringList(new string[] { "4", "7", "10" },

  new string[] { "4", "7", "10" },

  MultiSelect = false, ExclusiveValues = false)]

I also created a custom field on the SO Order Entry SOLine grid as a combox, SOLine.UsrLeadTimeSOLine, as follows:

[PXDBString(10)]
[PXUIField(DisplayName="Lead Time")]
[PXStringList(new string[] { "4", "7", "10" },

  new string[] { "4", "7", "10" },

  MultiSelect = false, ExclusiveValues = false)]

I am trying to set the default value of the UsrLeadTimeSOLine on the SOLine grid to be the value specified by UsrLeadTime from Stock Items based on the InventoryID selected when entering a new line on a Sales Order. My code on the SOOrderEntry graph extension is as follows:

    protected virtual void SOLine_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
   {
       SOLine row = (SOLine)e.Row;

        using (new PXConnectionScope())
            {
            InventoryItem invleadTime = PXSelect<InventoryItem,
            Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>
            .Select(Base, row.InventoryID);


            if (invleadTime != null)
                  {
                    PX.Objects.IN.InventoryItemExt ext = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(invleadTime);
                    sender.SetValue<SOLineExt.usrLeadTimeSOLine>             (row,ext.UsrLeadTime);
                   Base.Transactions.Update(row);
                    //SOLineExt rowExt = sender.GetExtension<SOLineExt>(row);
                    //rowExt.UsrLeadTimeSOLine = ext.UsrLeadTime;
                    //Base.Transactions.Update(row);
                   }
            else
             {
                 throw new PXException("invleadTime is null");
               }

              }

      }

I tossed in the else throw exception as an after thought to insure my query was returning a result. Without the else the code compiles correctly and the Sales Order page runs correctly, but the lead time value in the SOLine grid, SOLine.UsrLeadTimeSOLine, remains blank when you add a new line and choose an inventoryID even though the InventoryID has a value for lead time, InventoryItem.UsrLeadTime, on the Stock Items page.

After I added the else throw exception, when you add a new Sales Order Line, the exception appears immediately and declares invleadTime to be null as expected. Selecting an InventoryID causes the Lead Time value of the SOLine grid, SOLine.UsrLeadTimeSOLine, to be set equal to the Lead Time value from the Stock Items page, InventoryItem.UsrLeadTime. In other words, it works as expected.

Also, during Save it creates a new blank line and throws an error about a required field being blank. Delete the blank line and it saves fine. I am not much interested in the behavior of the page with the throw exception included other than the fact that the code seems to work in so far as setting my value as I wish when the throw exception is included.

Can anyone suggest a modification to my code without the else throw that will cause it to correctly set the value of SOLine.UsrLeadTimeSOLine to be equal to InventoryItem.UsrLeadTime. Thanks

Upvotes: 2

Views: 1932

Answers (3)

Hugues Beaus&#233;jour
Hugues Beaus&#233;jour

Reputation: 8278

I would suggest using the FieldDefaulting event to set a default value. Trying to do so in other events can lead to unexpected side effects.

protected virtual void SOLine_UsrLeadTimeSOLine_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
    e.NewValue = yourDefaultValue;
    e.Cancel = true;
}

Upvotes: 1

Brendan
Brendan

Reputation: 5613

You should just need to add these attributes to your soline usr field...

[PXDefault(typeof(Search<InventoryExt.UsrLeadTime , 
            Where<InventoryItem.inventoryID, Equal<Current<SOLine.inventoryID>>>>), 
            PersistingCheck = PXPersistingCheck.Nothing)]
[PXFormula(typeof(Default<SOLine.inventoryID>))]

By adding the PXFormula using Default... on the change of sales line inventory ID it will re-fire your default. This makes it easier to control the default from the DAC without needing any extra lines in your graph.

Upvotes: 1

Jeff Williams
Jeff Williams

Reputation: 1026

If you do not have a default value on lead time and/or do not want the field to be required I would use the "FieldUpdated" event of inventoryID to do this.

Something like below will work as well:

    protected void SOLine_InventoryID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
    {

      var row = (SOLine)e.Row;
      if (row != null && row.InventoryID !=null)
      {
           InventoryItem item = PXSelect<InventoryItem,Where<InventoryItem.inventoryID,Equal<Required<InventoryItem.inventoryID>>>>.Select(Base,row.InventoryID);
           InventoryItemExt itemext = item.GetExtension<InventoryItemExt>();
           if (itemext != null && itemext.UsrLeadTime != null)
           {
                cache.SetValueExt<SOLineExt.usrLeadTimeSOLine>(e.Row, itemext.UsrLeadTime);
           }
      }
    }

This will also allow the lead time value to be reset (as is) if you change the inventory ID

Upvotes: 2

Related Questions