cancel
Showing results for 
Search instead for 
Did you mean: 

Deleting only some rows (not all) from product tree with DI API

mucipilbuga
Participant
0 Kudos

Hi,

I need to some rows from product tree. Because I have Item rows and Resource rows. I want to delete only ITEM rows below code:

If vProdTree.GetByKey(agacKod) Then

For i As Integer = 0 To vProdTree.Items.Count - 1


vProdTree.Items.SetCurrentLine(i)

If vProdTree.Items.ItemType = SAPbobsCOM.ProductionItemType.pit_Item Then

Try
vProdTree.Items.Delete()

Catch ex As Exception
Console.WriteLine("Detay silinmedi? " & ex.Message & "--" & vCmp.GetLastErrorDescription & "-" & vCmp.GetLastErrorCode)

End Try


End If

Next


If vProdTree.Update() = 0 Then

Console.WriteLine(agacKod & " güncellendi...")

Else

MsgBox("Ürün ağacı detayı silinemedi!" & Chr(13) & "Sistem cevabı: " & vCmp.GetLastErrorDescription & "-" & vCmp.GetLastErrorCode, MsgBoxStyle.Critical, "Hata!")
Exit Sub

End If

End If

 

But I think when I delete row, index is changing in " vProdTree.Items.Count - 1" then I am getting error "Invalid Row"?!

How can I solve it?

 

Regards,

Mucip:)

 

Accepted Solutions (1)

Accepted Solutions (1)

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert

Hi mucipilbuga,

Yes, the index will be changed.

In my opinion, you should start from the last row. The following should work fine:

 

SAPbobsCOM.ProductTrees oPTs = (SAPbobsCOM.ProductTrees)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oProductTrees);
oPTs.GetByKey("A00001");

for (int i = oPTs.Items.Count - 1; i >= 0; i--)
{
    oPTs.Items.SetCurrentLine(i);

    var ItemCode = oPTs.Items.ItemCode;

    if (oPTs.Items.ItemType == ProductionItemType.pit_Resource)
    {
        oPTs.Items.Delete();
    }
}

 

Hope it helps!

Kind regards,

ANKIT CHAUHAN

SAP Business One Support

mucipilbuga
Participant
0 Kudos

Dear Ankit CHAUHAN,

Thanks a lot. I did not think like yours way. Really good.

But I did it my way like below 🙂 I decreased -1 on every delete step.

 

Dim sallamaKademe As Integer = 0

For i As Integer = 0 To vProdTree.Items.Count - 1

vProdTree.Items.SetCurrentLine(i - sallamaKademe)

Console.WriteLine(vProdTree.Items.ItemCode & " işleniyor... " & i)

If vProdTree.Items.ItemType = SAPbobsCOM.ProductionItemType.pit_Item Then

Try
vProdTree.Items.Delete()
sallamaKademe += 1

Catch ex As Exception
Console.WriteLine("Detay silinmedi? " & ex.Message & "--" & vCmp.GetLastErrorDescription & "-" & vCmp.GetLastErrorCode)

End Try


End If

Next

Regards,

Mucip:)

 

Answers (0)