Removing Objects from a CollectionTo remove items from a collection:
ApiCollection.clear() removes all items from this collection. The objects are not physically deleted. The typical application can be deleting persons from a certain department. This relation is stored in “persons” field that the department object has. Having done that, the persons no more belong to this department but remain in the system.
ApiCollection.remove(BO) removes the first instance of a particular business object from this collection. Remove doesn’t allow you to pass an order number of an item as a parameter. As a result, you can’t delete an item, but only a particular business object. To bypass this limitation and enable removing items according to their order number, use the following combination of get(int) and remove ApiCollection.remove(ApiCollection.get(int)) get returns an object by its order number, which you immediately pass as a parameter for calling remove. Example - Removing items from collections We assume that you have run Valuemation and logged in correctly before starting this code. oVM variable now holds Application object. See Login Syntax for the section of the opening code. oCollection.remove(oCollection.get(0)) Comment: We assume that we hold an existing collection, oCollection, containing some 3 items. This may represent a queue of waiting requests. Now, the first queued request is solved so we want to remove it, which can be easily carried out by calling oCollection.remove. Example - Task Manager (working with collections) We assume that you have run Valuemation and logged in correctly before starting this code. oVM variable now holds Application object. 'A queue of tasks and their solving. The "Project" BOType is used since it has an appropriate ’structure. 'creates a transaction, gets the "Project" BOType and creates a collection of all projects. Set oTrans = oVM.createTransaction Set oBOT = oVM.getBOTypes.find("Project") Set oColl = oBOT.createFilter(oTrans,"") 'the main body of this script. The cycle provides user with selection of particular function and ’procedures. Entering "0" quits the cycle. Do cmd = 0 cmd = MainMenu if not IsNumeric(cmd) then cmd = 666 Select case cmd case 1 AddTask case 2 CompleteTask case 3 FindTask case 4 PrintTask(1) case 5 PrintTask(InputBox("Insert index of task (1.." & oColl.length_ & "):")) End Select Loop While cmd<>0 'function implementing the main menu dialogue Function MainMenu() sTmp = "There are " & oBOT.countObjects(oTrans,"")_ & " tasks waiting to complete." & vbNewLine & vbNewLine sTmp = sTmp & "Add command number and press OK:" & vbNewLine sTmp = sTmp & "1 - Add task" & vbNewLine sTmp = sTmp & "2 - Complete task" & vbNewLine sTmp = sTmp & "3 - Find task" & vbNewLine sTmp = sTmp & "4 - Print current task" & vbNewLine sTmp = sTmp & "5 - Print task by index" & vbNewLine sTmp = sTmp & vbNewLine & "0 - Exit" & vbNewLine MainMenu = InputBox(sTmp,"Command") End Function 'a procedure enabling user to insert a task - creates a new BO, fills its "projectdescription" field with ’the given description. Adds the BO to the last position in the collection and commits the transaction, ’allowing for further work Sub AddTask sTmp = InputBox("Insert task description and press OK","Task Description") Set oBO = oBOT.createBO(oTrans,true) oBO.getBOFields.find("projectdescription").setValue sTmp oColl.add oBO oTrans.doCommitResume End Sub 'a task is solved - removes the first item from the queue, removes the BO and commits the ’transaction, allowing for further work Sub CompleteTask if oColl.isEmpty then MsgBox "Task queue is empty.",vbExclamation else Set oBO = oColl.item(0) oColl.remove oBO oBO.remove oTrans.doCommitResume MsgBox "Task was removed from queue." end if End Sub 'finds a task - looks for a "Project" BO with description matching the user entry, prints the position ’the task was found at. Sub FindTask sTmp = InputBox("Insert part of task description and press OK","Task Search") Set oTmpColl = oBOT.createFilter(oTrans,"projectdescription like '%" &_ sTmp & "%'") if oTmpColl.isEmpty then MsgBox "No such task found!",vbExclamation else position = oColl.indexOf(oTmpColl.item(0)) + 1 MsgBox "Task was found at position " & position end if End Sub 'prints a description of the task on a given position Sub PrintTask(i) if not IsNumeric(i) Then Exit Sub i = CInt(i) if (i>oColl.Length) or (i<1) then MsgBox "No such task found!",vbExclamation else MsgBox "Description: " & vbNewLine _ & oColl.item(i- 1).getBOFields.find("projectdescription").getValue end if End Sub | |||||