My requirement is that sales reps cannot create or edit accounts but we want to enable them to convert leads when there is an existing account in place, in order to create new contacts and Opportunities under them.
- I have written a custom Lead Convert page that allows them to lookup an existing Account and then the controller code will call the database.leadconvert() method to convert the lead to that Account. However SF requires that the user running this operation has edit rights on the Account in order to convert a lead under it, even if I declare the class as without sharing. Shoot.
- I next tried to give them edit perm on the Account and then lock down anything I could to prevent them from editing accounts - removed the Edit button from layout, made all fields on the layout readonly etc. But this is a pain and there are some fields we just can't lock down, like Name and Owner. It allowed them to convert but too many bad side effects.
My next brain wave was to insert a permission set assignment record giving them edit permission, convert the lead and then delete the permission set assignment. This seemed awesome until I hit the Mixed DML error when trying to convert the lead after inserting the Permission Set Assignment.
Any ideas on how to get around the mixed DML? I don't really want any part of this to be @future since I need to show the user any error messages from the lead conversion operation or take them to the Account page after it completes.// if user doesn't have edit rights on Account, add the permission set now if (!Account.sObjectType.getDescribe().isupdateable()) { permAdd = new PermissionSetAssignment(PermissionSetId = '0PS80000000blLl', AssigneeId = UserInfo.getUserId()); insert permAdd; } // prepare lead conversion here...snipped for brevity //convert the lead Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvertObj); if (permAdd != null) delete permAdd;