I have been trying to make a background task for email notifications continuously so that the user who has ticket and has resolved it can immediately close the ticket, but when the background task is running nothing happens. Here's the code that I created, please correct it if something goes wrong.
<?phpclassTriggerAutoNotifextendsTriggerOnObject{publicstaticfunctioninit(){$aParams=array("category"=>"core/cmdb,application,grant_by_profile","key_type"=>"autoincrement","name_attcode"=>"description","state_attcode"=>"","reconc_keys"=>array('description'),"db_table"=>"priv_trigger_onautonotif","db_key_field"=>"id","db_finalclass_field"=>"","display_template"=>"",);MetaModel::Init_Params($aParams);MetaModel::Init_InheritAttributes();MetaModel::Init_AddAttribute(newAttributeClassState("state",array("class_field"=>'target_class',"allowed_values"=>null,"sql"=>"state","default_value"=>null,"is_null_allowed"=>false,"depends_on"=>array('state'))));//DisplayListMetaModel::Init_SetZListItems('details',array('description','target_class','filter','state','action_list'));MetaModel::Init_SetZListItems('list',array('finalclass','target_class','description'));//PencarianMetaModel::Init_SetZListItems('standard_search',array('target_class','description'));}}classAutoNotifikasiimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return2;//dalamdetik}publicfunctionProcess($iTimeLimit){echo"Memulai Proses............."."\n";CMDBObject::SetTrackInfo('Otomatis - Mengirim Notifikasi');$aReport=array();$queryTriggerr="SELECT TriggerAutoNotif";$oSearch=DBObjectSearch::FromOQL($queryTriggerr);$oSet=newCMDBObjectSet($oSearch,array(),array());while($oTrigger=$oSet->Fetch()){$sClass=$oTrigger->Get('target_class');$oState=$oTrigger->Get('state');$action_list=$oTrigger->Get('action_list');if(MetaModel::IsValidClass($sClass)){$queryAutoNotif="SELECT '$sClass' WHERE status '$oState' = current_status()";$oSearchToNotify=DBObjectSearch::FromOQL($queryAutoNotif);$oSetToNotify=newCMDBObjectSet($oSearchToNotify,array(),array());while($oItem=$oSetToNotify->Fetch()){$sFilter=trim($oTrigger->Get('filter'));if(strlen($sFilter)>0){$oSearch=DBObjectSearch::FromOQL($sFilter);$oSearch->AddCondition('id',$oItem->GetKey(),'=');$oSet=newDBObjectSearch($oSearch);$bRet=($oSet->Count()>0);}else{$bRet=true;}if($bRet){echo$oItem->Get('name')."respect filter"."\n";$oLinkedActions=$oTrigger->Get('action_list');while($oLink=$oLinkedActions->Fetch()){$iActionId=$oLink->Get('action_id');$oAction=MetaModel::GetObject('Action',$iActionId);$oSearchEvent=DBObjectSearch::FromOQL('SELECT EventNotificationEmail');$oSearchEvent->AddCondition('action_id',$iActionId,'=');$oSearchEvent->AddCondition('object_id',$oItem->GetKey(),'=');$oSetEvent=newDBObjectSet($oSearchEvent);if($oAction->IsActive()&&$oSetEvent->Count()==0){$oAction->DoExecute($oTrigger,$oItem->ToArgs('this'));$aReport[]="Send Notifikasi for ".$oItem->Get('name');}else{//echo"Notification ".$oAction->Get('name')." already send for ".$oItem->Get('name');}}}else{//echo$oItem->Get('name')." don't respect fitler"."\n";}}}else{echo$class." is not a valide class for trigger TriggerOnDurationChange"."\n";}}if(count($aReport)==0){return"No Notification to Process";echo"No Notification to Process"."\n";}else{return"Some Notification were notify on duration \n ".implode("\n",$aStringReport);echo"Some Notification were notify on duration \n ".implode("\n",$aStringReport);}}}?>
thanks, akbar
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, I am using a state of change but I don't really understand, can you please tell me where is the syntax error in my code? and whether in a certain state "resolve" I can create a background task so I can send emails continuously?
Thanks :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
For more details like this,
So I plan to create an extension, where the extension that I will create will work and run in the Background. I want to make an extension where when the user creates a ticket and the ticket has resolved and the user does not close the ticket, an email notification will appear continuously until the user closes the ticket,
I've tried the following:
1. I created the following syntax, to create a process background
<?phpclassAutoNotifimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return3;// Once a day}publicfunctionProcess($iTimeLimit){$aReport=array();$oSet=newDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnState"));while($oNotify=$oSet->Fetch()){$oNotify->Get('state');$aReport[]='Notifikasi sudah terkirim #'.$oNotify->GetKey();}if(count($aReport)==0){return"tidak ada ticket yang resolved";}else{returnimplode('; ',$aReport);}}}
I created the following syntax to create a custom trigger:
but the error "Error: Unknown attribute state from class TriggerOnState appears." looks like this problem because I haven't defined the state atribute,
My question is:
1. How do I connect the background process with a trigger/action so that notifications can be sent automatically and continuously?
2. How do Notifications work on iTop?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have tried to understand what you are saying, what I understand is the following:
1. I use Crud DBObject::AfterInsert To Trigger Notifications,
2. I get an error like this: PHP Fatal error: Uncaught Error: Call to protected method DBObject::AfterInsert() from context
I know, maybe it's an error because I didn't add the "protected function AfterInsert()" syntax, I want to ask, where do I put the syntax? Because I've tried several lines, the error still occurs?
this is my code :
<?phpclassAutoNotifimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return3;//per detik}publicfunctionProcess($iTimeLimit){$aReport=array();$oSet=newDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateEnter"));while((time()<$iTimeLimit)&&$oNotify=$oSet->Fetch()){$oNotify->AfterInsert();$sClass=get_class($oNotify);$sClassList=implode("', '",MetaModel::EnumParentClasses($sClass,ENUM_PARENT_CLASSES_ALL));$oSetTrigger=newDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateEnter AS t WHERE t.target_class IN ('$sClassList')"));while($oTrigger=$oSetTrigger->Fetch()){$oTrigger->DoActivate($oNotify->ToArgs('this'));}return$oNotify->m_iKey;}}}?>
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You are doing a mix of Trigger and BackgroundProcess, I don't understand why you chose this path and I don't think this is a good idea.
Just create a BackgroundProcess that will :
get all objects you want to notify using an OQL query
for each object, send an email (with pure PHP code, no trigger involved)
If you want your custom notification email to be configurable in the admin console, then you should create a new iTop object, a menu entry to show them, and use those objects in your BackgroundProcess code.
Last edit: Pierre Goiffon 2021-06-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I'm confused about your answer
As said before, just forget about notifications, triggers and actions
The background task needs to find all tickets for which a mail needs to be send.
So if I understand correctly your original message, tickets that are in the resolved state ? SELECT Ticket WHERE status = 'resolved'
Then loop on all objects returned by this query, and for each of them send an email.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have tried what you suggested and nothing happens, here is my code:
<?php//Membuat Background TaskclassAutoNotifyimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return30;//setiap detik}publicfunctionProcess($iTimeLimit){CMDBObject::SetTrackInfo("Automatic - Notifikasi ketika Ticket Berstatus Resolved tetapi tidak di Close");$aReport=array();$sSet=newDBObjectSet(DBObjectSearch::FromOQL("SELECT UserRequest WHERE status = ('resolved')"));while($oToNotify=$sSet->Fetch()){$oToNotify->DBUpdate();//I'm confused here? how do I call the notification?}}returnimplode('; ',$aReport);}?>
honestly i'm confused and don't have basic php, i learn from the code i see in source itop then i follow code by code.
Thanks Pierre, if you can help me, i really appreciate you :)
Best Regards
Akbar
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi,,
I have made this code, but an error like this appears, What do you think?
this is my code:
<?php//Membuat Background TaskclassAutoNotifyimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return30;//setiap detik}publicfunctionProcess($iTimeLimit){CMDBObject::SetTrackInfo("Automatic - Notifikasi ketika Ticket Berstatus Resolved tetapi tidak di Close");$aReport=array();$sSet=newDBObjectSet(DBObjectSearch::FromOQL("SELECT UserRequest AS u WHERE u.status = 'resolved'"));while((time()<$iTimeLimit)&&$oNotify=$sSet->Fetch()){$oNotify->Get('ref');$oClass=newDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateEnter AS t WHERE t.target_class IN ('UserRequest') AND state = 'resolved'"));while($oTrigger=$Class->Fetch()){$oTrigger->DoActivate('this');}}}}
<?php//Membuat Background TaskclassAutoNotifyimplementsiBackgroundProcess{publicfunctionGetPeriodicity(){return30;//setiap detik}publicfunctionProcess($iTimeLimit){CMDBObject::SetTrackInfo("Automatic - Notifikasi ketika Ticket Berstatus Resolved tetapi tidak di Close");$aReport=array();$oSet=newDBObjectSet(DBObjectSearch::FromOQL("SELECT UserRequest AS u WHERE u.status = ('resolved')"));while((time()<$iTimeLimit)&&$oNotify=$oSet->Fetch()){$oNotify->Get('ref');$sClass=get_class($oNotify);$sClassList=implode("', '",MetaModel::EnumParentClasses($sClass,ENUM_PARENT_CLASSES_ALL));$oSetTrigger=newDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateEnter AS t WHERE t.target_class IN ('$sClassList') AND state ='resolved'"));while($oTrigger=$oSetTrigger->Fetch()){$oTrigger->DoActivate($oNotify->ToArgs('this'));}return$oNotify->m_iKey;}}}
it turns out I didn't add this to the following syntax:
return$oNotify->$m_iKey;
Notifications have appeared but can't send a lot of notifications, for example:
I have request A and request B, both have resolved status, when the notification keeps popping up on Request A, request B won't get a notification until request A is closed, is it possible to send notifications to Request A and Request B at the same time?
Thankss
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So I can use auto notification, and I want a recently completed ticket not to get a notification for about 3 days, after 3 days a new ticket with a resolved status will get a notification, can it be like that?
I use this query:
SELECT UserRequest AS u WHERE u.status = 'resolved' AND u.resolution_date < DATE_SUB(NOW(), INTERVAL 3 DAY)
Akbar.
Thankss
Last edit: barakbar 2021-07-12
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, everyone
I have been trying to make a background task for email notifications continuously so that the user who has ticket and has resolved it can immediately close the ticket, but when the background task is running nothing happens. Here's the code that I created, please correct it if something goes wrong.
thanks, akbar
Just wondering: is there a limitation in the use of the default notifications combined with a trigger "on state change"?
Hii, Jeffrey
Yes, I am using a state of change but I don't really understand, can you please tell me where is the syntax error in my code? and whether in a certain state "resolve" I can create a background task so I can send emails continuously?
Thanks :)
I have also created a notification trigger and the action but nothing happened.
Last edit: barakbar 2021-05-03
Hello, can anyone help me?
Hello,
In AutoNotifikasi you can replace echo function by IssueLog methods. Check the cron.php output (use verbose mode if needed) and iTop log.
Hello,
For more details like this,
So I plan to create an extension, where the extension that I will create will work and run in the Background. I want to make an extension where when the user creates a ticket and the ticket has resolved and the user does not close the ticket, an email notification will appear continuously until the user closes the ticket,
I've tried the following:
1. I created the following syntax, to create a process background
but the error "Error: Unknown attribute state from class TriggerOnState appears." looks like this problem because I haven't defined the state atribute,
My question is:
1. How do I connect the background process with a trigger/action so that notifications can be sent automatically and continuously?
2. How do Notifications work on iTop?
additional question:
1. can the trigger be run manually? if so, how can i run the trigger manually, without creating a ticket first?
Last edit: barakbar 2021-06-09
Hello,
You want to send a mail each day, so triggers won't do the job as they are launched during the CRUD stack
You'll have to do a background process then.
You can launch your iBackgroundProcess impl manually by running the cron.php manually. Enable verbose mode, add some logs in your code...
Hii, Pierre
I have tried to understand what you are saying, what I understand is the following:
1. I use Crud DBObject::AfterInsert To Trigger Notifications,
2. I get an error like this:
PHP Fatal error: Uncaught Error: Call to protected method DBObject::AfterInsert() from context
protected function AfterInsert()
" syntax, I want to ask, where do I put the syntax? Because I've tried several lines, the error still occurs?this is my code :
Thanks
Hello,
You are doing a mix of Trigger and BackgroundProcess, I don't understand why you chose this path and I don't think this is a good idea.
Just create a BackgroundProcess that will :
If you want your custom notification email to be configurable in the admin console, then you should create a new iTop object, a menu entry to show them, and use those objects in your BackgroundProcess code.
Last edit: Pierre Goiffon 2021-06-18
Hii, Pierre
Thank you for helping me :)
1. what is your suggestion for OQL Query so that notifications can be triggered by background tasks or Do you have documentation for me, so I can study it?
2. https://www.itophub.io/wiki/page?id=2_7_0%3Acustomization%3Aapi%3Aobjects-manipulation%3Adbobject#dbobject what function can be used to call/trigger notification?
Thanks,
Kind Regards
Last edit: barakbar 2021-06-17
Hello,
I'm confused about your answer
As said before, just forget about notifications, triggers and actions
The background task needs to find all tickets for which a mail needs to be send.
So if I understand correctly your original message, tickets that are in the resolved state ?
SELECT Ticket WHERE status = 'resolved'
Then loop on all objects returned by this query, and for each of them send an email.
Hii Pierre,
I have tried what you suggested and nothing happens, here is my code:
honestly i'm confused and don't have basic php, i learn from the code i see in source itop then i follow code by code.
Thanks Pierre, if you can help me, i really appreciate you :)
Best Regards
Akbar
Hii,
does anyone want to give me advice or input about this auto notif?
Thanks
do you have any suggestions for me, cause i'm stuck and confused,
Hii Pierre,please advise me on this matter..
hi,,
I have made this code, but an error like this appears, What do you think?
this is my code:
the error is like this:
Thankss
Last edit: barakbar 2021-07-05
Hey,
after I've been struggling all this time, finally I was able to make the notification appear,....but an error like this appears:
this is my code:
where is the error in the above code?
Thankss,,
it turns out I didn't add this to the following syntax:
Notifications have appeared but can't send a lot of notifications, for example:
I have request A and request B, both have resolved status, when the notification keeps popping up on Request A, request B won't get a notification until request A is closed, is it possible to send notifications to Request A and Request B at the same time?
Thankss
Hiiiii,
So I can use auto notification, and I want a recently completed ticket not to get a notification for about 3 days, after 3 days a new ticket with a resolved status will get a notification, can it be like that?
I use this query:
SELECT UserRequest AS u WHERE u.status = 'resolved' AND u.resolution_date < DATE_SUB(NOW(), INTERVAL 3 DAY)
Akbar.
Thankss
Last edit: barakbar 2021-07-12