Work with Collections
Subobjects such as Tasks and Attachments are organized into collections. To work with attributes in a collection, use the accessor and iterate through the records or rows in the collection.
Let's look at how to work with collections. 
        - Get task attributes
 - Set task attributes
 - Add a task to change order
 
Get Task Attributes
Tasks are stored as a collection and therefore to access them you must iterate
                through them. In this case, the accessor is Change
                Task.
            def changeTasks = ChangeTask
    changeTasks.reset()
    while(changeTasks.hasNext()){
        def eachTask = changeTasks.next() 
        println("ActionCode: "+eachTask.ActionCode) 
        println("Assigned By: "+eachTask.AssignedByText)
        println("Assigned On: "+eachTask.AssignedDate)
    }Set Task Attributes
You can update some task attributes by iterating through them, using an "if" statement to access the desired task, and then using set Attribute to set a value.
Let’s look at the code to set task attributes:
                
            def changeTasks = ChangeTask
    changeTasks.reset()
    while(changeTasks.hasNext()){
        def eachTask = changeTasks.next()
        if(eachTask.Name == "Update tasks"){
           eachTask.setAttribute("Description",'This is another new description')
           println("Description: "+eachTask.Description)   
          }
    }Add a Task to Change Order
Let's look at the code to add a task to a change
                order:
            def changeTasks = ChangeTask 
    def newTask = changeTasks.createRow() 
    newTask.setAttribute("Name", "Update the design doc") 
    newTask.setAttribute("RequiredFlag", "N") 
    newTask.setAttribute("CompleteBeforeStatusCode", 6)
    newTask.setAttribute("Description", "Update the design doc to fit the proposed changes.") 
    newTask.setAttribute("AssignedTo", "<username>") 
    changeTasks.insertRow(newTask)