Workflow: Date conditions

Hi Sir,
i create workflow same as it you explain,after that when i create contract module then it work and show in Notification bar,but i want to show those contract list that expired just after 60 days.

So i apply condition as:- Module:-Contracts,Field:-End date,Operator:-less than,Type:-Date,Value:-Now +60 days

but it not filter the value and not get notification in notification bar

but i remove the condition then it work only on save and new record when i create it,it show only new record name.

please help me to filter the value

Actually i want to notify that type of contract value that expired just after 2 month,if any contract expired just after 2 month then it get notification in notification bar with their name.

Hi,
You should set the execution type to scheduler, so that a manual save is not necessary (requires that you put the cron.php to the cron/scheduled tasks of your webserver).
Secondly, you’ll need a condition that prevents the notification from being created each minute after the 60 days (e.g. a checkbox that you set while creating the notification).
Last but not least, reduce the 60 days to something very small so that you can test your workflow before using it globally.

Hi,
Thank you for your response
Yes i checked cron job all will be set.
Can you please help me writing the condition.
I want to alert when my contract get expired before 2 month
Can you check the condition:-Module:-Contracts,Field:-End date,Operator:-less than,Type:-Date,Value:-Now +60 days.

And i apply calculate for 1st action and create record for 2nd action same as you describe.
But Alert will not come.

Hi,
this is my test setup:

  • please not that I checked “repeated runs”: this is necessary for the workflow to validate this record more than once
  • my field (“aufgelöst am”) is a datefield, and the workflow becomes active, if the stored value is greater/equal to “today minus 60 days” (not sure, I assume you need to set it to + 60 days).
  • I did not test with additional settings, but I described the steps above.

E: And I splitted our conversation into a new thread, it was not related to original topic.

Hi,
Thank you for your reponse.
I set an alert successfully but alert->save() method save my alert again and after 1-2 second time interval,How to stop that one and how to apply time interval when my alert notify.

Thanks,
Vishal Raj

Are you using logic hooks to create the alert? Or workflows?

Hi,
I am using logic hook.

then it is maybe best if you share the code you’re testing at the moment, along with the filenames/filepaths in use.

Hi,
See my code:—

function f(){
     
      //  $alert.clear();      
        $servername="localhost";
        $username="root";
        $password="";
        $database="suitecrm";
        $conn1 = new mysqli($servername, $username, $password, $database);
       
        if ($conn1->connect_error) {
          die("Connection failed: " . $conn1->connect_error);
        }
     
         $sql = "SELECT name,end_date from aos_contracts";
            $result = $conn1->query($sql);
     
            $startdate = date('Y-m-d H:m:s', time() + 86400 * 60);
            if ($result->num_rows > 0) {
               // output data of each row
             
               while($row = $result->fetch_assoc()) {
                $re = $row["name"];
                $enddate = $row["end_date"];
                if($enddate <= $startdate){
                  $alert = BeanFactory::newBean('Alerts');
                  $url = 'index.php?module=AOS_Contracts&action=DetailView&record=';
                  $alert->description =  $re .  'has been expired after 60 days please renew it !';
                  $alert->url_redirect = 'index.php';
                  $alert->target_module = 'AOS_Contracts';
                  $alert->assigned_user_id = '1';
                  $alert->type = 'info';
                  $alert->is_read = 0;
                  $alert->save();
       
                }//End of if block
                   
           }//End of While block
              
     }//End of if block
}//End of function
f();

Hi,
multiple issues here:

you should avoid code like this. There are framework-friendly ways of using the existing db-connection if you really need to, but initiating db-access like this will cause issues (e.g.: what happens if the credentials change? you need to remember all code parts that work this way…).
And secondly: instead of using queries, you can utilize the bean framework. this makes your code easier to read and to maintain.

Biggest issue in my mind:
you are writing that you use logic hooks. is the shown code an after-save or before-save-hook? In that case each record triggers the execution of this method, and within the method, you query all records that require a notification and create an alert for each. within a record-based hook, you should only generate alerts for the current record. this is probably why you have so many duplicated warnings to deal with.

From what I’ve understood, I would not use hooks or workflows to accomplish your task. I have the impression you are looking for a scheduled job:

these jobs are scheduled, so they run e.g. every evening. In such a scenario, your code makes more sense (“create alerts each evening for all qualified records”).

Hi,
Can you guide me, how to clear the old alert notification data.
Actually my code give correct data, like it give 7 contract that expire before 2 month from current date, but only one issue is there after some few seconds again 7 new data will be append to the old one after that again 7 new data added , data again and again added, how i clear the old one.
My function run only one time, i apply count to see how many times my function call.
please help me you have a code.

Thanks,
Vishal Raj

yep, but you need to understand first how hooks/scheduler work in order to write them properly. I see the following issues:

  • I don’t know how the current code works (you didn’t tell what kind of hook you are using), so it is hard to estimate why you have multiple warnings.
  • my assumption: you are using a save-hook for generating all warnings for all contracts. thats why one contract-save triggers the creation of multiple other alerts.
  • I would expect this kind of code rather in a scheduler (one handler that creates warnings on a regular basis).

In all cases, you still have to consider how to prevent the system from creating too many warnings (e.g.: your renewal day was last week, your code was executed somehow each day → 7 warnings per record would be created). So either you store within the contract some hidden checkbox (“needs warning”) to encapsulate this condition, or you query first if the current record already has unread warnings (skip it in this case).

Hi,
I am using $alert->save() logic hook.

That is just a small part of your code, but it does not state when the method is being called.

Hi,
path of my code:-modules/Alerts/Alerts.php

code:–
class Alert extends Basic

{

public $new_schema = true;

public $module_dir = 'Alerts';

public $object_name = 'Alert';

public $table_name = 'alerts';

public $importable = false;

public $disable_row_level_security = true ; // to ensure that modules created and deployed under CE will continue to function under team security if the instance is upgraded to PRO

public $id;

public $name;

public $date_entered;

public $date_modified;

public $modified_user_id;

public $modified_by_name;

public $created_by;

public $created_by_name;

public $description;

public $deleted;

public $created_by_link;

public $modified_user_link;

public $assigned_user_id;

public $assigned_user_name;

public $assigned_user_link;

public $is_read;

public $alert;



public $count = 0;



/**

 * @var string

 */

public $reminder_id;

public function __construct()

{

    parent::__construct();

}

public function bean_implements($interface)

{

    switch ($interface) {

        case 'ACL': return true;

    }

    return false;

}

}

function f(){

    $servername="localhost";

    $username="root";

    $password="";

    $database="suitecrm";

    $conn1 = new mysqli($servername, $username, $password, $database);

   

    if ($conn1->connect_error) {

      die("Connection failed: " . $conn1->connect_error);

    }

 

     $sql = "SELECT name,end_date from aos_contracts";

        $result = $conn1->query($sql);

        $startdate = date('Y-m-d H:m:s', time() + 86400 * 60);

        if ($result->num_rows > 0) {

         

           // output data of each row

         

           while($row = $result->fetch_assoc()) {

           

            $re = $row["name"];

            $enddate = $value["end_date"];

            if($enddate <= $startdate){

              $alert = BeanFactory::newBean('Alerts');

              $url = 'index.php?module=AOS_Contracts&action=DetailView&record=';

              $alert->description =  $re .  'has been expired after 60 days please renew it !';

              $alert->url_redirect = 'index.php';

              $alert->target_module = 'AOS_Contracts';

              $alert->assigned_user_id = '1';

              $alert->type = 'info';

              $alert->is_read = 0;

              $alert->save();

             

              // if( $alert->is_read == 0){

              //   $alert->save();

              // } else if ($alert->is_read != 0){

              //   $alert->clean();

              // }else{

              //   echo "Nothing happen";

              // }

           

            }//End of if block

               

       }//End of While block

          

 }//End of if block

}//End of function

// f();

// mysqli_close($conn1);

and where do you invoke the method? there has to be some kind of definition when to execute your method. For hooks, there is usually an array that includes the file path, the class name and the methods name.