[rt-users] rt-4.0.5 - How to populate a custom field using a web service?

Joe Harris drey111 at gmail.com
Mon Feb 6 07:19:40 EST 2012


> I am looking at rt-4.0.5 and it seems that you can tie a custom field into a
> web service.

I created a PHP web form to try and drive requestors to put in the
proper information.  What I provided was a drop down box to show
custom fields pulled from the RT database and then build a email to be
sent to the queue the user chose.

> Is there any documentation about using a webservice or does someone have an
> example of its use? Or if someone has details about doing something like I
> described above using another method I'd love to hear about that too.

The following is the PHP code I used to pull the info (billing codes)
from the customfieldvalues table where the ID of the custom field I am
using is 1.  Since this is a field that has a parent/child
relationship, this creates an option group heading with the selectable
fields in the drop down box under each option group.  If you have just
one field and no relationships, it is much more simple.  This requires
a database connection string which is in a file outside of my web
directory.  I know this may not be exactly what you are looking for,
but the main part you are asking about I believe the part you are
looking for specifically is down at the end of building the message
where the custom fields are pushed into the email with commandbymail.
Note, I found out the custom fields could NOT have any spaces in them
for commandbymail to function.  I hope this helps and is not too
confusing.  I am quite sure some of this could be done more
efficiently.  I am not a developer.  I have a good understanding of
php, but not always the most efficient way.

---connection_file---
 <?php
$host = "database_server";
$user = "postgres";
$pass = "dbpassword";
$db = "rtdb";
$conn_rtdb = pg_connect("host=$host dbname=$db user=$user
password=$pass") or die("Couldn't Connect to $db".pg_last_error());
?>
---connection_file---
---ticket_form---
<?php
//set page action based on how the user gets to the page (sendMail or showForm)
$action = $_REQUEST['action'];
global $action;
---form_code---
function showForm() {
include('/path/to/connection_file');
$getclientproject = pg_query($conn_rtdb, "select name from
customfieldvalues where customfield=1 order by name")or die("Get
ClientProject " . pg_last_error());
        $fields=pg_num_fields($getclientproject);
        echo "<tr><td>Task Code</td><td><select name=\"taskcode\">";
        echo "<option value=\"\" selected>Select...</option>";
        for ($i=0; $i < pg_num_fields($getclientproject); $i++)
        while ($row = pg_fetch_row($getclientproject)) {
        for ($f=0; $f < $fields; $f++) {
        echo "<optgroup label=\"$row[$f]\">$row[$f]";
        $gettaskcodes = pg_query($conn_rtdb, "select c.name from
customfieldvalues a,attributes b,customfieldvalues c where
a.name=b.content and b.objectid=c.id and b.content='$row[$f]' order by
c.name,c.sortorder")or die("Get Codes ".pg_last_error());
                $fields=pg_num_fields($gettaskcodes);
                for ($i=0; $i < pg_num_fields($gettaskcodes); $i++)
                while ($row = pg_fetch_row($gettaskcodes)) {
                for ($f=0; $f < $fields; $f++) {
                echo "<option value=\"$row[$f]\">$row[$f]";
                echo "</option>";
                }}
                echo "</optgroup>";
                }}
         echo "</select></td></tr>";
}
//end action showForm
?>
---form_code---

Then I gather the form data to be pushed into RT as an email.  I use
the commandbymail plugin to allow fields to be populated via email.
Then I build the email with PHP code to send to RT:

---form_submit---
<?php
function sendMail()
{
include("/path/to/connection_file");
// Gather form data... each item that is pulled had its own field in
the web form
$to = $_REQUEST['sendto'] ; //whatever queue they chose in a dropdown
box on the web form
$from = "$_REQUEST['from_email']" ;
$project = $_REQUEST['Project'] ;
$priority = $_REQUEST['Priority'] ;
$duedate = $_REQUEST['duedate'] ;
$time = $_REQUEST['time'] ;
$taskcode = $_REQUEST['taskcode'] ;
$admincc = $_REQUEST['AdminCC'] ;
//Get client project from RT database
$getcp = pg_query($conn_rtdb, "select a.content from attributes a,
customfieldvalues b where b.name='$taskcode' and b.id=a.objectid")or
die("Get CP ".pg_last_error());
$rescp = pg_fetch_row($getcp);
$cltprj = $rescp[0];
//create due date timestamp, concatenate fields and clean up strange characters
$due = $duedate." ".$time ;
$subjectdetails = pg_escape_string(stripslashes($_REQUEST['SubjectDetails'])) ;
$body = pg_escape_string(stripslashes($_REQUEST['Body'])) ;
$subject = $project.":  " . $subjectdetails ;
//Build data to be pushed into ticket for commandbymail
$fields = array();
$fields{"Project"} = "Project";
$fields{"SubjectDetails"} = "Subject";
$fields{"Body"} = "Message";
foreach($fields as $a => $b)
{
$bodymessage .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
//Build message headers
$headers = "From: $from\n";
$headers .= "Reply-To: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related;
type=\"multipart/alternative\";
boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Nmrmal
$headers .= "Return-Path: <" . $_POST['Email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative;
boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
//Build message contents
$message  = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add custom message, in this case it's plain text.  You could also
add HTML by changing the Content-Type to text/html */
$message .= "Due: ".$duedate." ".$time."\n";
$message .= "Priority: ".$priority."\n";
if($admincc != 'none'){
$message .= "AdminCc: ".$admincc."\n";
}
$message .= "CustomField.{Client_Project}: ".$cltprj."\n";
$message .= "CustomField.{Task_Code}: ".$taskcode."\n";
$message .= "CustomField.{Category}: ".$category."\n";
$message .= "\n";
$message .= "$bodymessage\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
//Send message
$ok = mail($to, $subject, $message, $headers);
if($ok == 1)
{
echo "Your ticket has been sent.";
}
else
{
print "We encountered an error sending your mail, please notify the RT
administrator";
}
}
?.
//Determine what state the form is in when the user gets to it
<?php
switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}
?>



More information about the rt-users mailing list