When list type field contact is added, mark Contact field true

MStritt
Tera Guru

I have a List Type field on the Account table called Incident Contact (u_incident_contact). It's configured for the Reference table to be the Contact table (customer_contact). So, multiple Contacts can be added to this field. I've created an incident contact (u_incident_contact) true/false field on the Contact table (customer_contact). What I want to do, is have the true/false field set to true, whenever a Contact is added to the u_incident_contact list field on the Account. So, if I add John Doe to this field on the Account, the checkbox will be set to true automatically when I view John Doe's contact record. In addition, it would get changed to false, if I remove him from the Incident Contact field on the Account. Keep in mind, there can be multiple Incident Contacts added/removed to this field at any time or at different times. I've created a BR on the the Account table with the help of a previous community member. It's not working, and the community member is no longer responding. So, I wanted to post again with the information I have so far, to see why the BR isn't working. Below are screenshots of the 2 fields (Contact/Account), along with screenshots of the BR/script created.

 

Incident Contact_Contact .png

 

Incident Contact_Account.png

 

Incident Contact BR_When to run.png

 

Incident Contact BR_Advanced.png

 

// Business Rule Script
(function executeRule(current, previous /* previous is not used in this example */) {
var accountID = current.sys_id; // Assuming Account table's sys_id field
var incidentContacts = current.u_incident_contact.getRefRecord(); // Get the list of incident contacts

// Loop through each incident contact
for (var i = 0; i < incidentContacts.length; i++) {
var contact = incidentContacts[i];
var contactID = contact.sys_id; // Assuming Contact table's sys_id field
var linked = current.u_incident_contact; // Checkbox field on the Contact table

// Update the checkbox field on the Contact record
var contactUpdate = new GlideRecord('customer_contact');
if (contactUpdate.get(contactID)) {
contactUpdate.u_incident_contact = linked;
contactUpdate.update();
}
}
})(current, previous);
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@MStritt Here is the final version.

 

(function executeRule(current, previous /* previous is not used in this example */ ) {

    if (current.u_incident_contact != previous.u_incident_contact) {
        var accountID = current.sys_id; // Assuming Account table's sys_id field
        var incidentContacts = current.u_incident_contact.split(','); // Get the list of incident contacts
        var previousincidentContacts = previous.u_incident_contact.split(','); // Get the list of previous incident contacts    

        // Loop through each incident contact
        for (var y = 0; y < previousincidentContacts.length; y++) {
            var prevContact = previousincidentContacts[y];
            // Update the checkbox field on the Contact record
            var prevContactUpdate = new GlideRecord('customer_contact');
            if (prevContactUpdate.get(prevContact)) {
                prevContactUpdate.u_incident_contact = false; //set false for previous contacts
                prevContactUpdate.update();
            }
        }

        // Loop through each incident contact
        for (var i = 0; i < incidentContacts.length; i++) {
            var contact = incidentContacts[i];
            // Update the checkbox field on the Contact record
            var contactUpdate = new GlideRecord('customer_contact');
            if (contactUpdate.get(contact)) {
                contactUpdate.u_incident_contact = true; //set true for new contacts
                contactUpdate.update();
            }
        }
    }
})(current, previous);

View solution in original post

17 REPLIES 17

Sandeep Rajput
Tera Patron
Tera Patron

@MStritt Here is the final version.

 

(function executeRule(current, previous /* previous is not used in this example */ ) {

    if (current.u_incident_contact != previous.u_incident_contact) {
        var accountID = current.sys_id; // Assuming Account table's sys_id field
        var incidentContacts = current.u_incident_contact.split(','); // Get the list of incident contacts
        var previousincidentContacts = previous.u_incident_contact.split(','); // Get the list of previous incident contacts    

        // Loop through each incident contact
        for (var y = 0; y < previousincidentContacts.length; y++) {
            var prevContact = previousincidentContacts[y];
            // Update the checkbox field on the Contact record
            var prevContactUpdate = new GlideRecord('customer_contact');
            if (prevContactUpdate.get(prevContact)) {
                prevContactUpdate.u_incident_contact = false; //set false for previous contacts
                prevContactUpdate.update();
            }
        }

        // Loop through each incident contact
        for (var i = 0; i < incidentContacts.length; i++) {
            var contact = incidentContacts[i];
            // Update the checkbox field on the Contact record
            var contactUpdate = new GlideRecord('customer_contact');
            if (contactUpdate.get(contact)) {
                contactUpdate.u_incident_contact = true; //set true for new contacts
                contactUpdate.update();
            }
        }
    }
})(current, previous);

Sandeep,

 

Fantastic. This updated code worked! I really appreciate all your time on this. Big help!

@MStritt You forgot to mark the solutions helpful 🙂