update a bool using setValue

lock
push_pin
done
Answered
2

Hello, I'm new to this code language and I still haven't figured a lot out yet. I've got this section of a script:

    if (c.setValue("contact_id", contactId.toString())) {
        if (mismatchFound) {
            c.setValue(BOOL_UDF_KEY, "1");
            printLine("Updated:" + companyName);
        } else {
            c.setValue(BOOL_UDF_KEY, "0");
        }
        c.save();
    } else {
        printLine("Failed to load contact: " + contactId.toString());
    }

Where I'm trying to update a UDF Checkbox, but I don't know exactly how to update it or how to pass the value. Passing it as a string return the error: EjScript::RunTimeException: Value::getBool(): m_type != TypeBool

but passing it as anything else is not allowed when using setValue. Any input or advice on this would be greatly appreciated.

31 Jul 2025 | 06:56 AM

All Replies (2)

Hi Fabian,

There's multiple issues with your code. It looks like you're trying to load a contact (a company) and set a UDEF checkbox value on it, is that correct? I'm gonna assume the "c" variable holds an instance of the Company class.

  • c.setValue() is not how you load a Company. You need to use c.load(contactId).
  • The reason for your EjScript::RunTimeException is that c.setValue() does not return a boolean, so it cannot be used as the condition for your if statement.
  • Besides, you can't use the Company class for setting UDEF values anyways. For that, we have the NSContactAgent and NSContactEntity classes.

Here's how to load a contact, set a UDEF value and save it.

String CHECKBOX_PROG_ID = "SuperOffice:1";
Integer contactId = 1000;

// Load the contact (a company)
NSContactAgent contactAgent;
NSContactEntity contactEntity = contactAgent.GetContactEntity(contactId);

// Set the udef value
Map udefs;
udefs.insert(CHECKBOX_PROG_ID, "1"); // Sets value to True
contactEntity.SetUserDefinedFields(udefs);

// Save the contact to database
contactEntity = contactAgent.SaveContactEntity(contactEntity);

Hope that helps!

Espen

31 Jul 2025 | 07:40 AM

Thank you, this worked great!

31 Jul 2025 | 11:42 AM

Add reply