+ Start a Discussion
Karleen MendozaKarleen Mendoza 

Update field on master object if checkbox is marked true on junction object

Hi all, I am having issues with getting a field to update. I want to pull in the "primary contact" of an account onto a custom object record (policy). After figuring out some limitations with field updates and the relationships, I have come to the conclusion that I can do a Flow and a few formula fields. BUT it's not working.

Here are the relationships heirarchy (to make things easier, I'll label them as A, B, C, and D.:
  • Account (A) >> Account/Contact (B) (junction object. we have contacts that link to multiple accounts) >>
  • Contact (C)
  • Account >> Policy (D)
Primary contact is marked on the B record. The account/contact relationship is linked to the A record. And the A record is linked to the D record. I need the information from the B record to display on the D record

My thoughts are if I can get the field from the B record onto the A record, I can create display the info via Formula field

So, what I've done so far:
  • Created a Formula field on the B record, to convert the contact ID to an actual name (pulled from C record).
  • Created a Text field on the D record to house the value from the Flow
  • Created a Text field on the A record to house the info pulled from B record using a Flow update
  • Created a Flow and PB Workflow to update the A record with the info from B record, D record then gets updated via Formula

I think there's something off with either my Flow or the WFR but don't know what I'm doing wrong!
Alain CabonAlain Cabon
Hi,

The big problem is to update correctly the parent (A) from the children updates (B). The flow can become quickly tricky

PROCESS BUILDER & FLOW: UPDATE PARENT STATUS BASED ON CHILD STATUSES (more complicated than your case):
https://seanbooksfdcdotcom.wordpress.com/2017/03/05/process-builder-flow-update-parent-status-based-on-child-statuses/

You have already used tricky flows in the past. 

At what step that doesn't work as desired?  (change of a "primary contact" of an account into the junction object ?)
Alain CabonAlain Cabon

APEX Trigger to Concatenate Records (from a Custom Object) and insert the Record Names into a field on the Account.
https://developer.salesforce.com/forums?id=9060G000000MOdLQAW
 
Karleen MendozaKarleen Mendoza
Hi Alain,

Thanks for the reply. I'm not too keen with APEX so using a flow would be better. LOL 

I'd need it to update when the "primary contact" checkbox is checked on the junction object - if there are multiple account/contact relationships on the account.

I'll try the Flow example you gave me and let you know how it goes!
Alain CabonAlain Cabon
Hi Karleen,

I am well aware that you prefer flows instead of some apex code and the second post is an alternative with a flow precisely (only the title is about apex). The flows work sometimes strangely and we need double arrow (otherwise that doesn't work as desired).
 
Karleen MendozaKarleen Mendoza
Double arrow? 

So here's what I've started based on the link you provided:

User-added image

Do I need the Assignment element still if I can just assign it the Record Update?

Also, I need this filter in the Fast Lookup correct? The field is the account ID (via lookup) on the junction object. That's how we match correct?
User-added image
Karleen MendozaKarleen Mendoza
Ok, I got the flow to work to update if the contact is marked as primary contact at first. However, when it is changed to a different contact, it does not update....

What's working:
If there is no primary contact already on account and a contact is then marked as a primary contact - the field updates and shows Contact A

What's not working: 
If a Contact A is in the field, but then when the primary contact is changed to Contact B - it does not update.
Karleen MendozaKarleen Mendoza
Is this an issue with the Flow or the PB workflow??
Karleen MendozaKarleen Mendoza
Ok, another quirk... it seems that the flow will only work for the first contact that was chosen as the primary. 

For example:
If Contact A was checked as the primary contact when the Account was created - then if you change the primary contact to Contact B. Nothing happens, it does not update. BUT if you delete the value of the field (that gets updated) and change the primary contact to Contact A again, it puts the value in the field...
Alain CabonAlain Cabon
Hi Karleen,

Not sure it is exactly your case but that works.

User-added image

if one relationship is marked "is Primary", the primary contact is updated or nullified into the related Account:

User-added image

User-added image

User-added image
User-added image

if there is no relationship with isPrimary__c = true, the collection is empty {!AccountContactRelatCollection} and has a value "null".

Then we just test this value in the decision.


 
Alain CabonAlain Cabon
There are two OUTCOMES for the decision : 

 {!AccountContactRelatCollection}  is null (false) (not empty) and  {!AccountContactRelatCollection} ​ is null (true) (empty)

User-added image

If there is no relationship with is Primary = true, we nullify the Primary_Contact__c with an empty string.

User-added image

Otherwise, there is loop because there could have many Primary contact (wrong use) :

User-added image


User-added image

User-added image


 
Alain CabonAlain Cabon
The autolaunch flow is just triggered by a simple process (each time a record is created or updated into  Account_Contact_Relationship__c.

var_AccountId is an input/output variable in the flow and we just initialize this variable with Account_Contact_relationship__c.Account.Id

User-added image


 
Alain CabonAlain Cabon
The "zero code" solution is fun but there is a big problem: Workflow Rules and Processes are not executed when a record is deleted.

In fact, if you want to use only the process to trigger the flow, you need to add a checkbox "isActive__c" in Account_Contact_relationship__c and prevent the deletion of active rows.  The user must deactivate the row of Account_Contact_relationship__c at first and then the deletion is possible.
This action is an update and can be used easily in the flow.

But to prevent this deletion of active rows, you need a minimal ... trigger (three lines of apex code). There is no alternative here.

https://success.salesforce.com/ideaView?id=08730000000DlPBAA0
 
Alain CabonAlain Cabon
a new checkbox isActive__c (default true) for Account_Contact_Relationship__c​.
trigger AccountContact_rel_beforedelete on Account_Contact_Relationship__c (before delete) {
    If(trigger.isBefore && Trigger.isDelete){
        for(Account_Contact_Relationship__c a: trigger.old){
            if(a.isActive__c) {
                a.adderror('You cant delete an active relationship.');
                break;
            }
        }      
    }
}

and Fast Lookup with : Account__c = {!var_AccountId}  and isPrimary__c = true and isActive__c = true
 
Karleen MendozaKarleen Mendoza
Thanks for all the help. But I think I figured out a simplier solution.. Doing a fast lookup with criteria - to look at the primary contact field if it is checked. Tested and it works how it should!
User-added image
User-added image

Then all I need to do is have a validation rule on account/contact object to prevent it from having more than one primary contact.
Alain CabonAlain Cabon
The big problem is when you remove all the Primary Contacts (or it is forbidden ?) or when you delete the complete row of Account_Contact_Relationship__c because the process is not called at all (nothing happens after a deletion).

The loop is useless indeed as I wrote (bad use with multiple primary contact without a validation rule). 

But it is interesting to know now what happens when you delete the complete row of Account_Contact_Relationship__c because nothing is fired for the process and the primary contact will remain in the account but there is perhaps a simple solution that you have used.