SFDO-Community / declarative-lookup-rollup-summaries

Declarative Lookup Rollup Summaries (DLRS) is a community built and maintained Salesforce application that allows you to create cross object roll-ups declaratively - no code! For install instructions and documentation, visit our website https://sfdo-community-sprints.github.io/DLRS-Documentation/
https://sfdo-community-sprints.github.io/DLRS-Documentation/
BSD 3-Clause "New" or "Revised" License
694 stars 237 forks source link

Error when creating a contact/user rollup summary: execution of AfterInsert caused by: System.DmlException: Upsert failed. #380

Open aparnaMAF opened 8 years ago

aparnaMAF commented 8 years ago

Hello,

I recently created a rollup summary field where the parent object was Contact, child was User. The idea was to count the number of active users associated to that contact (basically, to see if that contact was associated with an active communities user). The rollup summary was working really well! However, now I am unable to create a new communities user (we have both a partner community and a customer community; this is an issue for both types of users).

When I create the user, this is the error message I get:

capture

I tried changing the DLRS from Real-Time update to Scheduled, and it didn't make a difference. However, once I deactivated the DLRS (unchecked the Active box), I was able to create the user. Our current workaround is to deactivate the DLRS right before creating the user and reactivate it right after; as you can probably imagine, this is quite tedious. Also, in about a month, we're moving to a system where customer communities users will be created automatically by our system when a user signs up for an account with us, which will make this workaround unworkable. Do you know if there's a way to get around this issue, so that we can create new communities users while the trigger is still active?

Thanks so much for your help!

Regards, Aparna

afawcett commented 8 years ago

I fear this is a platform restriction as described here.

If you can answer these, i can confirm the above and/or think about any workarounds that come to mind.

Thanks,

Andy

aparnaMAF commented 8 years ago

Thank you for your quick response, Andy!

1- Here are screenshots of the rollups:

capture

capture

2- Other triggers on the user object:

capture

As you can see, the only ones I can actually see is one custom trigger that we created (UserTrigger) and the DLRS one. The rest are all from managed packages.

Our trigger converts partner community users into partner super users after the user is created. Here's the handler (pardon my shaky penmanship): capture

3- Steps to reproduce the issue: a- Go to a contact that isn't associated with a child user b- Click Manage External Users --> Create Partner Community User c- Set the profile to our Partner Community profile (we have one that we use) and save the user These steps return the error in the screenshot I sent you in my last message when either of the DLRSs are active and they work fine when the DLRSs are both inactive.

Does this help? Please let me know if you have any other questions.

Thanks! Aparna

afawcett commented 8 years ago

The error message indicates its trying to update one of the objects in the tool that keeps track of parent records that need their rollups recalculating in the background (Calculation Mode = 'Scheduled'). However i see your using Realtime. So this is most odd. The only other situation where this should occur is during a merge, but this i don't think is what your doing here either. Is it possible the rollup definitions have been changed since the error occured and you originally reported the issue? Also can you try on the latest version of the tool, as the code line number 900 does not appear to relate to what i am seeing (if thats an issue tell me the version your on). Other thought is to make sure their are no other rollups defined on these objects.

afawcett commented 7 years ago

@aparnaMAF hope your well, let me know if you still want to discuss this issue, if so we can re-open it and continue. Thanks. šŸ‘

aparnaMAF commented 7 years ago

Hi @afawcett, Sorry for the delay in my response! We just tried upgrading to the latest version of the DLRS package. When I tried to deactivate a user just now, I get the following error (different from pre-upgrade):

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger dlrs_UserTrigger caused an unexpected exception, contact your administrator: dlrs_UserTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 003i000003nTzMUAA0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User: []: (dlrs)

image

Do you know why this might be the case?

Thanks so much for your help!

Regards, Aparna

afawcett commented 7 years ago

This problem stems from a platform limitation described here.

So i think your step c above is hitting the follow documented scenario, so your updating the ProfileId.

You can update a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if the following fields are not also updated: UserRoleId IsActive ForecastEnabled IsPortalEnabled Username ProfileId

As you can see, there is a documented code work around. Unfortunatly the code in the tool needs to change to support this. Making this an enhancement request. I'm happy to consider it as such.

There is one strange thing though, you say it "was" working very well before. Which i don't understand, since according to what i have figured out, this should never have worked. What changed between it working and not working?

afawcett commented 7 years ago

Is there any way you can apply the Profile as a separate step?

aparnaMAF commented 7 years ago

Hi Andy,

Thanks for your response! A few things:

1- I am unable to create a user without setting a profile; similarly, I'm unable to remove the profile from the user before deactivating it. This is just going through the standard UI -- are you able to create a profile-less user, somehow?

2- I did, in fact, say it was working very well. Hearing your description of the problem, I'm wondering whether that was just a period of time when we didn't have to create any users? We only started creating client users automatically when I created this case, and we create partner users far more sporadically. So sorry .. this statement was probably misleading on my part!

3- Really appreciate your willingness to put a workaround into the code! Would you like me to submit a separate enhancement request, or would you prefer to leave everything consolidated in this thread?

Thanks so much!

Regards, Aparna

afawcett commented 7 years ago

RE: 1, the way the Salesforce docs read it suggests that 'creating a user record' should not invoke the platform restriction causing our problem here. But updating will. So i am wondering the profile was being applied by an update vs on creation? But it sounds like its on creation. There is no custom Apex code involved in the user creation is there?

RE: 2, Ah ok understood.

RE: 3, Sure, just marked this as an enhancement. I'll add the priority flag also, so it bubbles up when i have my next focused dev time on the tool. Especially since its impacting your operations, i thought i would assign this priority. Keep in mind though, my community time is variable, but i'll do my best! šŸ‘

bbromberg commented 2 years ago

Hi @afawcett! Thanks so much for all your amazing work on this application, it has been a life saver to me time and time again and I don't know what I'd do without it!

I wanted to reach out and ask about this issue. I'm encountering the same thing, and wondering if you have any better workaround suggestions than deactivating the rollup when I need to create a new Portal user?

My rollup is a fairly simple one, to populate a User ID onto Contact records (see image). This is making it possible for a PB automation to create Tasks and assign them to a portal User associated with a Contact related to the record that triggers the automation (which is awesome!). But now when I try to enable new Portal users, I'm running into the same DML operation error as the user above.

Please let me know if you can recommend any other fixes! Thank you again! Manage-Lookup-Rollup-Summaries-Salesforce