Grab LineNbr of Newly Inserted Line - acumatica

I'm trying to get the line number of a newly inserted INTran row.
Here is the code I'm using:
INRegister issue = new INRegister();
//Code to populate INRegister...
INIssueEntry graph = PXGraph.CreateInstance<INIssueEntry>();
graph.issue.Insert(issue);
graph.Actions.PressSave();
//PXSelect to get new RefNbr for INRegister object
issue = PXSelect<INRegister, Where<INRegister.refNbr, Equal<Current<INRegister.refNbr>>,
And<INRegister.docType, Equal<Current<INRegister.docType>>>>>.Select(graph);
graph.issue.Current = issue;
INTran issueRow = new INTran();
//Code to populate issueRow...
graph.transactions.Insert(issueRow);
graph.transactions.Current = issueRow;
graph.Actions.PressSave();
//Trying to get transaction line number
issueRow = PXSelect<INTran, Where<INTran.refNbr, Equal<Current<INTran.refNbr>>,
And<INTran.docType, Equal<Current<INTran.docType>>,
And<INTran.lineNbr, Equal<Current<INTran.lineNbr>>>>>>.Select(graph);
//At this point, issueRow is now null because LineNbr was null above
row.TranRefNbr = issueRow.RefNbr;
row.TranLineNbr = issueRow.LineNbr;
row.Released = true;
ItemReqs.Update(row);
//... ending code...
I've examined trying to save before setting as current (setting as current item for convenience for when I write the PXSelect to get the LineNbr), but I've found that the LineNbr, even on the graph, stays null during the entire excution. I've looked in the database as the graph is saving the new line and it does contain the line number. I'm not sure why my PXSelect isn't grabbing the line number.

When you insert your transaction row the return should have the linenbr
var issueRow = graph.transactions.Insert(issueRow);
I am guessing your issue is that you are setting current after using the issueRow that was NOT returned which would have a null linenbr.
Also you really do not need to PressSave until the end (or ready to generate the batch) as the linenbr will still get set through the LineNbr attribute. Referring to a save after inserting the INRegister record.
I think current will also be set after you insert so I don't see a need to set current which will remove your need for the PXSelects.
Example getting the LineNbr in the simplest of steps:
INIssueEntry graph = PXGraph.CreateInstance<INIssueEntry>();
graph.issue.Insert(new INRegister());
INTran issueRow = graph.transactions.Insert(new INTran());
//issueRow will now have a LineNbr value...
PXTrace.WriteInformation($"My Line Nbr is {issueRow.LineNbr}");
//RefNbr will receive its value when perform the Persist (Actions.PressSave)

Related

Where is my error with my join in acumatica?

I want to get all the attributes from my "Actual Item Inventry" (From Stock Items Form) so i have:
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem,
On<CSAnswers.refNoteID, Equal<Current<InventoryItem.noteID>>>
>
>.Select(new PXGraph());
But, this returns me 0 rows.
Where is my error?
UPDATED:
My loop is like this:
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
... but i can not go inside foreach.
Sorry for the English.
You should use an initialized graph rather than just "new PXGraph()" for the select. This can be as simple as "this" or "Base" depending on where this code is located. There are times that it is ok to initialize a new graph instance, but also times that it is not ok. Not knowing the context of your code sample, let's assume that "this" and "Base" were insufficient, and you need to initialize a new graph. If you need to work within another graph instance, this is how your code would look.
InventoryItemMaint graph = PXGraph<InventoryItemMaint>.CreateInstance<InventoryItemMaint>();
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem, On<CSAnswers.refNoteID, Equal<Current<InventoryItem.noteID>>>>>
.Select(graph);
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
However, since you should be initializing graph within a graph or graph extension, you should be able to use:
.Select(this) // To use the current graph containing this logic
or
.Select(Base) // To use the base graph that is being extended if in a graph extension
Since you are referring to:
Current<InventoryItem.noteID>
...but are using "new PXGraph()" then there is no "InventoryItem" to be in the current data cache of the generic base object PXGraph. Hence the need to reference a fully defined graph.
Another syntax for specifying exactly what value you want to pass in is to use a parameter like this:
var myNoteIdVariable = ...
InventoryItemMaint graph = PXGraph<InventoryItemMaint>.CreateInstance<InventoryItemMaint>();
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem, On<CSAnswers.refNoteID, Equal<Required<InventoryItem.noteID>>>>>
.Select(graph, myNoteIdVariable);
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
Notice the "Required" and the extra value in the Select() section. A quick and easy way to check if you have a value for your parameter is to use PXTrace to write to the Trace that you can check after refreshing the screen and performing whatever action would execute your code:
PXTrace.WriteInformation(myNoteIdVariable.ToString());
...to see if there is a value in myNoteIdVariable to retrieve a result set. Place that outside of the foreach block or you will only get a value in the trace when you actually get records... which is not happening in your case.
If you want to get deep into what SQL statements are being generated and executed, look for Request Profiler in the menus and enable SQL logging while you run a test. Then come back to check the results. (Remember to disable the SQL logging when done or you can generate a lot of unnecessary data.)

Acumatica - get the last displayed record

Is there an eloquent way, more or less, to get the last displayed record in a grid in Acumatica? Let's say even if they do all the sorting and rearranging, is there a way for example when pressing a button on a grid to get the last record? Basically, I would like to copy that record as a new one.
Create a PXAction for your button.
Inside the PXAction iterate in your data view until the last record.
For example, if the name of your Data view Bound to your grid is YzLines, and object type in the grid line (DAC) is Yz, then it can be:
Yz lastLine;
foreach (Yz line in YzLines.Select())
lastLine = line;
To get to the last record you can also use .Last() or .LastOrDefault().
If you need the last record according to client sorting, you should implement a data view delegate, it looks like this:
protected virtual IEnumerable yzLines()
{
PXSelectBase<Yz> cmd =
new PXSelectJoinGroupBy<Yz, ...>(this);
int startRow = PXView.StartRow; //Get starting row of the current page
int totalRows = 0;
foreach (PXResult<Yz> res in
cmd.View.Select(null, null,
PXView.Searches,
ARDocumentList.View.GetExternalSorts(),//Get sorting fields
ARDocumentList.View.GetExternalDescendings(),//Get sorting direction
ARDocumentList.View.GetExternalFilters(),//Get filters
ref startRow,
PXView.MaximumRows, //Get count of records in the page
ref totalRows))
{
//processing of records
}
PXView.StartRow = 0;//Reset starting row
}

How do I prevent SOLineSplit and INItemPlan from deleting when Qty = 0?

I noticed that when the quantity of a SOLine is zero, there are no SOLineSplit or INItemPlan records available for that line. The second the quantity is greater than 0, the system makes those records, and if the qty is set back to 0, the records are deleted.
Is there a way to prevent the SOLineSplit and INItemPlan objects from deleting when a record is set to 0 quantity?
Is there a way to still have the system create an SOLineSplit and INItemPlan if the SOLine is initially created with a 0 quantity?
The reason for the question is that a customer wants the system to lock the SOLine after a certain point, but also allow for the Qty to be adjusted from another screen. Since this is not directly changing the value on the SOLine screen, this isn't triggering the events to create the split and plan.
I have tried creating an instance of SOOrderEntry in the custom screen as follows:
SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>();
//Also tried graph.Transactions.Current = line, but did not work
graph.Transactions.Update(line);
graph.Actions.PressSave();
Doing that keeps resulting in a null object reference error:
Error: An error occurred during processing of the field OrderQty : Object reference not set to an instance of an object..
System.NullReferenceException: Object reference not set to an instance of an object.
at PX.Objects.SO.SOOrderEntry.SOLine_OrderQty_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e) ...
I think the problem with your statement in using SOOrderEntry is you are not truly loading the order in the graph to update it correctly. After you create the graph instance and before the transaction update, you should load the document header like this...
SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>();
graph.Document.Current = graph.Document.Search<SOOrder.orderNbr>(line.OrderNbr, line.OrderType);
if(graph.Document.Current == null)
{
return;
}
graph.Transactions.Update(line);
graph.Actions.PressSave();
As for controlling how the plan and split records are entered from SOOrderEntry... The entries are controlled through the attributes on PlanID. The cache attached is where this gets added on SOOrderEntry...
[PXMergeAttributes(Method = MergeMethod.Append)]
[SOLineSplitPlanID(typeof(SOOrder.noteID), typeof(SOOrder.hold), typeof(SOOrder.orderDate))]
protected virtual void SOLineSplit_PlanID_CacheAttached(PXCache sender)
{
}
You can make your own graph extension of Sales order and replace the attribute with your own version of SOLineSplitPlanID... it might be a battle for you as I am not sure why you would want the plan record to exist when zero qty to plan.

Updating DAC Extension

I am trying to update some custom fields in a DAC extension via code, it is not saving the changes to the DB. The code works fine to retrieve the extension and data. What am I missing - do i have to somehow update the myLS with the extension (i thought it did that automatically)?
myLS = LineItemSerial.Select();
INItemLotSerialExt myext = myLS.GetExtension<INItemLotSerialExt>();
myext.UsrFrame1 = "xyz";
myext.UsrFrame2 = "zzz";
myext.UsrFrame3 = "yyy";
LineItemSerial.Update(myLS);
graph.Actions.PressSave();
You should say to cache of Acumatica that you want to update value:
LineItemSerial.Cache.SetValueExt(myLS , "UsrFrame1", "xyz");
LineItemSerial.Cache.SetValueExt(myLS , "UsrFrame2 ", "zzz");
LineItemSerial.Cache.SetStatus(myLS , PXEntryStatus.Modified);
LineItemSerial.Cache.Update(myLS);
LineItemSerial.Cache.IsDirty = true;
NB. LineItemSerial.Cache.IsDirty = true; for some cases can be omitted but in my experience it was often helpful.
INItemLotSerialExt myext = LineItemSerial.GetExtension<INItemLotSerialExt>(myLS); //if LineItemSerial is a view related to the DAC. I hope LineItemSerial is a public view defined in the graph as you are trying to save the changes when u press the save of graph.
OR
INItemLotSerialExt myext = PXCache<INItemLotSerial>.GetExtension<INItemLotSerialExt>(myLS);
Is'nt this the rite way to get the extension?
From documentation
GetExtension(object)
InventoryItem item = cache.Current as InventoryItem;
InventoryItemExtension itemExt =
cache.GetExtension<InventoryItemExtension>(item);
OR
GetExtension(Table)
The code below gets an extension data record corresponding to the given instance of the base data record.
InventoryItem item = cache.Current as InventoryItem;
InventoryItemExtension itemExt =
PXCache<InventoryItem>.GetExtension<InventoryItemExtension>(item);
Try something like this...
ContractExtension cExt = PXCache<PMProject>.GetExtension<ContractExtension>(project);
ARInvoiceEntry graph = PXGraph.CreateInstance<ARInvoiceEntry>();
graph.Document.Current = graph.Document.Search<ARInvoice.projectID, ARInvoice.docDate>(projectID.Value, invoiceDate.Value);
if(graph.Document.Current !=null)
{
ARInvoice i = graph.Document.Current;
i.InvoiceNbr = cExt.CustomerPO;
graph.Document.Update(i);
graph.Actions.PressSave();
}

Returning an Object[][] gives NullPointerException

I have an Access database which I need to retrieve all fields except the first and last and display it in a JTable. Everything works perfectly fine when I create my Object[][] but when i return it, i get a NullPointerException. I tried to find where there could be a null value in the database by printing the whole object out but that works fine and no values are null. Why would returning the Object[][] give me a NullPointerException and how can i fix it?
the stack trace is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
public Object [] [] SetTrainingLogTable() throws SQLException
{
DatabaseConnection connection = new DatabaseConnection();
//Retrieves all the data from the TrainingLog table
ResultSet resultset = connection.SelectStatements("SELECT * FROM TrainingLog");
//Retrieves the number of entries
ResultSet numberofworkouts = connection.SelectStatements("SELECT COUNT(*) FROM TrainingLog");
int count = numberofworkouts.getInt(1);
number = count;
String[][] table = new String [count] [6];
//Number to incriment for while loops
int row = 0;
String date = "";
while(row<count)
{
date = resultset.getString(2);
table [row][0] = calculate.RefineDate(date);
table [row][1] = resultset.getString(3);
table [row][2] = resultset.getString(4);
table [row][3] = resultset.getString(5);
table [row][4] = resultset.getString(6);
table [row][5] = resultset.getString(7);
resultset.next();
row++;
}
Object[][] data = table;
connection.close();
return data;
}
I ran a debugger and it only gives the error when the return line is run.
It's best to post the stack trace and tell which line is raising the error. However, the typical way of writing such code is:
Connection con = ...;
Statement st = ...;
ResultSet rs = ...;
while (rs.next()) {
// ...
}
The result set starts out pointing before the first row. rs.next() returns whether there is a next row, and advances to it if it exists. Can you rewrite it in that style?
Other suggestions:
Can you create an actual object type instead of using Object[] to store the data from each row? Call it Workout.
Can you use a List<Workout> instead of your Object[][]?
Is the date stored in the database as a SQL DATE or TIMESTAMP? Then, don't convert it to a Java String: use java.sql.Date or java.util.Date. At work, I have a large old program that uses strings for dates, and it uses different formats to convert the values at different times. It's pretty miserable.
Don't use SELECT *. Give the names of the columns to return. Use the rs.getString("column_name") syntax.
There's no need to set one variable to the returned table and immediately set another variable to it.
Closing the connection or statement should be done in a finally block, or by try-with-resources.

Resources