Set first value of an CF as default value


#1

Hi there,

Recently I’ve used ExternCustomFieldValue, to populate two custom field with values from a database. These two field are select one value type and they are category based fields. As you can see in the print screen, I have a Project Name and each project has a Manager. I want to write a script, so when the ticket is created, the system set the value of Project Manager custom field to it’s first value. I know how to set a value on a ticket creation, but my question is, how I can get the first value in that field and set it as the default value?

38

Thanks!


#2

If the CF is applied to the ticket you can do my $default_manager = $ticket->FistCustomFieldValue('Project Manager');


#3

Thank you for the response, @craig but I’ve already try this and I get the (no value) as the default value on ticket creation

$self->TicketObj->AddCustomFieldValue(
       Field => 'Project Manager',
       Value => $ticket->FistCustomFieldValue('Project Manager')
    );

#4

Ah yes you are correct, how about this:

my $customfield = $Ticket->LoadCustomFieldByIdentifier('My Field');
my $value = $customfield->Values->First->Name;

#5

This return me the same value as before, ( no value ) . Is there any way I can loop trough the values , and select the first value in that field ? I don’t know


#6

If this is a direct cut-n-paste, you might want to change FistCustomFieldValue to FirstCustomFieldValue.


#7

To loop through the collection:

my $values = $customfield->Values;
while (my $value = $values->Next) {
...
}

#8

Hi @GreenJimll,

I just copied the code from @craig reply, but I have already tried that method, and the result was the ( no value ) .

Thanks anyway