$newdep->fullname = 'Agents';
         $newdep->shortname = 'EXT';
         $newdep->usermodified  = $USER->id;
         $newdep->timecreated = time();
         $newdep->type = 1;
         if ($newdep->parentid == 0) {
             $newdep->depth = 1;
             $newdep->path = '';
         } else {
             /* ---parent item must exist--- */
             $parent = $DB->get_record('local_costcenter', array('id' => $newdep->parentid));
             $newdep->depth = $parent->depth + 1;
             $newdep->path = $parent->path;
         }
         /* ---get next child item that need to provide--- */
         if (!$sortorder = $costcenter->get_next_child_sortthread($newdep->parentid, 'local_costcenter')) {
             return false;
         }
 
         $newdep->sortorder = $sortorder;
         $costcenters = $DB->insert_record('local_costcenter', $newdep);
          // used to inserting costcenterinfo to context(mapping to costcenter context level)
         $context = context_costcenter::instance($costcenters, MUST_EXIST);
         
         $DB->set_field('local_costcenter', 'path', $newdep->path . '/' . $costcenters, array('id' => $costcenters));   
         $context->mark_dirty();
         $data->costcenterid = $costcenters;
         $data->zone  = 'Agents';
         $data->zone_code  = 'EXT';
     } else {
         $data->costcenterid = $department->id;