In my FieldDefaulting event I have the below code
var row = (PMProject)e.Row;
decimal? dec = 0;
foreach (atcProjectLinesTable tb in ProjectLines.Select(this))
{
dec += tb.UnPrice;
}
throw new PXException("Total is "+dec);
e.NewValue = dec;
Im getting an unreachable code warning in Visual Studio and when I publish my project the fields value is zero.
Visual Studio reports about unreachable code due to PXException thrown in the middle of your FieldDefaulting event handler:
throw new PXException("Total is "+dec);
To profile intermediate values, you might take a look at the PXTrace class. The sample below shows how to write in Acumatica Trace value generated value for Sales Order Description:
public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
public void SOOrder_OrderDesc_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
var testDescr = "Test Order Description";
PXTrace.WriteInformation(string.Format("Sales Order Description: {0}", testDescr));
e.NewValue = testDescr;
}
}
Related
I need to change the default value of the Residential Delivery checkbox on the Customers AR.30.30.00 screen (Shipping tab) to checked by default. See screenshot:
In 2017R2, this event handler worked without error:
public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
{
protected virtual void LocationExtAddress_CResedential_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
var row = (LocationExtAddress)e.Row;
if (row != null)
{
e.NewValue = true; // checked by default
}
}
}
I'm updating this customization for 2020R2. It appears that LocationExtAddress has been replaced with DefLocationExt in newer versions. (Resedential is mis-spelled intentionally in the code... that's how Acumatica defined it.) I've tried changing the event handler to:
protected virtual void DefLocationExt_CResedential_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
var row = (DefLocationExt)e.Row;
if (row != null)
{
e.NewValue = true; // checked by default
}
}
But this results in a run-time error:
Failed to subscribe the event PX.Objects.AR.CustomerMaint_Extension::DefLocationExt_CResedential_FieldDefaulting in the graph PX.Objects.AR.CustomerMaint. The method signature looks like an event handler, but the cache DefLocationExt has not been found in the list of auto-initialized caches. Remove unused event handlers from the code.
How can I attach an event to this field in 2020R2?
Try a generic event handler and see if you get the same result.
It might look something like this.
protected virtual void _(Events.FieldDefaulting<PX.Objects.CR.Standalone.Location.cResedential> e)
{
var row = (PX.Objects.CR.Standalone.Location)e.Row;
if (row != null)
{
e.NewValue = true; // checked by default
}
}
I have a graph override for Invoices
public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
and in one of the event handlers I am updating TaxZoneID, which works fine. However, the taxes do not get updated or recalculated. I have tried the approach mentioned here
cache.SetValueExt<SOOrder.taxZoneID>(order, branchLoc.VTaxZoneID);
but that doesn't work for me. I have tried it in _FieldUpdating, _FieldUpdated, and even ARInvoice_RowPersisting(PXCache cache, PXRowPersistingEventArgs e, PXRowPersisting InvokeBaseHandler) events. Any ideas on why it doesn't work? The TaxZone and rates are already in the database (we are not using Avatax).
--- edit 1 ---
Here is the code where TaxZoneID is updated
namespace PX.Objects.AR
{
public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
{
#region Event Handlers
protected void ARShippingAddress_PostalCode_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e, PXFieldUpdated del)
{
// PXTrace.WriteInformation("ARShippingAddress_PostalCode_FieldUpdated");
ARShippingAddress row = e.Row as ARShippingAddress;
if (row != null) {
if (DoChangeTaxZone(row)) {
var invoice = Base.Document.Current;
if (invoice != null) {
invoice.TaxZoneID = GetTaxZoneId(row);
sender.SetValueExt<ARInvoice.taxZoneID>(invoice, invoice.TaxZoneID);
}
}
}
if (del != null)
{
del(sender, e);
}
}
#endregion
private bool DoChangeTaxZone(ARShippingAddress row)
{
// logic ...
return true;
}
private string GetTaxZoneId(ARShippingAddress row)
{
// logic ...
return "TAX-ZONE-ID";
}
}
}
When you programmatically interact with tax records using the typical methods, the tax total will not refresh properly. The Tax DAC attribute doesn't recalculate the totals by default to improve performance.
To force tax attribute refresh you need to change the tax calc mode.
Tax calc mode NoCalc does not recalculate totals. This is the default mode.
Setting tax calc mode to ManualCalc is necessary to refresh the updated tax.
Code example to update the tax amount field, you can adapt it to update tax zone.
ARInvoiceEntry invoiceMaint = PXGraph.CreateInstance<ARInvoiceEntry>();
TX.TaxAttribute.SetTaxCalc<ARTran.taxCategoryID>(invoiceMaint.Transaction.Cache, null, TX.TaxCalc.ManualCalc);
invoiceMaint.CurrentDocument.Current = invoiceMaint.Document.Search<ARInvoice.refNbr>("AR005452", ARDocType.Invoice).FirstOrDefault();
invoiceMaint.Taxes.Select();
invoiceMaint.Taxes.Current = invoiceMaint.Taxes.Search<ARTaxTran.taxID>("CAGST").FirstOrDefault();
invoiceMaint.Taxes.Cache.SetValueExt<ARTaxTran.curyTaxAmt>(invoiceMaint.Taxes.Current, 3);
invoiceMaint.Taxes.Update(invoiceMaint.Taxes.Current);
invoiceMaint.SelectTimeStamp();
invoiceMaint.Save.Press();
I am in the process of upgrading from Acumatica 2018 R2 to 2019 R1. In quite a few of my customizations, I have code, triggered by a button click or an event handler, which sets/changes the value of a custom field. That was working as expected in 2018 R2. However, in 2019 R1 the values on the custom fields are not being updated. Here's a simple example.
public class SOOrderEntry_SOOpenPOsGILink_Extension : PXGraphExtension<SOOrderEntry>
{
protected virtual void SOLine_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
{
SOLine row = (SOLine)e.Row;
if (row != null)
{
bool isEmpty = true;
SOLineExt ext = row.GetExtension<SOLineExt>();
// logic determining value of isEmpty
ext.UsrEnableOpenPOs = isEmpty;
}
}
}
Where UsrEnableOpenPOs is defined as folows.
public class SOLineExt : PXCacheExtension<PX.Objects.SO.SOLine>
{
#region UsrEnableOpenPOs
[PXBool]
[PXUIField(DisplayName="EnableOpenPOs", Enabled = false, Visible=false)]
[PXUnboundDefault(false, PersistingCheck = PXPersistingCheck.Nothing)]
public virtual bool? UsrEnableOpenPOs { get; set; }
public abstract class usrEnableOpenPOs : PX.Data.BQL.BqlBool.Field<usrEnableOpenPOs> { }
#endregion
}
In this example, when a SOLine is selected on the SO Order Entry screen, the value of SOLineExt.UsrEnableOpenPOs should be set to the value of isEmpty. This code works in 2018 R2 and correctly updates UsrEnableOpenPOs. However, in 2019 R1, the code is triggered correctly and runs, but the value on the screen is not updated.
As I mentioned, we have quite a few instances where we are running into this problem. In some cases the code is triggered by a button click and in other cases by different events like RowSelected, RowInserting, FieldUpdated and RowUpdated.
I would appreciate some insight on why this code is no longer working and what I can do to fix it.
Should always try to set the value in cache vs the row instance.
ex:
sender.SetValueExt<SOLineExt.usrEnableOpenPOs>(row, isEmptry);
Unbound fields should be set in the RowSelecting or FieldSelecting events. The RowSelected event should be for UI changes such as disabled, visible, etc. fields.
I am using rehosted-designer of WF 4.5 to give an interface for the users to create a workflow with custom activities and some of the inbuilt activities of the framework.
I am using AsyncCodeActivity model (perfect fit for my requirement), and hence is bound to execute a workflow as below, i mean execution is triggered at once without the possibility to iterate the activities in the workflow:
WorkflowApplication wf = new WorkflowApplication(activeFlowChart);
............
............
var result = wf.BeginRun(null,null);
while (!result.IsCompleted)
{
Thread.Sleep(1);
continue;
}
wf.EndRun(result);
My workflow can have multiple "WriteLine" activities scattered in the workflow.
I want to retrieve the message of a particular "WriteLine" and show in the trace window as in the order how it appears in the workflow.
I have tried as below on completion, which displays all the "WriteLine" messages appended at the end of the execution:
wf.Extensions.Add(writer);
wf.Completed = arg =>
{
if (!string.IsNullOrEmpty(writer.ToString()))
{
//display
}
};
I am looking for a way to get the "WriteLine" message immediately when it occurs in the workflow, not on completion.
The below link helped me to think about writing custom TextWriter to solve the above usecase:
Redirecting Console.WriteLine() to Textbox
Instead of appending the characters to the "writer" object and print it to the display window when the workflow is completed (refer the code in my question), i wrote a custom TextWriter to print the message of each "WriteLine" activity when a new line("\r\n") is encountered. example below:
in constructor:
var writer = new WriteLineTextWriter(DisplayTraceMessage);
Console.SetOut(writer);
custom TextWriter:
public class WriteLineTextWriter : TextWriter
{
private Action<string, Brush> WriteTraceMessageToGUI { get; set; }
private StringBuilder line;
private byte counter;
public WriteLineTextWriter(Action<string, Brush> action)
{
WriteTraceMessageToGUI = action;
line = new StringBuilder();
}
public override System.Text.Encoding Encoding
{
get { return System.Text.Encoding.Unicode; }
}
public override void Write(char value)
{
if (value == '\r' || value == '\n')
{
counter++;
}
else
{
line.Append(value.ToString());
}
if (counter == 2)
{
WriteTraceMessageToGUI(line.ToString(), Brushes.BlueViolet);
line.Clear();
counter = 0;
}
}
public override void Write(string value)
{
WriteTraceMessageToGUI(value, Brushes.BlueViolet);
}
}
The goal is taking the Journal Transaction generated from the AP Bill page and adding 2 additional rows in GLTran.
1st Attempt
First, I extended the Release action from the Journal Transactions graph to include the 2 new lines:
public class JournalEntryExt : PXGraphExtension<JournalEntry>
{
public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
[PXOverride]
public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
baseMethod(adapter);
//new code
GLTran tranRow = new GLTran();
tranRow = this.Base.GLTranModuleBatNbr.Insert(tranRow);
tranRow.AccountID = 2713;
tranRow.SubID = 467;
tranRow.CuryDebitAmt = 100;
this.Base.GLTranModuleBatNbr.Update(tranRow);
tranRow = new GLTran();
tranRow = this.Base.GLTranModuleBatNbr.Insert(tranRow);
tranRow.AccountID = 1514;
tranRow.SubID = 467;
tranRow.CuryCreditAmt = 100;
this.Base.GLTranModuleBatNbr.Update(tranRow);
this.Base.Actions.PressSave();
return adapter.Get();
}
Result: Creating and releasing the batch, entered the 2 new lines correctly.
After this, I thought that releasing the AP Bill would also trigger this extended logic from the GL Page. However, that didn't occur - The release of the Bill doesn't seem to re-use the Release logic defined in the GL page.
2nd Attempt
Then, I went back to the GL page and included the logic in the RowPersisted event, so that the 2 new lines would get created right after saving the document:
public class JournalEntryExt : PXGraphExtension<JournalEntry>
{
protected virtual void Batch_RowPersisted(PXCache sender, PXRowPersistedEventArgs e)
{
if (e.Row == null)
{
return;
}
Batch batchRow = (Batch)e.Row;
if (batchRow != null
&& e.Operation == PXDBOperation.Insert
&& e.TranStatus == PXTranStatus.Completed)
{
////new code
GLTran tranRow = new GLTran();
tranRow = this.Base.GLTranModuleBatNbr.Insert(tranRow);
tranRow.AccountID = 2713;
tranRow.SubID = 467;
tranRow.CuryDebitAmt = 102;
this.Base.GLTranModuleBatNbr.Update(tranRow);
tranRow = new GLTran();
tranRow = this.Base.GLTranModuleBatNbr.Insert(tranRow);
tranRow.AccountID = 1514;
tranRow.SubID = 467;
tranRow.CuryCreditAmt = 102;
this.Base.GLTranModuleBatNbr.Update(tranRow);
}
}
Result: Creating and saving the Batch correctly entered the 2 new lines.
After this, I thought that releasing the AP Bill would trigger this extended event, given that a Journal Entry graph should get created and used from the Bill page, but in this case, also releasing the AP Bill, did not add the 2 new lines in the generated Batch.
3rd Attempt
Then I thought I could extend the Bill's Release action and take control of the generated Journal Entry with the Search<> method. However, in this case, the extended logic seems to be executed within a transaction as the Document.Current.BatchNbr was still NULL:
4th Attempt
Finally, I tried to extend the Persist() method of APReleaseProcess similarly to how it's done in the guide T300, however none of the methods are listed (version 17.207.0029):
Any other ideas as to how to enter these GL Lines?
Thanks!
Hopefully, it didn't take you forever to go through these 4 attempts... I've got to say though, the number of efforts and details in your question is quite impressive and definitely very appreciated!
A 2-step customization will be required to insert 2 additional GL Transactions in the Batch generated for an AP Bill:
to insert additional GL Transactions, you need to override Persist method within the JournalEntry BLC extension and invoke the logic to insert additional GLTrans only if the custom ModifyBatchFromAP boolean flag value equals True:
using PX.Data;
using System;
namespace PX.Objects.GL
{
public class JournalEntry_Extension : PXGraphExtension<JournalEntry>
{
private bool modifyBatchFromAP = false;
public bool ModifyBatchFromAP
{
get
{
return modifyBatchFromAP;
}
set
{
modifyBatchFromAP = value;
}
}
[PXOverride]
public void Persist(Action del)
{
if (ModifyBatchFromAP)
{
var glTran = Base.GLTranModuleBatNbr.Insert();
Base.GLTranModuleBatNbr.SetValueExt<GLTran.accountID>(glTran, "20000");
glTran = Base.GLTranModuleBatNbr.Update(glTran);
Base.GLTranModuleBatNbr.SetValueExt<GLTran.subID>(glTran, "000000");
glTran.CuryDebitAmt = 100;
glTran.TranDesc = "Additional Debit Transaction for AP Doc";
Base.GLTranModuleBatNbr.Update(glTran);
glTran = Base.GLTranModuleBatNbr.Insert();
Base.GLTranModuleBatNbr.SetValueExt<GLTran.accountID>(glTran, "20200");
glTran = Base.GLTranModuleBatNbr.Update(glTran);
Base.GLTranModuleBatNbr.SetValueExt<GLTran.subID>(glTran, "000000");
glTran.CuryCreditAmt = 100;
glTran.TranDesc = "Additional Credit Transaction for AP Doc";
Base.GLTranModuleBatNbr.Update(glTran);
}
del();
}
}
}
after that in the overridden Release action within the APInvoiceEntry_Extension, you will subscribe to the InstanceCreated event for the JournalEntry BLC type to set ModifyBatchFromAP flag value to True allowing your logic from step 1 to execute for the Batch generated only for an AP document:
using PX.Data;
using PX.Objects.GL;
using System.Collections;
namespace PX.Objects.AP
{
public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
{
public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
[PXOverride]
public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
PXGraph.InstanceCreated.AddHandler<JournalEntry>((JournalEntry graph) =>
{
graph.GetExtension<JournalEntry_Extension>().ModifyBatchFromAP = true;
});
return baseMethod(adapter);
}
}
}
P.S. it is not currently possible to use the Select Method to Override dialog with the APReleaseProcess class due to PXHiddenAttribute applied to it. Let me forward this to our Engineering Team to bring their attention on that matter.