How to get DAC record from Note Table - acumatica

Does anyone have a code snippet on how to go from RefNoteId => DAC when I dont know what dac type the note is attached to?
I have made it this far (row.RefNoteID is what I am starting from)
Note note = PXSelect<Note, Where<Note.noteID, Equal<Required<Note.noteID>>>>.Select(this, row.RefNoteID);
Type recordType = Type.GetType(note.EntityType);
PXCache recordCache = Caches[recordType];
How can I now do a PXSelect<recordType, Where<recodType.noteID, Equal<Required<recordType.noteID>>>>.Select(GRAPH) ? The recordType could be any DAC in the system that has a noteID.
Thanks

The below code works for me and it is based on the way Acumatica gets the record inside the PXRefNoteSelectorAttribute.PrimaryRow_RowPersisted.
The problem with this approach is that this will work for Header entities like SOOrder, INRegister, SOInvoice, SOShipment, and others. But for "detail" entities like SOLine, INTran, and others this approach will work only if that corresponding record has some Note related to Text/File. Acumatica is adding records corresponding to their NoteID into the Note table only if that detail records have some Note/Text. My best guess is that this is done in order to avoid over-spamming the Note table.
using PX.Data;
using PX.Objects.SO;
using System;
using System.Collections;
using System.Linq;
using System.Web.Compilation;
namespace SearchByNoteID
{
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
public PXAction<SOOrder> searchByNoteID;
[PXUIField(DisplayName ="Search by Note ID")]
[PXButton(CommitChanges = true)]
public virtual IEnumerable SearchByNoteID(PXAdapter adapter)
{
var order = adapter.Get<SOOrder>().FirstOrDefault();
if(order!=null)
{
//
//...
//
Guid? noteID = GetNoteID();
object record = GetRecordByNoteID(noteID);
//
//... do whatever you want with the record
//
}
return adapter.Get();
}
protected object GetRecordByNoteID(Guid? noteID)
{
var type = GetEntityType(this.Base, noteID);
if(type==null) return null;
object entityRow = new EntityHelper(this.Base).GetEntityRow(type, noteID);
return entityRow;
}
protected Type GetEntityType(PXGraph graph, Guid? noteID)
{
if (noteID == null)
{
return null;
}
Note note = PXSelectBase<Note, PXSelect<Note, Where<Note.noteID, Equal<Required<Note.noteID>>>>.Config>.SelectWindowed(graph, 0, 1, new object[]
{
noteID
});
if (note == null || string.IsNullOrEmpty(note.EntityType))
{
return null;
}
return PXBuildManager.GetType(note.EntityType, false);
}
}
}

Related

How to get Selector Substitute Key / Description values

Lets say I have a DAC record, like SOOrder, and I have a field like customerID, where there is a PXSelectorAttribute defined on an integer field, that has a SubstitueKey = typeof(Customer.acctCD) and Description = typeof(Customer.acctName). Is there some way that I can get the values of the substitute key / description field for that record without doing a PXSelect against the selectors table?
Thanks
-Kyle
Note since no version was specified my example was coded against 2020r2
The following is an example that updates an Invoice/Memo's Description to the Location's CD and Descr fields [CD: Descr] when the location is changed on the Invoice/Memo. I believe the function you're looking for is PXSelectorAttribute.Select<SELECTOR_FIELD>(SELECTOR_FIELD_CACHE, SELECTOR_FIELD_RECORD) AS SELECTOR_TARGET_DAC.
public class ArInvoiceEntrySoExt : PXGraphExtension<ARInvoiceEntry>
{
#region Event Handlers
#region ArInvoice
public virtual void _(Events.FieldUpdated<ARInvoice.customerLocationID> e, PXFieldUpdated del)
{
var inv = e.Row as ARInvoice;
del?.Invoke(e.Cache, e.Args);
if (inv != default)
{
var loc = PXSelectorAttribute.Select<ARInvoice.customerLocationID>(e.Cache, inv) as Location;
e.Cache.SetValueExt<ARInvoice.docDesc>(inv, string.Format("{0}: {1}", loc?.LocationCD, loc?.Descr));
}
}
#endregion
#endregion
}
An alternative, you can use PXSelectorAttribute.GetField(SELECTOR_FIELD_CACHE, SELECTOR_FIELD_RECORD, "SELECTOR_FIELD_NAME", SELECTOR_FIELD_VALUE, "SELECTOR_TARGET_FIELD_NAME") to get a specific field from the selector's target record. As an example, the following code does the same thing as above using this alternative method:
public class ArInvoiceEntrySoExt : PXGraphExtension<ARInvoiceEntry>
{
#region Event Handlers
#region ArInvoice
public virtual void _(Events.FieldUpdated<ARInvoice.customerLocationID> e, PXFieldUpdated del)
{
var inv = e.Row as ARInvoice;
del?.Invoke(e.Cache, e.Args);
if (inv != default)
{
var loc = PXSelectorAttribute.Select<ARInvoice.customerLocationID>(e.Cache, inv) as Location;
e.Cache.SetValueExt<ARInvoice.docDesc>(inv, string.Format("{0}: {1}",
PXSelectorAttribute.GetField(e.Cache, inv, "customerLocationID", inv.CustomerLocationID, "LocationCD"),
PXSelectorAttribute.GetField(e.Cache, inv, "customerLocationID", inv.CustomerLocationID, "Descr")));
}
}
#endregion
#endregion
}
I also was told about another method that can be used
PXFieldState.UnwrapValue(object value) will return the UI equivalent of a value in the back end. This is nice because then you can use one method for both drop downs and selectors, but I cant seem to see how to get the description field using this

How can I restrict Site ID (Whse) selection to only the current branch?

While it is easy in thought to just limit records to INSite.branchID = AccessInfo.branchID, the need is a bit more complex. I thought that I found a simple solution when looking at the DAC for INTran to find:
#region SiteID
public abstract class siteID : PX.Data.BQL.BqlInt.Field<siteID> { }
protected Int32? _SiteID;
[IN.SiteAvail(typeof(INTran.inventoryID), typeof(INTran.subItemID))]
[PXDefault(typeof(INRegister.siteID))]
[PXForeignReference(typeof(FK.Site))]
[InterBranchRestrictor(typeof(Where<SameOrganizationBranch<INSite.branchID, Current<INRegister.branchID>>>))]
public virtual Int32? SiteID
{
get
{
return this._SiteID;
}
set
{
this._SiteID = value;
}
}
#endregion
which has an intriguing attribute for InterBranchRestrictor. After a little digging, I found this attribute actually is used rather widely in Acumatica, but it appears to be limited to only Report Graphs and enabling the feature for Inter-Branch Transactions. Easy enough, I enabled the feature and tried an Inventory Issue again. No luck. I still could select a site id for a different branch.
So far, I only have limited control by creating a graph extension on INIssueEntry to set and validate the site ID. But what I really want is to limit the selector to only site id's of the current branch.
protected void _(Events.FieldDefaulting<INTran.siteID> e)
{
PXResultset<INSite> Results = PXSelect<INSite, Where<INSite.branchID, Equal<Current<AccessInfo.branchID>>>>.Select(Base);
if (Results.Count == 1)
{
foreach (PXResult<INSite> result in Results)
{
INSite site = result;
e.NewValue = site.SiteID;
e.Cancel = true;
}
}
}
protected void _(Events.FieldVerifying<INTran.siteID> e)
{
int? siteID = (int?)e.NewValue;
INTran row = (INTran)e.Row;
INSite site = PXSelect<INSite, Where<INSite.siteID, Equal<Required<INSite.siteID>>,
And<INSite.branchID, Equal<Current<AccessInfo.branchID>>>>>.Select(Base, siteID);
if(siteID != null && site?.SiteID == null)
{
PXUIFieldAttribute.SetError<INTran.siteID>(e.Cache, row, "Invalid Warehouse for Branch");
}
}
I really want to leverage what it seems like
[InterBranchRestrictor(typeof(Where<SameOrganizationBranch<INSite.branchID, Current<INRegister.branchID>>>))]
does, but clearly I just don't understand what it does. Alternatively, if I could add a where clause to
[IN.SiteAvail(typeof(INTran.inventoryID), typeof(INTran.subItemID))]
then I could restrict the list to the current branch that way, but I'm struggling to make that work as well. (Problems around implementing the PXForeignReference attribute in the extension needed to override the field definition.)
How can I restrict (in a manner that can be replicated efficiently throughout Acumatica) branch specific records to only site ID's of the current branch?
The InterBranchRestrictorAttribute is checking the graph to be working from a Report or the Inter-Branch Transactions feature to be turned on in the IsReportOrInterBranchFeatureEnabled method, so you need to remove this from your implementation.
You can write your own PXResrictorAttribute in the way shown in the example below:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = true)]
public class CustomRestrictorAttribute: PXRestrictorAttribute
{
public CustomRestrictorAttribute(Type where) : base(CustomRestrictorAttribute.EmptyWhere, "Restrictor Message.", Array.Empty<Type>())
{
this._interBranchWhere = where;
}
protected override BqlCommand WhereAnd(PXCache sender, PXSelectorAttribute selattr, Type Where)
{
return base.WhereAnd(sender, selattr, this._interBranchWhere);
}
private static readonly Type EmptyWhere = typeof(Where<True, Equal<True>>);
protected Type _interBranchWhere;
}
And apply it to the DAC field like below:
[SiteAvail(typeof(SOLine.inventoryID), typeof(SOLine.subItemID))]
[PXParent(typeof(Select<SOOrderSite, Where<SOOrderSite.orderType, Equal<Current<SOLine.orderType>>, And<SOOrderSite.orderNbr, Equal<Current<SOLine.orderNbr>>, And<SOOrderSite.siteID, Equal<Current2<SOLine.siteID>>>>>>), LeaveChildren = true, ParentCreate = true)]
[PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
[PXUIRequired(typeof(IIf<Where<SOLine.lineType, NotEqual<SOLineType.miscCharge>>, True, False>))]
[InterBranchRestrictor(typeof(Where2<SameOrganizationBranch<INSite.branchID, Current<SOOrder.branchID>>,
Or<Current<SOOrder.behavior>, Equal<SOBehavior.qT>>>))]
[CustomInterBranchRestrictor(typeof(Where<INSite.branchID,Equal<Current<SOOrder.branchID>>>))]
protected virtual void SOLine_SiteID_CacheAttached(PXCache cache)
{
}

Button not showing in the UI

I'm extending the AccountByPeriodEnq logic, I just want to add a button on top of my screen to modify the selected GL records but it just doesnt want to show up and I can't figure out why.
Here's my code :
namespace PX.Objects.GL
{
class AccountByPeriodEnqExtensions : PXGraphExtension<AccountByPeriodEnq>
{
#region Actions
public PXAction<AccountByPeriodFilter> Letter;
[PXUIField(Visible = true, DisplayName = "Lettrer")]
[PXButton(CommitChanges = true)]
protected virtual IEnumerable letter(PXAdapter adapter)
{
IReadOnlyCollection<GLTranR> selectedTrans = GetSelectedTrans();
if (selectedTrans.Any())
{
PXLongOperation.StartOperation(this, delegate ()
{
foreach(GLTranR line in selectedTrans)
{
// UpdateSomeFieldsAndPersists
}
});
}
else
{
throw new PXException("Error");
}
return Base.Filter.Select();
}
#endregion
#region Utility
private IReadOnlyCollection<GLTranR> GetSelectedTrans()
{
return Base.GLTranEnq.Cache.Updated
.Cast<GLTranR>()
.Where(tran => tran.Selected == true)
.ToArray();
}
#endregion
}
}
Is there anything I'm missing here ?
Regards,
Edit:
To clarify i'm trying to customize the GL404000, Account Details. And using the inspector I saw the Business logic is in the AccountByPeriodEnq Graph
Using Acumatica Inspect Element feature notice that the 'Account by Period' screen (GL402000) is not using the 'AccountByPeriodEnq' graph.
It is using the 'AccountHistoryByYearEnq' graph instead so that's the graph you want to target:
You also need to declare Actions on the primary DAC of that Graph.
The one for 'AccountHistoryByYearEnq' is a little bit harder to find than usual.
You can use Acumatica Source Code page and search for 'PXPrimaryGraph(typeof(AccountHistoryByYearEnq)':
In this case it is AccountByYearFilter DAC that you should use:
[System.SerializableAttribute()]
[PXCacheName(Messages.Account)]
[PXPrimaryGraph(typeof(AccountHistoryByYearEnq), Filter = typeof(AccountByYearFilter))]
public partial class Account : PX.Data.IBqlTable, PX.SM.IIncludable
{
[…]
}
I think this is a special case for Filter, when there's no filter Account would have been the DAC to use for the Actions.
Now that you have identified the Primary Graph of the Screen (AccountHistoryByYearEnq) and the Primary DAC of the Graph (AccountByYearFilter) it should work as expected:
public class AccountByPeriodEnq_Extension : PXGraphExtension<AccountHistoryByYearEnq>
{
public PXAction<AccountByYearFilter> letter;
[PXUIField(DisplayName = "Letter")]
[PXButton]
protected virtual IEnumerable Letter(PXAdapter adapter)
{
return adapter.Get();
}
}
UI:
EDIT:
For Account detail page (GL404000), use the same code with different DAC and Graph:
using PX.Data;
using System.Collections;
namespace PX.Objects.GL
{
public class AccountByPeriodEnq_Extension : PXGraphExtension<AccountByPeriodEnq>
{
#region Actions
public PXAction<AccountByPeriodFilter> letter;
[PXUIField(DisplayName = "Letter")]
[PXButton]
protected virtual IEnumerable Letter(PXAdapter adapter)
{
return adapter.Get();
}
#endregion
}
}
With a base Acumatica install that's all that is needed for the Action to appear:
Note that you can specify explicit state and view rights for the button control, though I don't think your issue is related to access rights if you're working in a developer instance:
[PXUIField(DisplayName = "Letter",
MapEnableRights = PXCacheRights.Select,
MapViewRights = PXCacheRights.Select)]
I finally found my problem, I missed the public member for my class.
namespace PX.Objects.GL
{
public class AccountByPeriodEnqExtensions : PXGraphExtension<AccountByPeriodEnq>
{
#region Actions
public PXAction<AccountByPeriodFilter> Letter;
Thanks for your answer HB, i'll reuse it.

Acumatica Vendor ID segment key different to BIZZACCOUNT segment length

I want to know if it's possible to setup Vendor ID segment keys with a less number of characters than the BiZAccounts..
For example I have created branch Keys like USBRANCH1 but now I want to create Vendor ID like V001 but not give the user possibility to enter more than 4 characters?
I'm not sure why BIZaccount is tied to vendors, customers, employees in terms of segment keys as this reduces flexibility and changing key length and type
Thanks
You can do the following to achieve that:
1. Create your own Segmented Key like below:
Create Cache Extension to the Vendor's DAC to change Default VENDOR Segmented Key which is inherited from BIZACCTto yours MYVENDOR like below:
using PX.Data;
using PX.Data.EP;
using PX.Objects.AP;
using PX.Objects.CR;
using PX.Objects.GL;
using System;
namespace ClassLibrary1.DAC
{
public class VendorExt:PXCacheExtension<Vendor>
{
[MyVendorRaw(IsKey = true)]
[PXDefault]
[PXFieldDescription]
public virtual string AcctCD { get; set; }
}
[PXDBString(30, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Vendor", Visibility = PXUIVisibility.Visible)]
public sealed class MyVendorRawAttribute : AcctSubAttribute
{
public MyVendorRawAttribute() : this(null)
{
}
public MyVendorRawAttribute(Type where)
{
Type type = BqlCommand.Compose(new Type[]
{
typeof(Search2<, , >),
typeof(Vendor.acctCD),
typeof(LeftJoin<Contact, On<Contact.bAccountID, Equal<Vendor.bAccountID>, And<Contact.contactID, Equal<Vendor.defContactID>>>, LeftJoin<Address, On<Address.bAccountID, Equal<Vendor.bAccountID>, And<Address.addressID, Equal<Vendor.defAddressID>>>>>),
(where == null) ? typeof(Where<Match<Current<AccessInfo.userName>>>) : BqlCommand.Compose(new Type[]
{
typeof(Where2<, >),
typeof(Where<Match<Current<AccessInfo.userName>>>),
typeof(And<>),
where
})
});
PXDimensionSelectorAttribute pxdimensionSelectorAttribute;
this._Attributes.Add(pxdimensionSelectorAttribute = new PXDimensionSelectorAttribute("MYVENDOR", type, typeof(Vendor.acctCD), new Type[]
{
typeof(Vendor.acctCD),
typeof(Vendor.acctName),
typeof(Vendor.vendorClassID),
typeof(Vendor.status),
typeof(Contact.phone1),
typeof(Address.city),
typeof(Address.countryID)
}));
pxdimensionSelectorAttribute.DescriptionField = typeof(Vendor.acctName);
this._SelAttrIndex = this._Attributes.Count - 1;
this.Filterable = true;
((PXDimensionSelectorAttribute)this._Attributes[this._SelAttrIndex]).CacheGlobal = true;
}
public const string DimensionName = "MYVENDOR";
}
}
After this Vendor CD will work with 4 length. But as you can see on the screenshot below there will be some issues with the existing records. So I recommend you to do this only in case if the system has not existing Vendors.
Also after doing this I assume you to check all the pages which use Vendor for correct working.

Acumatica Custom Selector SQL Error - Incorrect Syntax near the keyword

I am trying to create a custom Parts selector to search all records if there is no vendor selected, if a vendor is selected I want to filter the selector's results to only include Parts with the vendor selected.
Using PXCustomSelectorAttribute:
SQL Error: Incorrect syntax near the keyword 'AND'
Incorrect syntax near the keyword 'ORDER'
[PXNonInstantiatedExtension]
public class SO_SOLine_ExistingColumn : PXCacheExtension<PX.Objects.SO.SOLine>
{
#region InventoryID
[PXMergeAttributes(Method = MergeMethod.Replace)] //was append
[PXUIField(DisplayName = "Part #")]
[PartSelector(typeof(SOLineExt.usrCusVendor))]
public int? InventoryID { get; set; }
#endregion
}
public class PartSelector : PXCustomSelectorAttribute
{
[Serializable]
[PXProjection(typeof(
Select2<atcVendorItem,
LeftJoin<InventoryItem,
On<InventoryItem.inventoryCD, Equal<atcVendorItem.inventoryCD>, And<InventoryItem.inventoryID, Equal<atcVendorItem.inventoryID>>>>>), Persistent = false)]
public class atcPartView : IBqlTable
{
// DAC W/Inventory Item Table mapped and joined with Vendor Items
}
//Selected table
private Type _MfgField;
////way to have multiple description fields
public PartSelector(Type MfgField) : base(typeof(atcPartView.inventoryID))
{
_MfgField = MfgField;
}
protected virtual IEnumerable GetRecords()
{
var cache = this._Graph.Caches[BqlCommand.GetItemType(_MfgField)];
var cbs = (BAccount)cache.Current;
// make mfgfield
if (cbs != null)
{
foreach (atcPartView p in PXSelect<atcPartView, Where<atcPartView.vendorID, Equal<Required<SOLineExt.usrMfg>>>>.Select(_Graph, cbs.BAccountID))
{
yield return p;
}
}
else
{
foreach (atcPartView p in PXSelect<atcPartView, Where<atcPartView.vendorID, IsNotNull>>.Select(_Graph))
{
yield return p;
}
}
}
}
I was able to find another exception while debugging in visual studio,
my issue was because I was trying to cast the field as BAccount when referencing the SOLine Extension. Changing the cast fixed my issue

Resources