An unhandled exception has occured in function 'MoveNext' - acumatica

Does anyone knows what is the cause of this error message? Here's my code that I think that triggers that error. (This is from our customization project), the error triggers when I select more than 1 item.
foreach (InventoryItem line in soinvlineview.Cache.Cached)
{
if (line.Selected == true)
{
StyleColorSelected newline = PXCache<StyleColorSelected>.CreateCopy(styleselected.Insert(new StyleColorSelected()));
newline.InventoryID = line.InventoryID;
newline = PXCache<StyleColorSelected>.CreateCopy(styleselected.Update(newline));
styleselected.Update(newline);
}
}

First things first, the CreateCopy method was mandatory with ver. 4.0 and earlier. Starting ver. 4.1, you don't need to use it at all. This is how your code can be simplified:
foreach (InventoryItem line in soinvlineview.Cache.Cached)
{
if (line.Selected == true)
{
StyleColorSelected newline = styleselected.Insert(new StyleColorSelected());
newline.InventoryID = line.InventoryID;
styleselected.Update(newline);
}
}
I suspect, your custom StyleColorSelected DAC either has no key fields specified or attributes used on the key field(s) do not generate unique values - this results in no record inserted in PXCache (Insert method returns null instead of the inserted value) and most likely causes reported unhandled exception to occur in function 'MoveNext'. Could you please double-check implementation of the StyleColorSelected DAC and also update you code as follows to verify that key fields have unique values set and record is always inserted into PXCache.
foreach (InventoryItem line in soinvlineview.Cache.Cached)
{
if (line.Selected == true)
{
StyleColorSelected newline = new StyleColorSelected();
// if necessary assign unique values to key field(s) here
newline = styleselected.Insert(newline);
if (newline == null) throw PXException("StyleColorSelected was not inserted in the cache!");
newline.InventoryID = line.InventoryID;
styleselected.Update(newline);
}
}

Related

Angular - TypeError Cannot set _id to Null Value

Has a transaction function that worked in first pass and at 2nd pass, got "TypeError: Cannot set _id to Null value. Both passes were to create a new transaction. Besides, system seemed to indicate that there was value for variable that was being used to assign to_id. In this case,
print"this.selectedLeave_id" and saw value as expected. However, Angular right away in next statement complaining that Null value was set to "this.SLTran.leave_id".
Below pls find code and any help is appreciated.
onSubmitLeaveTran()
{
if (this.selectedLeaveTran_id != 0) // means first loaded all trans and then select from table to modify
{
this.sLTran = this.tempSLTran; // tempSLTran was from row selected to be modified
this.sLTran.leaveType = this.tempLeaveType; // from dialog box edited data
this.sLTran.leaveStartDate = this.tempLeaveStartDate; // from dialog box edited data
this.sLTran.leaveEndDate = this.tempLeaveEndDate; // from dialog box edited data
this.sLTran.numDaysRequested = this.tempNumDaysRequested; // from dialog box edited data
console.log('2-2.5 inside onSubmit Leave Tran for update :',this.sLTran);
this.staffLeaveDataSvc.updateSLTran(this.sLTran).subscribe((sLTran:staffLeaveTran) => {this.sLTran = sLTran});
}
else
{ // a new tran
console.log('2-2.4 inside onSubmit Leave Tran selectedLeave_id for new tran:',this.selectedLeave_id);
this.sLTran.leave_id = this.selectedLeave_id; // first established the leave_id associated with this new tran
this.sLTran.leaveType = this.tempLeaveType;
this.sLTran.leaveStartDate = this.tempLeaveStartDate;
this.sLTran.leaveEndDate = this.tempLeaveEndDate;
this.sLTran.numDaysRequested = this.tempNumDaysRequested;
this.staffLeaveDataSvc.addSLTran(this.sLTran).subscribe(
(sLTran:staffLeaveTran) => {
this.sLTran = sLTran
});
}
};

Office JS issue with recognising ListItems

I'm trying to add a paragraph at the end of the document and escape the possibility of the newly added paragraph to be added inside a list (if the document is ending with a list).
I have the following code:
let paragraph = paragraphs.items[paragraphs.items.length - 1]
let p = paragraph.insertParagraph('', window.Word.InsertLocation.after)
if (paragraph.listItemOrNullObject) {
p.detachFromList()
p.leftIndent = 0
}
The following happens: if there is a ListItem, the code works. If not, it breaks inside the if condition, like I wrote paragraph.listItem.
Shouldn't this be used like this?
EDIT - error thrown:
name:"OfficeExtension.Error"
code:"GeneralException"
message:"GeneralException"
traceMessages:[] 0 items
innerError:null
â–¶debugInfo:{} 4 keys
code:"GeneralException"
message:"GeneralException"
toString:function (){return JSON.stringify(this)}
errorLocation:"Paragraph.detachFromList"
the issue here is that the *.isNullObject methods/properties does not return a regular js 'null' object, but a NullObject (a special framework type of null).
check out this code i rewrote it i think in a more efficient way. excuse my js, you can port it to ts.
hope this helps.
Word.run(function (context) {
var listI = context.document.body.paragraphs.getLast().listItemOrNullObject;
context.load(listI);
return context.sync()
.then(function () {
if (listI.isNullObject) { // check out how i am validating if its null.
console.log("there is no list at the end")
}
else {
context.document.body.paragraphs.getLast().detachFromList();
context.document.body.paragraphs.getLast().leftIndent = 0;
return context.sync();
}
})
})
listItemOrNullObject will return a null object if it isn't a ListItem. Conceptually you're if is asking "if this is a list item or it isn't a list item" which effectively will also return true.
It is failing here you are attempting to detach from a non-existent list. I would take a look at isListItem. This will tell you specifically if the paragraph is a ListItem so you only execute p.detachFromList() when in fact it is part of a list.

How i can get latest record by using FirstOrDefault() method

Suppose i have 2 records in data base
1) 2007-12-10 10:35:31.000
2) 2008-12-10 10:35:31.000
FirstOrDefault() method will give me the first record match in sequence like 2007-12-10 10:35:31.000 but i need the latest one which is 2008-12-10 10:35:31.000
if ((from value in _names where value != null select value.ExpiryDate < now).Any())
{
return _names.FirstOrDefault();
}
You can use:
return _names.LastOrDefault();
However, your if just sends another unnecessary query (and it is a wrong query too). If you don't have any record, LastOrDefault and FirstOrDefault will return null. You can use something like this to improve the code:
var name = _names.LastOrDefault();
if(name != null)
{
return name;
}
// other code here
If you really want to use FirstOrDefault, you should order descending, like:
var name = _names.Where(n => n.ExpiryDate < now).OrderByDescending(n => n.ExpiryDate).FirstOrDefault();

Using LDAP filter to find all results. Getting error message

DirectoryEntry testAD = new DirectoryEntry();
DirectorySearcher search = new DirectorySearcher(testAD);
StringBuilder add = new StringBuilder();
search.PropertiesToLoad.Add("mail");
search.Filter = "(&(objectClass=user))";
foreach (SearchResult SearchAll in search.FindAll())
{
DirectoryEntry de = SearchAll.GetDirectoryEntry();
add.Append(de.Properties["mail"].Value.ToString()); // error message here
}
PrefixDescription.Text = add.ToString();
I'm trying to find all emails first as a test and then all information (first name, last name, etc) and list it in a text box using a LPAR filter but I keep getting this error message when I run the app:
Object reference not set to an instance of an object.
Well, you're enumerating users - but you have no guarantee that the resulting user will have an e-mail address! You need basic "programming 101" error prevention:
.....
foreach (SearchResult result in search.FindAll())
{
// this is totally unnecessary - the "SearchResult" already *contains* all
// the properties you've defined in your "PropertiesToLoad" collection!
// DirectoryEntry de = SearchAll.GetDirectoryEntry();
if(result.Properties["mail"] != null && result.Properties["mail"].Count > 0)
{
add.Append(result.Properties["mail"][0].ToString());
}
}
With this extra check, you avoid the Object reference not set.... error...

Element is null in foreach loop

I have a problem that I can not understand.
There is a for-each loop, and the debugger tells me there are values in the list, but the concrete body-loop says the current element is null.
How can that be?
public void test(){
List cs = ["a"];
for(String c:cs){
print c; // but c is null(sais the debugger)! The console shows "null".
}
}
Edit: Another occourence is:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = subFoobaries.findAll { it.numericId in electedSubFoobaryIds}
return electedSubFoobaries
}
Do throw an NPE
But:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = []
for(StaticSubFoobary it:subFoobaries)
if(it.numericId in electedSubFoobaryIds)
electedSubFoobaries.add(it)
return electedSubFoobaries
}
Doesn't!
but the concrete body-loop says the current element is null
No it doesn't. If you run this code in the Groovy console, the assertion passes:
List cs = ["a"];
for(String c:cs){
assert c == 'a'
}
Conversely, if you run this code in the console the assertion fails
List cs = ["a"];
for(String c:cs){
assert c == null
}
Which conclusively proves that the first element of the list is "a" and not null
Trying to cast the GroovyString to String could be the problem. Try with a single quoted string literal.
I Finally checked out the project again and build it like i always did before.
The problem is gone away, not sure why but it works now!
I just have to copy all my changes to the new workspace and continue development.
Let this workaround be part of experiences.

Resources