Getting Combo Box Values Set For Rest Api

 

Hello everybody,

today I want to share idea on how to get getting Combo-box values set for REST API.

As usually values of comboboxes values are just hardcoded in web api calls, but sometimes it may be necessary to load them from Rest API, for example for cases if you want to target multiplve versions of Acumatica. For such a purpose I'd suggest to create Graph, which via reflection will read values from dlls. Quite similar to what Acumatica team does. 

Below goes source code of graph, which via reflection loads data:

using PX.Data;
using System;
using System.Collections;
using System.Reflection;
 
namespace LA
{
    public class ListAttributesInq : PXGraph<ListAttributesInq>
    {
        public PXCancel<ClassFilter> Cancel;
 
        public PXFilter<ClassFilter> Filter;
 
        public override bool IsDirty
        {
            get
            {
                return false;
            }
        }
 
        public ListAttributesInq()
        {
            Records.Cache.AllowInsert = false;
            Records.Cache.AllowDelete = false;
            Records.Cache.AllowUpdate = false;
 
            Actions["Process"].SetVisible(false);
            Actions["ProcessAll"].SetVisible(false);
            Actions["Schedule"].SetVisible(false);
        }
 
        [PXFilterable]
        [PXVirtualDAC]
        public PXFilteredProcessing<KeyValueRecordClassFilter> Records;
 
        protected IEnumerable records()
        {
            //var row = new ClassFilter { ClassName = "PX.Objects.CR.CRMSourcesAttribute" };
            var row = Filter.Current;
            if (row != null && !string.IsNullOrWhiteSpace(row.ClassName))
            {
                var type = Type.GetType(row.ClassName) ??
                            Type.GetType(row.ClassName + ", PX.Objects");
 
                if (type != null)
                    switch (type.BaseType.Name)
                    {
                        case "PXIntListAttribute":
                            {
                                int[] values;
                                string[] labels;
                                GetRecords(typeout valuesout labels);
 
                                for (int i = 0; i < values.Length; i++)
                                    yield return new KeyValueRecord { Key = values[i].ToString(), UiValue = labels[i] };
                                break;
                            }
                        case "PXStringListAttribute":
                            {
                                string[] valueslabels;
                                GetRecords(typeout valuesout labels);
 
                                for (int i = 0; i < values.Length; i++)
                                    yield return new KeyValueRecord { Key = values[i], UiValue = labels[i] };
                                break;
                            }
                    }
            }
        }
 
        private void GetRecords<T>(Type typeout T[] valuesout string[] labels)
        {
            var obj = Activator.CreateInstance(type);
            var flags = BindingFlags.NonPublic | BindingFlags.Instance;
 
            values = type.GetField("_AllowedValues"flags).GetValue(objas T[];
            labels = type.GetField("_AllowedLabels"flags).GetValue(objas string[];
        }
    }
}

 

It has few features:

  1. Method get records, which creates instance, and fills values
  2. Method records, which is delegate overload, and which distinguishes between PXIntListAttribute and PXStringListAttribute.
  3. yield return purpose of it is to be pageble ( for page opening it doesn't have big value ) and getting all records at once
  4. As entry, it is needed to enter class with it's namespace. Like PX.Objects.CR.CRMSourcesAttribute, and not like CRMSources

Aspx source code looks like this:

<%@ Page Language="C#" MasterPageFile="~/MasterPages/FormDetail.master" AutoEventWireup="true" ValidateRequest="false" CodeFile="LA401000.aspx.cs" Inherits="Page_LA401000" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPages/FormDetail.master" %>
 
<asp:Content ID="cont1" ContentPlaceHolderID="phDS" Runat="Server">
    <px:PXDataSource ID="ds" runat="server" Visible="True" Width="100%"
        TypeName="LA.ListAttributesInq"
        PrimaryView="Filter">
		<CallbackCommands></CallbackCommands>
	</px:PXDataSource>
</asp:Content>
<asp:Content ID="cont2" ContentPlaceHolderID="phF" Runat="Server">
    <px:PXFormView SkinID="" ID="form" runat="server" DataSourceID="ds" DataMember="Filter" Width="100%" Height="" AllowAutoHide="false">
		<Template>
			<px:PXLayoutRule ID="PXLayoutRule1" runat="server" StartRow="True"></px:PXLayoutRule>
			<px:PXTextEdit CommitChanges="True" runat="server" ID="CstPXTextEdit1" DataField="ClassName" ></px:PXTextEdit></Template>
	</px:PXFormView>
</asp:Content>
<asp:Content ID="cont3" ContentPlaceHolderID="phG" Runat="Server">
    <px:PXGrid AllowFilter="True" AllowPaging="True" AllowSearch="True" SyncPosition="True" ID="grid" runat="server" DataSourceID="ds" Width="100%" Height="150px" SkinID="Inquire" AllowAutoHide="false">
		<Levels>
			<px:PXGridLevel DataMember="Records">
			    <Columns>
				<px:PXGridColumn DataField="Key" Width="120" ></px:PXGridColumn>
				<px:PXGridColumn DataField="UiValue" Width="180" ></px:PXGridColumn></Columns>
			
				<RowTemplate>
					<px:PXTextEdit runat="server" ID="CstPXTextEdit2" DataField="Key" ></px:PXTextEdit>
					<px:PXTextEdit runat="server" ID="CstPXTextEdit3" DataField="UiValue" ></px:PXTextEdit></RowTemplate></px:PXGridLevel>
		</Levels>
		<AutoSize Container="Window" Enabled="True" MinHeight="150" ></AutoSize>
		<ActionBar >
		</ActionBar>
	</px:PXGrid>
</asp:Content>

One more necessary DAC Class ClassFilter looks like this:

using PX.Data;
using System;
 
namespace LA
{
    [Serializable]
    public class ClassFilter : IBqlTable
    {
        #region ClassName
        public abstract class className : PX.Data.IBqlField
        {
        }
        [PXString(50, IsUnicode = true, InputMask = "")]
        [PXUIField(DisplayName = "Class Name")]
        public string ClassName { getset; }
        #endregion
    }
}

 

DAC class KeyValueRecord looks very trivial in this case:

using PX.Data;
using System;
 
namespace LA
{
    [Serializable]
    public class KeyValueRecord : IBqlTable
    {
        #region Key
        public abstract class key : PX.Data.IBqlField
        {
        }
        [PXString(10, IsUnicode = true, InputMask = "")]
        [PXUIField(DisplayName = "Key")]
        public string Key { getset; }
        #endregion
        #region Value
        public abstract class uiValue : PX.Data.IBqlField
        {
        }
        [PXString(50, IsUnicode = true, InputMask = "")]
        [PXUIField(DisplayName = "Value")]
        public string UiValue { getset; }
        #endregion
    }
}

And the end result looks like this:

Summary

If you need to read something from Attributes, just use reflection.

 

Create Payment Proc

 

Hello everybody,

today I want to write a few words about method CreatePaymentProc in graph SOOrderEntry.

Among different features of this method, want to describe that it have for some reason out parameter! Take a look on it's declaration:

public virtual void CreatePaymentProc(SOOrder orderout PXGraph targetstring paymentType = ARPaymentType.Payment)
{

 

as you see, it accepts as a parameter PXGraph. Inside of open part of Acumatica source code there is only one more file, which has similar staff: ServiceOrderCore. Take a look on it:

public static void CreatePrepayment(FSServiceOrder fsServiceOrderRowFSAppointment fsAppointmentRowout PXGraph targetstring paymentType = ARPaymentType.Payment)
{

But similarities not finished. Take notice how those methods utilize PXGraph target. First goes ServiceOrderCore:

ARPaymentEntry graphARPaymentEntry = PXGraph.CreateInstance<ARPaymentEntry>();
target = graphARPaymentEntry;

And second one goes SOOrderEntry:

public virtual void CreatePaymentProc(SOOrder orderout PXGraph targetstring paymentType = ARPaymentType.Payment)
{
	ARPaymentEntry docgraph = PXGraph.CreateInstance<ARPaymentEntry>();
	target = docgraph;

And now you'd probably ask, and so what? How can I use it? One of the very useful ways would be getting graph of ARPaymentEntry after calling of the method. 

In one of my projects it was needed to call method CreatePaymentProc, and persisting of payment but without showing dialog window of creation of Payment. Below goes code how I've achieved it:

 
PXLongOperation.StartOperation(Base, delegate()
{
    PXGraph target;
    Base.CreatePaymentProc(Base.Document.Current, out target);
    var arPaymentEntry = target as ARPaymentEntry;
    //here some additional manipulations may be added to in memory created payment
    arPaymentEntry.Persist();
});

Summary

If you have a need to call method CreatePaymentProc, and then massage data after execution of that method, jast ignore showing pop up window and with help of type casting and additional changes make it. Afterwards call Persist.

 

 

Acumatica Application Architecuture

 

Hello everybody,

today I want to leave a short note about Acumatica Architecture. Take a look on this picture:

As you can see from the schema business logic controllers is kind of single source of truth. Acumatica doesn't have dependencies between UI and Web services. Also it means, if you hide something from UI on the page, it will be also hidden from Mobile app.

But want to say about few exceptions as well.

Suppose following scenario. You make a graph, and you know for sure, that from UI standpoint, it's logicall to load some visualization data, but from API call there is no reason to call some piece of code. How to inform Acumatica about it?

For this purpose you can use IsContractBasedAPI method of class graph. It may look like this:

if (!Base.IsContractBasedAPI)
{
    // Do something for UI
}
else
{
    //Do Something for non UI
}

Among other interesting modes of functionality, it is worthy to mention also IsMobile ( do something only for mobile apps ), IsExport ( do something in scope of export scenario ), IsImport ( do something in scope of Import scenario ), IsCopyPasteContext ( do something in scope of copy/paste functionality )

Summary

Taking into account all of these facts, you can be almost certain that in majority of cases Acumatica will execute pieces of logic like on the UI of Acumatica instance. But it will not be 100% times. So next time, when you API call give you data different of what you see in UI, check base source code, maybe there is the issue.

 

 

Property From Cropportunity Is Not Loaded

 

Hello everybody,

today I want to leave short note on issue with CROpportunity DAC class.

For quite a few times I've noticed that someone adds field to CROpportunity, but later notices that field is lost either on moment of loading from database, or lost during persisting record to database.

Reason for such weirdness is that starting from some Acumatica version CROpportunity got following declaration:

	[System.SerializableAttribute()]
	[PXCacheName(Messages.Opportunity)]
	[PXPrimaryGraph(typeof(OpportunityMaint))]
	[CREmailContactsView(typeof(Select2<Contact,
		LeftJoin<BAccountOn<BAccount.bAccountIDEqual<Contact.bAccountID>>>,
		Where2<Where<Optional<CROpportunity.bAccountID>, IsNullAnd<Contact.contactID
Equal<Optional<CROpportunity.contactID>>>>,    Or2<Where<Optional<CROpportunity.bAccountID>, IsNotNull
And<Contact.bAccountIDEqual<Optional<CROpportunity.bAccountID>>>>, Or<Contact.contactTypeEqual<ContactTypesAttribute.employee>>>>>))] [PXEMailSource]//NOTE: for assignment map [PXProjection(typeof(Select2<Standalone.CROpportunity, InnerJoin<Standalone.CROpportunityRevisionOn<Standalone.CROpportunityRevision.noteID
Equal<Standalone.CROpportunity.defQuoteID>>, LeftJoin<Standalone.CRQuote, On<Standalone.CRQuote.quoteIDEqual<Standalone.CROpportunity.defQuoteID>>>>>), new Type[] { typeof(Standalone.CROpportunity), typeof(Standalone.CROpportunityRevision) } )]     public partial class CROpportunity : IBqlTableIAssignIPXSelectableINotable {

List of applied attributes is pretty heavy and can be confusing, but reason of such losts live in this part:

[PXProjection(typeof(Select2<Standalone.CROpportunity,
	InnerJoin<Standalone.CROpportunityRevisionOn<Standalone.CROpportunityRevision.noteIDEqual<Standalone.CROpportunity.defQuoteID>>,
	LeftJoin<Standalone.CRQuote,
		On<Standalone.CRQuote.quoteIDEqual<Standalone.CROpportunity.defQuoteID>>>>>),
	new Type[] { typeof(Standalone.CROpportunity), typeof(Standalone.CROpportunityRevision) }
	)]	

As you can see, CROpportunity has decoration of PXProjection. I have one more sample of PXProjection on my blog.

But coming back, how to deal with data loss? Answer is simple: you'll need to make extension for two DAC classes, instead of one. 

Below goes code sample of what I've did for one of my customers:

    public class CROpportunityExt : PXCacheExtension<CROpportunity>
    {
		#region UsrContractLength
		[PXDBInt]
		[PXUIField(DisplayName = "Contract Duration ")]
                [PXDefault(0)]
		public virtual int? UsrContractDuration { getset; }
		public abstract class usrContractDuration : IBqlField { }
		#endregion

and one more cache extension:

public class CROpportunityStandAloneCSEXt : PXCacheExtension<PX.Objects.CR.Standalone.CROpportunity>
{
    #region UsrContractLength
    [PXDBInt]
    [PXUIField(DisplayName = "Contract Duration")]
    [PXDefault(0)]
    public virtual int? UsrContractDuration { getset; }
    public abstract class usrContractDuration : IBqlField { }
    #endregion

Summary

As usually if field doesn't arrive from db or not to db the problem is in forgeting of DB prefix. Instead of PXDBInt quite often is typed ( or probably would say copy/pasted ) PXInt. Another common reason is override of PXView with delegate, and forgetting of including newly added column in the output, or sometime if delegate has plenty of conditional statements then not including some field into those statements list. Third reason is or could be FieldSelecting/FieldUpdating/RowUpdated behavior which can "kill" value. And fourth by commonality but not by complexity reason can be issues when you have PXProjection over DAC class. 

 

 

Lastmodifieddatetime Has Wrong Datetime Information

 

Hello everybody,

today I want to share interesting use case which raised recently when dealt with syncrhonization of records between Magento and Acumatica.

I had following declaration:

#region LastModifiedDateTime
[PXDBDateAndTime()]
[PXUIField(DisplayName = "Last Modified Date Time")]
public virtual DateTime? LastModifiedDateTime { getset; }
public abstract class lastModifiedDateTime : PX.Data.BQL.BqlDateTime.Field<lastModifiedDateTime> { }
#endregion

and for my disappointment as well as disappointment of Magento developers we had to add some hours shift to each call, in order to filter out properly by Last modified Date time.

One of the solutions was to add one more flag to attributes in LastModifiedDateTime: UseTimeZone. By default that attribute is set to true, but in my case it was necessary to set it to false:

#region LastModifiedDateTime
[PXDBDateAndTime(UseTimeZone = false)]
[PXUIField(DisplayName = "Last Modified Date Time")]
public virtual DateTime? LastModifiedDateTime { getset; }
public abstract class lastModifiedDateTime : PX.Data.BQL.BqlDateTime.Field<lastModifiedDateTime> { }
#endregion

After I've made usage of that attribute, values in UI started to display according to what I've seen on db level.

Summary

In case if you see discrepancy between value on db, and UI, and want to have only db level displayed in UI, use flat UseTimeZone set to false.

 

Selectfrom For Usage In View Select

 

Hello everybody,

I want to leave a short note on how to use SelectFrom for legacy code, and pass it in View.Select for Acumatica newer versions. 

var cmd = new SelectFrom<PartsCatalog>.
    InnerJoin<INSiteStatus>.On<Use<PartsCatalog.inventoryID>.AsInt.IsEqual<Use<INSiteStatus.inventoryID>.AsInt>>.View(this);
 
var s = (currentFilter.PageNbr ?? 0) * (currentFilter.PageSize ?? 0);
int startRow = s > 0 ? s : PXView.StartRow;
int totalRows = 0;
int maxRows = (currentFilter.PageSize ?? 0) == 0 ? PXView.MaximumRows : currentFilter.PageSize ?? 0;
 
var list = cmd.View.Select(new[] { currentFilter }, nullPXView.Searches,
    PXView.SortColumns, PXView.Descendings, PXView.Filters, ref startRowmaxRowsref totalRows).ToList();

As you can see, each time, when field is not converted to a newer Acumatica FBQL, you can refer to Use and AsType.

 

How To Overrde Authorize Cc Payment Action In Acumatica

 

Hello everybody,

as it was mentioned in one of my previous blog posts, regarding overriding base actions of Acumatica graphs, family of graph extensions grows and grows. 

 

Today I want to share one more code snippet which you can use for extending modification of Acumatica actions. Below goes code fragment, which you can use for modification of Authorize CC Payment:

public class PaymentTransactionExt : PXGraphExtension<PaymentTransactionSOOrderEntry>
{
    [PXOverride]
    public virtual IEnumerable AuthorizeCCPayment(PXAdapter adapterFunc<PXAdapterIEnumerablebaseFunc)
    {
        
        var result = baseFunc(adapter);
        
        return result;
    }
}

Of what I foresee now, is a loooooot of work for Acumatica developers upgrading to a newer versions of Acumatica. 

Why I say so? 

Because as usually ISV have their code in SOOrderEntry extensions. But now they either will need to move their code from SOOrderEntry extensions or for example to reference Base1 ( which is your extension ) as well as Base.

Reason why Acumatica does this is probably following few general programming principles of: SRP ( single responsibility principle ), DRY ( Dont Repeat Yourself ) principle, Open Closed principle on graph level, which in long term will bring more stability to the system.

Basic advice will be this, don't expect that upgrade of your code base to 2019 R2 will be as fast, as it used to be in the past, and before giving estimate which you used to give, pay special attention to how new actions are declared.

 

 

How To Override Shoprates Action In Sales Orders Form

 

Hello everybody,

today I want to leave a short post on how to override Shop rates Action in Acumatica. 

I mean this button:

Below goes C# code, with which you can achieve it:

public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry.CarrierRatesSOOrderEntry>
{
    [PXOverride()]
    public virtual IEnumerable ShopRates(PXAdapter adapterFunc<PXAdapterIEnumerablebaseMethod)
    {
        //your code here
        var retVal = baseMethod?.Invoke(adapter);
        //and possibly here
        return retVal;
    }
}

As you can see from the code, life in Acumatica becomes more complex, and if in the past you've used to override directly your actions, now you'll need to create extension for extension in order to override some Actions. 

Similar issues I've seen in other places of SOOrderEntry, for example in AuthorizeCCPayment, CaptureCCPayment, etc.

 

Pxlongoperation Cleans Everything How To Avoid

 

Hello everybody,

today I want to write a few words about interesting behavior of PXLongOperation.StartOperation in Acumatica.

On the glance that looks pretty simple and straightforward. You have something processing of which will take a long time? Use PXLongOperation.StartOperation. So imagine, you extend ARPaymentEntry graph like this:

public PXAction<ARPayment> StartOperation;
[PXProcessButton]
[PXUIField(DisplayName = "Some long running operation")]
protected virtual IEnumerable startOperation(PXAdapter adapter)
{
   
   PXLongOperation.StartOperation(Base, () => SomeLongRunningOperation(Base.Document.Current));
return adapter.Get(); }

In this case you may notice interesting behavior. If your ARPayment is saved, then everything will work relatively fine. But if not, any kind of data, that user enetered will be lost. How to deal with it. Below goes implementation that will deal with this:

public PXAction<ARPayment> StartOperation;
 
[PXProcessButton]
[PXUIField(DisplayName = "Some long running operation")]
protected virtual IEnumerable startOperation(PXAdapter adapter)
{
    Base.Save.Press();
    PXCache cache = Base.Document.Cache;
    List<ARRegisterlist = new List<ARRegister>();
    list.Add(Base.Document.Current);
    PXLongOperation.StartOperation(Base, () => SomeLongRunningOperation(Base.Document.Current));
    return list;
}

Besides that, in the end of your function SomeLongRunningOperation you may consider adding code like this:

ARPayment arPayment = cuArPayment;
var newGraph = PXGraph.CreateInstance<ARPaymentEntry>();
newGraph.Document.Current =
    Base.Document.Search<ARPayment.refNbr>(arPayment.RefNbr, arPayment.DocType);
 
//  Some additional lines of code
newGraph.Persist();
PXRedirectHelper.TryRedirect(newGraphPXRedirectHelper.WindowMode.Same);

In that case after SomeLongRunningOperation function will finish it's processing, it will refresh current screen of user.

Summary

If you deal with PXLongRunningOperation, you'll need to have following elements:

  1. Save existing item
  2. Return list with one element ( current primary element of view ), which will leave user on the same element
  3. In the end of PXLongRunningOperation call TryRedirect which will refresh currently selected view

 

 

How To Make Dynamic List With Check Boxes In Acumatica

 

Hello everybody,

today I want to share with you a piece of code, with help of which you can get list of all activer order types in Acumatica and also you'll get a chance to select those types with checkbox. Take a look on a screenshot below:

how to achieve this?

Code below without description of DAC class gives you this:

protected virtual void _(Events.FieldSelecting<SetupClassSetupClass.orderTypese)
{
    if (e.Row == null)
    {
        return;
    }
 
    var orderTypes = SelectFrom<SOOrderType>.Where<SOOrderType.active.IsEqual<True>>.View.Select(this).Select(a => a.GetItem<SOOrderType>())
        .ToList();
    var allowedValues = orderTypes.Select(a => a.OrderType).ToArray();
    var allowedLabels = orderTypes.Select(a => a.Descr).ToArray();
 
    var returnState = PXStringState.CreateInstance(e.ReturnValue, allowedLabels.Length, true,
        typeof(SetupClass.orderTypes).Name,
        false, -1, string.Empty, allowedValuesallowedLabelsfalsenull);
    (returnState as PXStringState).MultiSelect = true;
    e.ReturnState = returnState;
}

On aspx level, it look trivial:

<px:PXDropDown ID="PXDropDown1" runat="server" DataField="OrderTypes" CommitChanges="true" />

With help of this code, you'll get list of all order types, which you'll be able to persist in database.