function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Laura Babb 4Laura Babb 4 

flowapplication errors from process builder

I have process builder that looks at a custom lookup field on the account object and uses that to assign the related contacts to that user. The logic is that if that custom lookup field is not "unassigned" then the related contact owners are transferred to that user id. If it is "unassigned" then the related contact owners are transferred to the account owner user id.

I am getting hundreds of these flowapplication errors:

Error element myRule_4_A1 (FlowRecordUpdate).
The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: DSCORGPKG.DiscoverOrgCustomMapping_Contact: System.LimitException: Apex CPU time limit exceeded.

I don't know how to fix this or why it is referencing a discoverorg pakage field... nothing like that is even part of the process! Please help! TIA!
Best Answer chosen by Laura Babb 4
Manish ArvindManish Arvind
Hi Laura,
Greetings to you!

So Sorry for late replying, now. Using Apex class in Process Builder, bulk data can also be handled. So Error may not come in this case. Error comes up, if there are more triggers are there on same objects(in this case on Contacts or Accounts) already.

Let's say there is a custom field Demand_Gen_Owner__c on Account for User lookup. So your Apex class may as
 
global class UpdateContactsFromAccount {
    
    @InvocableMethod 
    global static void saveInformation (List<Id> accountIds) {
    	List<Account> listAccount = [Select Id, Demand_Gen_Owner__c, OwnerId FROM Account WHERE Id In :accountIds];
        List<Contact> listContact = [Select Id, AccountId, OwnerId FROM Contact WHERE AccountId In :accountIds];
        
        Map<Id, Account> mapIdToAccount = new Map<Id, Account>();
        mapIdToAccount.putAll(listAccount);
        System.debug('Accounts for which Contacts to be updated:: mapIdToAccount : ' + mapIdToAccount);
        for(Contact con: listContact){
            if(con.AccountId != null){
                Account acc = mapIdToAccount.get(con.AccountId);
                if(acc.Demand_Gen_Owner__c != null){
                	con.OwnerId = acc.Demand_Gen_Owner__c;
                }
                else {
                    con.OwnerId = acc.OwnerId;
                }
            }
        }
        update listContact;
    }
}
I you have any query, please let me know.

Thanks and Regards,
Manish Arvind

All Answers

Manish ArvindManish Arvind
Hi LAura,
Greetings to you!

Are you using Apex class invocable method or Update related record into Action of Process Builder?
I think you can use Apex class invocable method, if you are not using it. Using invocable method you can control more on code, as I think.
Please use it and let me know.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Manish Arvind
Laura Babb 4Laura Babb 4
Hi Manish,
Greetings and thanks for responding quickly!

I am doing an update to related records. You think I would be better to create an Apex class and call it in the process builder action? I think you are right, however I am not very apex savvy. Are you able to provide any assistance on writing it?
Manish ArvindManish Arvind
Hi Laura,

You can explain functional requirement of process, so that I can give you some idea :)

Thanks,
Manish Arvind
manish.arvind@gmail.com
Laura Babb 4Laura Babb 4
Hi Manish,

I have a custom field on the account object (Demand Gen Owner). This is a user lookup field. This field gets updated with a user and that triggers the process to transfer the contact owner on all related contacts to the user that has been populated in the Demand Gen Owner field. If the Demand Gen Owner is changed to 'Unassigned' then all related contacts are transferred to the same owner as the account owner.

Demand Gen Owner is changed and is not 'Unassigned' >> Update related contacts' owner via field reference to the Demand Gen Owner
Demand Gen Owner is 'Unassigned' or is changed to 'Unassigned' >> Update related contacts' owner to the Account Owner

When a bulk load is taking place, it seems to trigger an apex trigger that is part of the discoverorg managed package to update contacts and this is causing the flow error I described above, but the flow is erroring on my contact ownership process even though there is nothing related to discoverorg updates in the process. I'm confused!

Thanks again for your help. Manish!
Laura
Manish ArvindManish Arvind
Hi Laura,
Greetings to you!

So Sorry for late replying, now. Using Apex class in Process Builder, bulk data can also be handled. So Error may not come in this case. Error comes up, if there are more triggers are there on same objects(in this case on Contacts or Accounts) already.

Let's say there is a custom field Demand_Gen_Owner__c on Account for User lookup. So your Apex class may as
 
global class UpdateContactsFromAccount {
    
    @InvocableMethod 
    global static void saveInformation (List<Id> accountIds) {
    	List<Account> listAccount = [Select Id, Demand_Gen_Owner__c, OwnerId FROM Account WHERE Id In :accountIds];
        List<Contact> listContact = [Select Id, AccountId, OwnerId FROM Contact WHERE AccountId In :accountIds];
        
        Map<Id, Account> mapIdToAccount = new Map<Id, Account>();
        mapIdToAccount.putAll(listAccount);
        System.debug('Accounts for which Contacts to be updated:: mapIdToAccount : ' + mapIdToAccount);
        for(Contact con: listContact){
            if(con.AccountId != null){
                Account acc = mapIdToAccount.get(con.AccountId);
                if(acc.Demand_Gen_Owner__c != null){
                	con.OwnerId = acc.Demand_Gen_Owner__c;
                }
                else {
                    con.OwnerId = acc.OwnerId;
                }
            }
        }
        update listContact;
    }
}
I you have any query, please let me know.

Thanks and Regards,
Manish Arvind
This was selected as the best answer
Laura Babb 4Laura Babb 4
Hi Manish!
I got wrapped up in something else, but have gotten back to this. I modified a little (below), and it doesn't update the owners as expected. I added it to the immediate action in the process builder. Is that what I was supposed to do?
 
global class UpdateContactsFromAccount {
    
    @InvocableMethod 
    global static void saveInformation (List<Id> accountIds) {
    	List<Account> listAccount = [Select Id, Demand_Gen_Owner__c, OwnerId FROM Account WHERE Id In :accountIds];
        List<Contact> listContact = [Select Id, AccountId, OwnerId FROM Contact WHERE AccountId In :accountIds];
        
        Map<Id, Account> mapIdToAccount = new Map<Id, Account>();
        mapIdToAccount.putAll(listAccount);
        System.debug('Accounts for which Contacts to be updated:: mapIdToAccount : ' + mapIdToAccount);
        for(Contact con: listContact){
            if(con.AccountId != null){
                Account acc = mapIdToAccount.get(con.AccountId);
                if(acc.Demand_Gen_Owner__c != 'Unassigned' || acc.Demand_Gen_Owner__c != null || acc.Type != 'Current Customer'){
                	con.OwnerId = acc.Demand_Gen_Owner__c;
                }
                else {
                    con.OwnerId = acc.OwnerId;
                }
            }
        }
        update listContact;
    }
}
Thanks again for your help!
Laura