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

  • Hello Lavanya,

    I do not see you saving the record

    try adding $record ->save(false);

    R

  • Hi Delsugar,

    I have tried the same but no luck. We are not getting the result as expected.

    Thanks,

    Lavanya

  • Hi Lavanya,

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

    in sugarcrm.log?

    foreach($beanList as $record )
      //I THINK YOU NEED TO INSTANTIATE NEW TASKS
      $focus = new Tasks();
      $focus->retrieve($record['id']);

      //

      $focus->description = 'Lavanya';

      //

      $focus ->update_date_modified = false;
      $focus ->update_modified_by = false;
      $focus ->tracker_visibility = false;
      $focus ->in_workflow = false;
      $GLOBALS['log']->fatal('Scheduled tasks record updated successfully: '.$focus->id);
      $focus->save(false);
    }

  • Hi Lavanya,

    Or check this one out. 

    if(!empty($result)) {
    foreach($result as $id_arr) {
    //get the bean corresponding to the matching id
    $b = BeanFactory::retrieveBean('Cases', $id_arr['id']);
    if(!empty($b)){
    // These lines prevent the modified date and user from being changed.
    $b->update_date_modified = false;
    $b->update_modified_by = false;
    $b->tracker_visibility = false;
    $b->in_workflow = true;
    $b->save();
    $cnt++;
    }
    }
    }
    //I don't think scheduler likes printing... best to put your notice in the sugar log
    //you can change info to fatal to have it print in the log for testing
    //then change it back to info or debug when you're done testing.
    $GLOBALS['log']->info("Finished updating: $cnt records.\n<br>");
    //return true for completed
    return true;
    }

    Custom scheduler job help needed 

  • Hi delsugar,

    Thanks for your answers. We have tried the solution mentioned in the above post but no luck.

    Thanks & regards,

    Lavanya

  • delsugar,

    We have tried to print the values in the Sugar log. We have observed that, in the below line,

    $moduleBean = BeanFactory::getBean('Tasks');

    we have failed to get Tasks module bean but got the module beans of Calls and Cases(replaced Tasks with Calls and Cases in above code).

    Please help on how to get Tasks module beans using $moduleBean = BeanFactory::getBean('Tasks') or any other method to get Tasks module records?.

    Thanks in advance,

    Lavanya

  • Hello Lavanya, 

    See documentation http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.9/Data_Framework/Models/BeanFactory/in… 

    Try below:

    //Create bean and save bean
    $bean = BeanFactory::newBean(‘Tasks’);
    $bean->name = ‘Lavanya MR’;
    return $bean->save();

    Example: 

    $task = BeanFactory::newBean('Tasks');
    $task->name = $data['name'];
    $task->date_due = $data['date_due'];
    $task->priority = $data['priority'];
    $task->parent_type = $bean->parent_type;
    $taskBean->status = $data['status'];
    $task->description = $data['desc'];
    $task->parent_id = $bean->parent_id;
    $task->assigned_user_id = $data['assigned_user_id']; 
    $task->save();

  • 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