You need to sign in to do that
Don't have an account?
Terri Henry
Feedback on User Object Trigger
Hi All,
I have created a trigger to prevent the deactivation of a user if they own any Accounts, Contacts or unconverted leads - as this causes issues with the Pardot sync (the Marketing team lead me to believe)
The trigger works as expected, though I'm aware that with containing SOQL queries within the FOR loop I'm breaking best practice - but would there be a performance issue on taking this out of the loop and running them on every update (even when they are not needed)
Constructive criticism welcomed
Thanks
I have created a trigger to prevent the deactivation of a user if they own any Accounts, Contacts or unconverted leads - as this causes issues with the Pardot sync (the Marketing team lead me to believe)
The trigger works as expected, though I'm aware that with containing SOQL queries within the FOR loop I'm breaking best practice - but would there be a performance issue on taking this out of the loop and running them on every update (even when they are not needed)
Constructive criticism welcomed
Thanks
//Trigger to prevent deactivation of user if they own any records referenced by Pardot. //Deactivating a user while they still own these records (accounts, contacts, uncoverted leads) will cause issues with Pardot sync trigger UserPreventDeactivation on User (before update) { for(User u : Trigger.new){ User oldU = Trigger.oldMap.get(u.Id); //Check if user has changed from active to inactive if(!u.IsActive && oldU.IsActive){ //Query number of Accounts, Contacts and Unconverted Leads in User's Name //SHOULD BE OUTSIDE OF LOOP TO BE BULKIFIED? but don't want to run these queries on every update to user if not needed. //but as have only internal users, not likely to deactivate users in bulk Integer contactCount = [SELECT count() FROM Contact WHERE ownerId = :u.id LIMIT 1]; Integer accountCount = [SELECT count() FROM Account WHERE ownerId = :u.id LIMIT 1]; Integer leadCount = [SELECT count() FROM Lead WHERE ownerId = :u.id AND isConverted = FALSE LIMIT 1]; //add error to screen alerting user if( (contactCount + accountCount + leadCount) > 0){ u.addError('This user may own accounts, contacts, or unconverted leads.' + 'Ensure that these records are transferred to a different owner ' + 'before deactivating this user. This is due to issues it will cause ' + 'with the Pardot sync.'); } } } }
You dont need to run this on every update. Do the calculation only when user has been deactivated.
All Answers
You dont need to run this on every update. Do the calculation only when user has been deactivated.