Fields are not updating inTasks module using scheduled custom job

Hi all,

I have scheduled a custom job in Tasks module to update fields everyday. Scheduled custom job is running successfully but fields were not updating. Previously i have scheduled custom job for Calls module using same method. Custom job is running as expected but not for the Tasks module. Can any one please help on this?

Please refer below code for more information:

1. custom_job_task.php

<?php

$job_strings[] = 'UpdateNumberOfDays';

function UpdateNumberOfDays()
{
    $moduleBean = BeanFactory::getBean("Tasks");
    $beanList = $moduleBean->get_full_list();
    if( $beanList != null )
    {
        foreach($beanList as $record )
        {
            if((strcmp($record->id,"57f7f222-f526-11e7-b198-02c6991e5099"))==0)
            {
                $test="Lavanya";
                $sql = "update tasks set tasks.description ='".$test."' where tasks.id='".$record->id."'";
                $result = $moduleBean->db->query($sql, true);
                
                $record ->update_date_modified = false;
                $record ->update_modified_by = false;
                $record ->tracker_visibility = false;
                $record ->in_workflow = false;
                $GLOBALS['log']->fatal('Scheduled tasks record updated successfully: '.$record->id);
                $GLOBALS['log']->fatal('Query is: '.$sql);
                $GLOBALS['log']->fatal('Result is: '.$result);
            }
        }
    }
    return true;
}
 
?>

2. en_us.custom_job_task.php

<?php

$mod_strings['LBL_UPDATENUMBEROFDAYS'] = 'UpdateNumberOfDays';
?>

Thanks & regards,

Lavanya

Parents Reply Children
  • delsugar,

    Thanks for your solution. According to above solution, it will create a new task record if we use $bean = BeanFactory::newBean(‘Tasks’);.

    But i want to update some of the existing task records based on some conditions. I need to retrieve all existing records. Is there any other way?.

    Thanks,

    Lavanya

  • Hello Lavanya,

    I do something very similar for the accounts module in a custom scheduled tasks.

    Have a look at my business logic I tried to replace it with the Tasks module and fields.

    $query = new SugarQuery();
    $query->select(array('id','name', 'status'));
    $query->from(BeanFactory::getBean('Tasks'), array('team_security' => false));
    $query->where()->equals('status','Completed');
    $rows = $query->execute();
    //
    foreach ($rows as $row) {

    $GLOBALS['log']->fatal(' Tasks completed id='.$row['id'].' name='.$row['name'].' Status='.$row['status']);

     if((strcmp($record->id,"57f7f222-f526-11e7-b198-02c6991e5099"))==0)

     {

      $record= new Task();    //  $record= new Tasks(); 
      $record->retrieve($row['id']);
      $record->description= 'Lavanya';
      $record->save(false);

      $GLOBALS['log']->fatal('Scheduled tasks record updated successfully: '.$record->id);

     }
    }

  • delsugar,

    I appriciated all your solutions. I have explored similar solution for this issue and implemented the same. it worked for me. Please refer below script for more information,

    <?php

    $job_strings[] = 'UpdateNumberOfDays';

    function UpdateNumberOfDays()
    {
        $sql = "SELECT id,name FROM tasks WHERE deleted = 0";
        $result = $GLOBALS['db']->query($sql);

        while($row = $GLOBALS['db']->fetchByAssoc($result) )
        {
        if((strcmp($row['id'],"57f7f222-f526-11e7-b198-02c6991e5099"))==0)
        {
            $GLOBALS['log']->fatal('Task id found'.$row['id']);
            $sql = "update tasks set tasks.description ='Lavanya' where tasks.id='".$row['id']."'";
            $result = $GLOBALS['db']->query($sql, true);
        }
        }
        return true;
    }
     
    ?>

    OR

    <?php

    $job_strings[] = 'UpdateNumberOfDays';

    function UpdateNumberOfDays()
    {
        $sql = "SELECT id,name FROM tasks WHERE deleted = 0";
        $result = $GLOBALS['db']->query($sql);

        while($row = $GLOBALS['db']->fetchByAssoc($result) )
        {
        if((strcmp($row['id'],"57f7f222-f526-11e7-b198-02c6991e5099"))==0)
        {
            $GLOBALS['log']->fatal('Task id found'.$row['id']);
            
            $record= new Task();
            $record->retrieve($row['id']);
            $record->description= 'Lavanya';
            $record ->update_date_modified = false;
            $record ->update_modified_by = false;
            $record ->tracker_visibility = false;
            $record ->in_workflow = false;
            $record->save(false);
            $GLOBALS['log']->fatal('Scheduled tasks record updated successfully: '.$record->id);
        }
        }
        return true;
    }
     
    ?>

    Thanks for your support.

    Lavanya