Acumatica - field type decimal getting cast error - casting

Customized decimal type field generates error after being published.
I've tried many different syntaxes, if isnull(), etc and for some reason I am not thinking to try the right one.
if (tran != null && tran.TranLineNbr != null &&
arTran != null && arTran.TranType == tran.TranType &&
arTran.RefNbr == tran.RefNbr && arTran.LineNbr == tran.TranLineNbr)
{
decimal? amtOrg = arTran.GetExtension<ARTranExt>().UsrDLYAMTORG;
tran.GetExtension<GLTranExt>().UsrDLYAMTORG = amtOrg;
}
The value on release should go from Ar to Gl. All the other custom fields work, but not the decimal.
"Error: An error occurred during processing of the field Original Amount. Specified cast is not valid."

Your data access class should look similar to
/// <summary>
/// Adds extension fields to and modifies attributes in <see cref="ARTran"/>.
/// </summary>
public sealed class ARTranExt : PXCacheExtension<ARTran>
{
public abstract class usrDLYAMTORG : IBqlField
{
}
[PXDBDecimal]
[PXUIField(DisplayName = "Your Field Name")]
[PXDefault(TypeCode.Decimal, "0.00", PersistingCheck = PXPersistingCheck.Nothing)]
public decimal? UsrDLYAMTORG { get; set; }
}
In regards to bringing a custom value from AR to GL on release we have accomplished the same with the following code.
public class ARReleaseProcessExtension : PXGraphExtension<ARReleaseProcess>
{
public delegate GLTran InsertInvoiceDetailsTransactionDel(JournalEntry je, GLTran tran, ARReleaseProcess.GLTranInsertionContext context);
[PXOverride]
public virtual GLTran InsertInvoiceDetailsTransaction(JournalEntry je, GLTran tran, ARReleaseProcess.GLTranInsertionContext context, InsertInvoiceDetailsTransactionDel del)
{
tran.GetExtension<GLTranExt>().UsrDLYAMTORG = context.ARTranRecord.GetExtension<ARTranExt>().UsrDLYAMTORG;
return del?.Invoke(je, tran, context);
}
}

Related

Acumatica - Adding Multiple Tracking Numbers in Field

I am adding the Tracking Number data field from SOPackageDetail to the Invoices screen (SO303000) on the Freight Details Tab. I know that it only shows one ShipmentNbr and this is what I'm using to join the two tables but I would like all of the tracking numbers, since there can be more than one per shipment number, to show in the field instead of just one. They can be just separated in the field value by a comma. Here is my code and it does work for just one tracking number.
Graph:
public class SOInvoiceEntry_Extension : PXGraphExtension<SOInvoiceEntry>
{
#region Event Handlers
protected void SOFreightDetail_RowSelecting(PXCache cache, PXRowSelectingEventArgs e, PXRowSelecting del)
{
if (del != null)
del(cache, e);
var row = (SOFreightDetail)e.Row;
if (row == null) return;
using (new PXConnectionScope())
{
SOPackageDetail track = PXSelect<SOPackageDetail, Where<SOPackageDetail.shipmentNbr, Equal<Required<SOFreightDetail.shipmentNbr>>>>.Select(Base, row.ShipmentNbr);
if(track != null){
SOFreightDetailExt invoiceExt = row.GetExtension<SOFreightDetailExt>();
if (invoiceExt != null){
invoiceExt.TrackNumber = track.TrackNumber;
}
}
}
}
#endregion
}
DAC Extension:
public class SOFreightDetailExt : PXCacheExtension<PX.Objects.SO.SOFreightDetail>
{
#region TrackNumber
public abstract class trackNumber : PX.Data.IBqlField
{
}
protected string _TrackNumber;
[PXString()]
[PXDefault()]
[PXUIField(DisplayName = "Tracking Number", IsReadOnly = true)]
public virtual string TrackNumber
{
get
{
return this._TrackNumber;
}
set
{
this._TrackNumber = value;
}
}
#endregion
}
I want all tracking numbers associated with the Shipment Nbr to be displayed in this field, right now it only shows one. This only will happen if there is multiple packages for one shipment number.
You need to loop on your records (PXSelect) in a foreach. You then need to add each string value to your tracknumber field. Something like this should work...
SOFreightDetailExt invoiceExt = row.GetExtension<SOFreightDetailExt>();
if(invoiceExt == null)
return;
foreach(SOPackageDetail track in PXSelect<SOPackageDetail, Where<SOPackageDetail.shipmentNbr, Equal<Required<SOFreightDetail.shipmentNbr>>>>.Select(Base, row.ShipmentNbr))
{
invoiceExt.TrackNumber = $"{invoiceExt.TrackNumber}, {track.TrackNumber}";
}
Also, there is no need for the PXConnectionScope. You can remove that.

Persist entity : cannot be null

When I use this code, I have this error " Integrity constraint violation: 1048 Column 'ecriture_id' cannot be null " :
$produit = new Produit();
$ecriture = new Ecriture();
$produit->setEcriture($ecriture);
// $this->em->persist($ecriture);
$this->em->persist($produit);
If I uncommented " $this->em->persist($ecriture) " it's work true.
I don't understand because my "Produit" entity have a cascade persist :
Entity "Produit" :
class Produit
{
/**
* #ORM\OneToOne(targetEntity="LogicielBundle\Entity\Ecriture", inversedBy="gestionLocativeProduit", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $ecriture;
public function setEcriture(\LogicielBundle\Entity\Ecriture $ecriture)
{
$ecriture->setGestionLocativeProduit($this);
$this->ecriture = $ecriture;
// Similar problem if I add "$ecriture->setGestionLocativeProduit($this);" right here
return $this;
}
public function getEcriture()
{
return $this->ecriture;
}
Entity "Ecriture" :
class Ecriture
{
/**
* #ORM\OneToOne(targetEntity="LogicielBundle\Entity\GestionLocative\Produit", mappedBy="ecriture")
* #ORM\JoinColumn(nullable=true)
*/
private $gestionLocativeProduit;
public function setGestionLocativeProduit(\LogicielBundle\Entity\GestionLocative\Produit $gestionLocativeProduit)
{
$this->gestionLocativeProduit = $gestionLocativeProduit;
return $this;
}
public function getGestionLocativeProduit()
{
return $this->gestionLocativeProduit;
}
I have always had similair problems when I added new columns to my database, and got liek you the cannot be null error.
What mostly fixed it for me was adding nullable is true, then in twig, controller or form add the required = true attribute. Therefor it always had to be filled.

JavaBeans property with if statement

I have a label form JavaFx to disply if an operation was succeed or not. This should updated automaticaly when the value changed
public class operation {
private BooleanProperty success = new SimpleBooleanProperty();
public final boolean getSuccess() {
return success.get();
}
public final void setSuccess(boolean value) {
success.set(value);
}
public BooleanProperty successProperty() {
return success;
}
}
there is somewhere this code:
operation.setSuccess(true);
and this:
label1.textProperty().bind(Bindings.format("%s", operation.successProperty() != null || false? "succeed": "not succeed" ));
The Problem successProperty() is not value and if I invoke getValue() it will not updated on UI
Your condition never changes. operation.successProperty() != null just checks whether the reference returned by the method successProperty() is null: it never is.
I think you want
label1.textProperty().bind(Bindings
.when(operation.successProperty())
.then("succeed")
.otherwise("not succeed"));

How can I override the test method name that appears on the TestNG report?

How can I override the test name that appears on the TestNG report? I want to override the name that appears in the middle column (currently shows as the method name). Is this even possible?
I tried to do it like this, but it didn't work.
public class EchApiTest1 extends TestBase {
...
#BeforeTest
public void setUp() {
restClient = new RestClientPost();
this.setTestName( "ech: XXXXXX" );
}
And, the base class:
import org.testng.ITest;
public class TestBase implements ITest {
String testName = "";
#Override
public String getTestName() {
return this.testName;
}
public void setTestName( String name ) {
this.testName = name;
}
}
NOTE: The above code does work when I am viewing the report detail in the Jenkins TestNG plugin report, which shows the overridden test name as a string called "Instance Name:" at the beginning of the Reporter log output. Why, in this case, WHY does a "setTestName()" method alter a string labeled "Instance Name" in the report?
One answer I found had a suggestion like this but I don't know how to pass an ITestResult arg to a AfterMethod method:
#AfterMethod
public void setResultTestName( ITestResult result ) {
try {
BaseTestMethod bm = (BaseTestMethod)result.getMethod();
Field f = bm.getClass().getSuperclass().getDeclaredField("m_methodName");
f.setAccessible(true);
f.set( bm, bm.getMethodName() + "." + your_customized_name );
} catch ( Exception ex ) {
Reporter.log( "ex" + ex.getMessage() );
}
Thoughts?
Please find following code for set custom name of testcase in TestNG reports.
Following features are available in this code.
Dynamic execution on same test-case in multiple time
Set custom test-case name for reports
Set parallel execution of multiple test-cases execution
import java.lang.reflect.Field;
import org.testng.ITest;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.internal.BaseTestMethod;
import com.test.data.ServiceProcessData;
public class ServiceTest implements ITest {
protected ServiceProcessData serviceProcessData;
protected String testCaseName = "";
#Test
public void executeServiceTest() {
System.out.println(this.serviceProcessData.toString());
}
#Factory(dataProvider = "processDataList")
public RiskServiceTest(ServiceProcessData serviceProcessData) {
this.serviceProcessData = serviceProcessData;
}
#DataProvider(name = "processDataList", parallel = true)
public static Object[] getProcessDataList() {
Object[] serviceProcessDataList = new Object[0];
//Set data in serviceProcessDataList
return serviceProcessDataList;
}
#Override
public String getTestName() {
this.testCaseName = "User custom testcase name";
// this.testCaseName = this.serviceProcessData.getTestCaseCustomName();
return this.testCaseName;
}
#AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result) {
try {
BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
f.setAccessible(true);
f.set(baseTestMethod, this.testCaseName);
} catch (Exception e) {
ErrorMessageHelper.getInstance().setErrorMessage(e);
Reporter.log("Exception : " + e.getMessage());
}
}}
Thanks
I found a "workaround" but I am hoping for a better answer. I want to be able to show this "test name" OR "instance name" value on the HTML report (not just within the Reporter.log output) and I am starting to think its not possible :
#Test(dataProvider = "restdata2")
public void testGetNameFromResponse( TestArguments testArgs ) {
this.setTestName( "ech: " + testArgs.getTestName() );
Reporter.log( getTestName() ); // this magic shows test name on report
....
With this workaround, the user can now identify which test it was by looking at the Reporter.log output but I still wish the name was more prominant.
I suspect the answer lies in writing a TestListenerAdapter that somehow overrides the ITestResult.getTestNameMethod() method? That is the holy grail I am looking for.
The ‘result’ object will automatically pass in the method setResultTestName( ITestResult result )
Make sure you put alwaysRun=true like the following when you have groups defined in your test class otherwise “AfterMethod” will not be excuted.
#AfterMethod (alwaysRun=true)

Why does this unit test fail when testing DateTime equality?

Using NUnit 2.2 on .NET 3.5, the following test fails when using DateTime.Equals. Why?
[TestFixture]
public class AttributeValueModelTest
{
public class HasDate
{
public DateTime? DateValue
{
get
{
DateTime value;
return DateTime.TryParse(ObjectValue.ToString(), out value) ? value : new DateTime?();
}
}
public object ObjectValue { get; set; }
}
[Test]
public void TwoDates()
{
DateTime actual = DateTime.Now;
var date = new HasDate {ObjectValue = actual};
Assert.IsTrue(date.DateValue.Value.Equals(actual));
}
}
The dates aren't equal. TryParse drops some ticks. Compare the Tick values.
For one test run:
Console.WriteLine(date.DateValue.Value.Ticks);
Console.WriteLine(actual.Ticks);
Yields:
633646934930000000
633646934936763185
The problem isn't really TryParse, but actually ToString().
A DateTime object starts with precision (if not accuracy) down to millionth of seconds. ToString() convertsit into a string, with precision only to a second.
TryParse is doing the best it can with what it is given.
If you add a format specifier (along the lines of "yyyy-MM-dd HH:mm:ss.ffffff"), it should work.
To specify a format that includes all the precision, you can use the String.Format() method. The example that James gives would look like this:
String.Format("{0:yyyy-MM-dd HH:mm:ss.ffffff}", ObjectValue);
I don't know what that will do when you pass it something that's not a date.
Perhaps a simpler approach is to add a special case when you've already got a date object:
public DateTime? DateValue
{
get
{
DateTime value = ObjectValue as DateTime;
if (value != null) return value;
return DateTime.TryParse(ObjectValue.ToString(), out value) ? value : new DateTime?();
}
}
public DateTime? DateValue
{
get
{
DateTime value;
bool isDate = DateTime.TryParse(ObjectValue.ToString(), out value);
return isDate ? new DateTime?(value) : new DateTime?();
}
}
I don't know if this is the same in .NET, but in Java the equals often will only compare if the instances are the same, not if the values are the same. You'd instead want to use compareTo.