CustomField value is reset or deleted back to prior value in scrip

I have 4 custom fields in this scenario:

1st Approval
2nd Approval

Those are both “Select One Value” dropdowns that apply to tickets. Permissions are “view” for Privileged and modify for a group allowed to approve these tickets.

I have 2 additional fields:
1st Approved By
2nd Approved By

Both are “Enter one value” that apply to tickets. Privileged has view and there are no other permissions set (I don’t want users able to set this field, essentially “read only” - you’ll see why in a minute).

If an approver makes a selection in one of the select boxes, I have scrip that will enter a value in the corresponding “approved by” field.

Here’s the weird thing - it works perfect for the “1st” boxes. But when I choose a value for the 2nd approval, it sets it, then reverts it back to the previous value (I’ve tried modifying the field as a superuser to confirm its rolling back and not just deleting the value). The scrip code from the “1st” check/set was copy/pasted and only the variables changed to reflect the different column ids.

Here is the snippet from the ticket history:
Fri Dec 02 09:35:37 2011 Brent Wiese - (CC) 2nd Approval No changed to Yes
Fri Dec 02 09:35:37 2011 The RT System itself - (CC) Seconded By Approved by: Brent Wiese added
Fri Dec 02 09:35:37 2011 Brent Wiese - (CC) Seconded By Approved by: Brent Wiese deleted

Scrip:
Condition: User Defined
Action: User Defined
Template: Global template: Blank
Stage: TransactionCreate

Custom condition:
unless ($self->TransactionObj->Type eq “CustomField” ) {
return 0;
}
1;

Prep:

Set Field to ID of (CC) 1st Approval:

my $CFid = 5;

Set to ID of (CC) 1st Approved By:

my $CFAppID = 18;

Set Field to ID of (CC) 2nd Approval:

my $CFid2 = 17;

Set to ID of (CC) 2nd Approved By:

my $CFAppID2 = 20;

Set to 1 if you want an extra line that the scrip made changes logged in the ticket history

my $rec = 1;

my $cf_obj = RT::CustomField->new($RT::SystemUser);

my $trans = $self->TransactionObj;
my $ticket = $self->TicketObj;
my $person = $trans->CreatorObj->RealName;

if ( $self->TransactionObj->Type eq “CustomField” && $self->TransactionObj->Field == $CFid ) {

                            $cf_obj->LoadByName( Name => $CFAppID );

                            my $val = $ticket->FirstCustomFieldValue($CFid);
                            if ($val eq 'Yes') {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Approved by: $person", RecordTransaction => $rec );
                                            } elsif ($val eq 'No') {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Disapproved by: $person", RecordTransaction => $rec );
                                            } else {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Pending", RecordTransaction => $rec );
                                            }

}

if ( $self->TransactionObj->Type eq “CustomField” && $self->TransactionObj->Field == $CFid2 ) {

                            $cf_obj->LoadByName( Name => $CFAppID2 );

                            my $val2 = $ticket->FirstCustomFieldValue($CFid2);
                            if ($val2 eq 'Yes') {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Approved by: $person", RecordTransaction => $rec );
                                            } elsif ($val2 eq 'No') {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Disapproved by: $person", RecordTransaction => $rec );
                                            } else {
                                            $ticket->AddCustomFieldValue(Field => $cf_obj, Value => "Pending", RecordTransaction => $rec );
                                            }

}
return 1; <-- I’ve tried return 0 here too and no difference

No cleanup.

It was working this way before I added the $cf_obj variable. I thought maybe hard-coding it to enter the value as RT_System would help, but it made no difference.

I tried doing all the code in the custom condition (which is why I’m still checking if it’s a customfield transaction in the if statement). Works the same either way I try it.

Any thoughts?

Thanks,
Brent

Brent,

Without looking at your code, I can tell you that you should make the stage
“TransactionBatch” (be sure to turn that on in your RT_SiteConfig.pm file).

Try and see if that works.

KennOn Fri, Dec 2, 2011 at 8:49 AM, Brent Wiese bwiese@elementps.com wrote:

I have 4 custom fields in this scenario:****


1st Approval****

2nd Approval****


Those are both “Select One Value” dropdowns that apply to tickets.
Permissions are “view” for Privileged and modify for a group allowed to
approve these tickets.****


I have 2 additional fields:****

1st Approved By****

2nd Approved By****


Both are “Enter one value” that apply to tickets. Privileged has view and
there are no other permissions set (I don’t want users able to set this
field, essentially “read only” - you’ll see why in a minute).****


If an approver makes a selection in one of the select boxes, I have scrip
that will enter a value in the corresponding “approved by” field.****


Here’s the weird thing – it works perfect for the “1st” boxes. But when I
choose a value for the 2nd approval, it sets it, then reverts it back to
the previous value (I’ve tried modifying the field as a superuser to
confirm its rolling back and not just deleting the value). The scrip code
from the “1st” check/set was copy/pasted and only the variables changed
to reflect the different column ids.****


Here is the snippet from the ticket history:****

Fri Dec 02 09:35:37 2011 Brent Wiese - (CC) 2nd Approval No changed to Yes


Fri Dec 02 09:35:37 2011 The RT System itself - (CC) Seconded By Approved
by: Brent Wiese added ****

Fri Dec 02 09:35:37 2011 Brent Wiese - (CC) Seconded By Approved by:
Brent Wiese deleted ****


Scrip:****

Condition: User Defined****

Action: User Defined****

Template: Global template: Blank****

Stage: TransactionCreate****


Custom condition:****

unless ($self->TransactionObj->Type eq “CustomField” ) {****

return 0;****

}****

1;****


Prep:****

Set Field to ID of (CC) 1st Approval:****

my $CFid = 5;****

Set to ID of (CC) 1st Approved By:****

my $CFAppID = 18;****

Set Field to ID of (CC) 2nd Approval:****

my $CFid2 = 17;****

Set to ID of (CC) 2nd Approved By:****

my $CFAppID2 = 20;****

Set to 1 if you want an extra line that the scrip made changes logged in

the ticket history****

my $rec = 1;****


my $cf_obj = RT::CustomField->new($RT::SystemUser);****


my $trans = $self->TransactionObj;****

my $ticket = $self->TicketObj;****

my $person = $trans->CreatorObj->RealName;****


if ( $self->TransactionObj->Type eq “CustomField” &&
$self->TransactionObj->Field == $CFid ) {****


                            $cf_obj->LoadByName( Name => $CFAppID );**

**

                            ****

                            my $val =

$ticket->FirstCustomFieldValue($CFid);****

                            if ($val eq 'Yes') {****

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Approved by:
$person”, RecordTransaction => $rec );****

                                            } elsif ($val eq 'No') {**

**

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Disapproved by:
$person”, RecordTransaction => $rec );****

                                            } else {****

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Pending”,
RecordTransaction => $rec );****

                                            }****

}****


if ( $self->TransactionObj->Type eq “CustomField” &&
$self->TransactionObj->Field == $CFid2 ) {****


                            $cf_obj->LoadByName( Name => $CFAppID2 );


                            my $val2 =

$ticket->FirstCustomFieldValue($CFid2);****

                            if ($val2 eq 'Yes') {****

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Approved by:
$person”, RecordTransaction => $rec );****

                                            } elsif ($val2 eq 'No') {*

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Disapproved by:
$person”, RecordTransaction => $rec );****

                                            } else {****

$ticket->AddCustomFieldValue(Field => $cf_obj, Value => “Pending”,
RecordTransaction => $rec );****

                                            }****

}****

return 1; ß I’ve tried return 0 here too and no difference****


No cleanup.****


It was working this way before I added the $cf_obj variable. I thought
maybe hard-coding it to enter the value as RT_System would help, but it
made no difference.****


I tried doing all the code in the custom condition (which is why I’m still
checking if it’s a customfield transaction in the if statement). Works the
same either way I try it.****


Any thoughts?****


Thanks,****

Brent****




RT Training Sessions (http://bestpractical.com/services/training.html)

  • Boston — March 5 & 6, 2012

I’d tried TransactionBatch previously when I was doing a lot of logging and I noticed it was creating 2 log entries for every ticket update. When I switched to TransactionCreate it only logged once, so I figured that’s the one I wanted.

However, you were right, changing to TransactionBatch worked. Why it worked on 1 of the CF changes and not the other… who knows.

But it’s now brought up another couple issues:

1: If I change the value of BOTH “approved” CF’s and update the ticket, the scrip is only catching the 2nd approval field and updating its “approved by” field. I’m logging inside of each “if” statement and I’m only seeing the log entry in the second if. It should be passing both if statements and making the necessary changes. So either one works individually, but when changing both, only the second gets caught.

2: If I find both approvals are yes and the approved by CF’s match, then I set the ticket stalled. That works fine. However, if I go back into “display” and change the status to open and one of the approval fields to “no”, it doesn’t change the “approved by” field (it does reopen the ticket and retain the “no” choice.) In order to catch both CF and Status updates, I changed the condition code to this:

unless (($self->TransactionObj->Type eq “CustomField” ) || ($self->TransactionObj->Type eq “Status” )) {
$RT::Logger->info(“Made it into condition”);
return 0;
}
1;

Any idea why that would happen?From: rt-users-bounces@lists.bestpractical.com [mailto:rt-users-bounces@lists.bestpractical.com] On Behalf Of Kenneth Crocker
Sent: Friday, December 02, 2011 11:41 AM
To: RT User List
Subject: Re: [rt-users] CustomField value is reset or deleted back to prior value in scrip

Brent,

Without looking at your code, I can tell you that you should make the stage “TransactionBatch” (be sure to turn that on in your RT_SiteConfig.pm file).

Try and see if that works.

Kenn