Set boolean checkbox to checked with script

Am trying to set a table field of the type boolean (checkbox) to checked with script. The name of the field is x_invoiced and I have tried the following without success (t is my Ticket object):

  • t.setValue("x_invoiced",1)
  • t.setValue("x_invoiced","1")
  • t.setValue("x_invoiced",yes)
  • t.setValue("x_invoiced","yes")
  • t.setValue("x_invoiced",true)
  • t.setValue("x_invoiced","true")
  • t.setValue("x_invoiced",on)
  • t.setValue("x_invoiced","on")

What is the correct way?

RE: Set boolean checkbox to checked with script

Hi,

t.setValue("x_boolean", "1");

is the correct syntax. If that does not work, then you are doing something else wrong I believe.

Sverre

By: Sverre Hjelm 4 Mar 2020

RE: Set boolean checkbox to checked with script

Then I must be doing something else wrong. This is my code:

Ticket t;
t.load(getParserVariable("ticket.id").toInteger());
t.setValue("x_ticket_brand",getCgiVariable("brand"));
if(getCgiVariable("invoiced") == "on") {

t.setValue("x_serialnumber",getCgiVariable("invoiced"));
t.setValue("x_invoiced","1");
}

The brand and invoiced are form fiels in the ticket form on the customer centre. The brand field is set correct and populating another field (x_serialnumber) with the value of the checkbox from the form works just fine. But setting the checkbox does not work.

By: Patrik Larsson 4 Mar 2020

RE: Set boolean checkbox to checked with script

"on" is the value I get from the HTML form checkbox. Based on that value, I need to flag the boolean checkbox in Service.

By: Patrik Larsson 4 Mar 2020

RE: Set boolean checkbox to checked with script

Hi,

In your code snippet you are missing t.save(), could it be that simple ?

By: Michel Krohn-Dale 4 Mar 2020

RE: Set boolean checkbox to checked with script

I wish it was that simple, here is my full code:

Ticket t;
t.load(getParserVariable("ticket.id").toInteger());
t.setValue("x_ticket_brand",getCgiVariable("brand"));
if(getCgiVariable("invoiced") == "on") {
	t.setValue("x_serialnumber",getCgiVariable("invoiced"));
	t.setValue("x_invoiced","1");
}
else {
	t.setValue("x_serialnumber","no");
}
t.save();
By: Patrik Larsson 4 Mar 2020

RE: Set boolean checkbox to checked with script

If you test a simple script in the editor:

Integer ticketId = 42; // Change to something you've got

Ticket t;
t.load(ticketId);
t.setValue("x_invoiced", "1");
t.save();

SearchEngine se;
se.addField("ticket.x_invoiced");
se.addCriteria("ticket.id", "equals", ticketId.toString());
printLine(se.executeTextTable());

Does it work?

Sverre

By: Sverre Hjelm 4 Mar 2020

RE: Set boolean checkbox to checked with script

I'm afraid not, this is the result:

Changed to ticket with ID 1434 which was created through the form in the customer centre.

By: Patrik Larsson 4 Mar 2020

RE: Set boolean checkbox to checked with script

Hi,

I tried to recreate your setup with:

Running simple script:

#setLanguageLevel 3;

Integer ticketId = 73; // Change to something you've got

Ticket t;
t.load(ticketId);
t.setValue("x_invoiced", "1");
t.save();

SearchEngine se;
se.addField("ticket.x_invoiced");
se.addCriteria("ticket.id", "equals", ticketId.toString());
printLine(se.executeTextTable());

Produces correct result:

Leads me to think that perhaps your field is of wrong type?

By: Michel Krohn-Dale 4 Mar 2020

RE: Set boolean checkbox to checked with script

My field:

Does the options for the field matter?

By: Patrik Larsson 4 Mar 2020

RE: Set boolean checkbox to checked with script

Have you restarted netserver?

Sverre

By: Sverre Hjelm 4 Mar 2020

RE: Set boolean checkbox to checked with script

Dont see that they should impact this.

Of course you also have pressed this button?

By: Michel Krohn-Dale 4 Mar 2020

RE: Set boolean checkbox to checked with script

I hadn't restarted the Netserver and that was it. After a restart, it worked setting the boolean checkbox.

Thanks for the help!

By: Patrik Larsson 4 Mar 2020