Question
How to remove all LineItems except the first one?
Answer
The following script will remove all instances except the first one:
if (Document.HasField("Invoice Layout\\LineItems"))
{
while (Document.Field("Invoice Layout\\LineItems").Items.Count>1)
{
int itemsCount = Document.Field("Invoice Layout\\LineItems").Items.Count;
Document.Field("Invoice Layout\\LineItems").Items.Delete(itemsCount-1);
}
}
Please use such script in Event Handler "After Document State Changed" (Project > Project Properties > Event Handlers, choose the After Document State Changed section, and press Edit Script):
Comments
2 comments
Jose Ignacio Garcia Velazquez
Hi Ivan,
I tried to use your code in the Tab Event Handlers as you mention, but in my case, I want to remove one item considering two things inside of the rows:
1- If the ValueName is empty.
2-If the Value is empty or if this one is not a number.
Do you know if my logic in the next code is correct or something is missing?
for(int i = 0;i<Document.Field("Invoice Layout\\LineItems").Items.Count;i++){
IField valname = Document.Field("Invoice Layout\\LineItems\\ValueName").Items[i];
IField val = Document.Field("Invoice Layout\\LineItems\\Value").Items[i];
if(string.IsNullOrEmpty(valname.Text)){
bool flag = int.TryParse(val.Text, out i);
if(string.IsNullOrEmpty(val.Text) || !flag){
Document.Field("Invoice Layout\\LineItems").Items.Delete(i);
}
}
}
Thank you!
Archit Agrawal
HI Jose Ignacio Garcia Velazquez,
Probably you would have already got this working but just for FYI. You might get error saying "No such field found with name "Invoice Layout\LineItems\ValueName" when you run your code. Below code should work in your case.
int lineItemCount = Document.Field("Invoice Layout\\LineItems").Items.Count ;
string valname = string.Empty;
string val = string.Empty;
for(int i = 0;i<lineItemCount;i++){
valname = Document.Field("Invoice Layout\\LineItems").Items[i].Field("ValueName").Text;
val= Document.Field("Invoice Layout\\LineItems").Items[i].Field("Value").Text; if(string.IsNullOrEmpty(valname)){
bool flag = int.TryParse(val, out i);
if(string.IsNullOrEmpty(val) || !flag){
Document.Field("Invoice Layout\\LineItems").Items.Delete(i);
}
}
}
Please sign in to leave a comment.