Simplfiy scrip code / Extension

Hi, I currently have several scrips retreiving data from an external application (Dolibarr) via a REST API based on custom fields content.
It works correctly, but many code blocks are duplicated in each and every scrip.

Reading @GreenJimll excellent blog post https://blog.lboro.ac.uk/middleware/blog/office365/implementing-change-management-in-rt I noticed he talks about creating an extension to talk to Microsoft API.
Then reading the relevant wiki page https://rt-wiki.bestpractical.com/wiki/Extensions led to great deception as no page explains the process to create and extension which could wrap my API code.

Here is a scrip condition code example to check if an invoice is paid or not before triggering the scrip action :

my $txn = $self->TransactionObj;
my $type = $txn->Type;

#--- vérifie qu'on passe bien vers un statut récup
return 0 unless $type eq "Status"
    || ( $type eq 'Set' && $txn->Field eq 'Status');
return 0 unless $txn->NewValue eq "recup";

#---- Récupère le numéro de facture dans le CP
my $DolInRTConfig = RT->Config->Get('DolInRT');
my $ref_facture = $self->TicketObj->FirstCustomFieldValue($DolInRTConfig->{'CFInvoice'});
$ref_facture =~ s/[\(|\)]//g;
#---- Sortir si le champ est vide
if (!defined($ref_facture)){
  $RT::Logger->debug( "[Scrip#35] Aucune référence de facture renseignée");
  return 0;
}

#---- Crée le header d'authentification
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $headers = {Dolapikey => $DolInRTConfig->{'Dolibarr'}->{'Key'}, Accept => 'application/json'};
#---- Crée le client REST
my $client = REST::Client->new();
$client->setHost($DolInRTConfig->{'Dolibarr'}->{'URL'});

#----  Recupère l'ID de la facture a partir de sa référence
$client->GET("/api/index.php/invoices?sortfield=t.rowid&sortorder=ASC&sqlfilters=(t.ref%3Alike%3A'%25".$ref_facture."%25')", $headers);

#----  Teste si une erreur s'est produite dans la requĂŞte API
if($client->responseCode() != 200) {  
    $RT::Logger->error ( "[Scrip#35] Error retreiving invoice info (" . $client->responseCode().") ".$client->responseContent() );
    return 0;  
}
#--- Traitement du retour de l'API
else {
     my $invoice_json = $client->responseContent();
     my $invoice_array = decode_json($invoice_json);
     foreach my $invoice ( @$invoice_array ) {
         if($invoice->{"paye"} != 0) {
            $RT::Logger->debug( "[Scrip#35] La facture " . $invoice->{"ref"}." est déjà payée");
            return 0;
         }
         else {
            $RT::Logger->debug( "[Scrip#35] La facture " . $invoice->{"ref"}." n'est pas payée, envoi du mail automatique");
         }
    }
}

return 1;

I’d like to change this condition to something like :

my $txn = $self->TransactionObj;
my $type = $txn->Type;

#--- vérifie qu'on passe bien vers un statut récup
return 0 unless $type eq "Status"
    || ( $type eq 'Set' && $txn->Field eq 'Status');
return 0 unless $txn->NewValue eq "recup";

#---- Récupère le numéro de facture dans le CP
my $DolInRTConfig = RT->Config->Get('DolInRT');
my $ref_facture = $self->TicketObj->FirstCustomFieldValue($DolInRTConfig->{'CFInvoice'});
$ref_facture =~ s/[\(|\)]//g;
#---- Sortir si le champ est vide
if (!defined($ref_facture)){
  $RT::Logger->debug( "[Scrip#35] Aucune référence de facture renseignée");
  return 0;
}

#---- check invoice status
if($RT::Extension::Dolibarr->InvoicePaid($ref_facture)) { return 0; }
else { return 1; }

where the “InvoicePaid” method would contain the above longer code.

This would be quite shorter and easier to maintain, and the extension code could be under git versionning.

Could someone point me to any helper on how to create an extension to achieve this goal ?

found this good article on extensions https://docs.bestpractical.com/rt/4.4.4/writing_extensions.html but it doesn’t look appropriate for scrips code

Finally found the good help page https://docs.bestpractical.com/rt/4.4.4/customizing/scrip_conditions_and_action.html
So I can write a “Custom Condition Module” named DolibarrInvoicePaid for example, with the necessary code :slight_smile:
RT is really nice !

2 Likes