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 Could you please update the script as follows and check if it works.

 

(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.split(','); // Get the list of incident contacts

// 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;
contactUpdate.update();
}
}
})(current, previous);

Hi Sandeep!

 

Thanks for the response. I updated the script, and it does mark the incident contact box on the contact record as true (even when adding multiple contacts to the incident contact list field on the Account). So, works! However, if I then remove that contact from the incident contact list field, the checkbox is still true on the Contact record. I need to have that box changed to false, if they are removed from the incident contact field on the Account.

@MStritt Here is the script covering both the scenarios.

 

(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.split(','); // Get the list of incident contacts

    if (incidentContacts.length == 0) {
        contactUpdate.u_incident_contact = false;
        contactUpdate.update();
    } else {
        // 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;
                contactUpdate.update();
		        break; //break if the contact is set
            }
        }
    }

})(current, previous);

Please mark the response helpful and correct if it manages to address your requriement

Sandeep,

This didn't work. Also, now when I add multiple (2 contacts) to the incident contact list field on the Account, only the first contact I entered had the incident contact field change to true. This worked prior to this recent script change.