Problem:
Updating Custom Setting
values after user record is created or updated
Custom Setting API Name : ActiveUser__c
Field
Name : Name(Standard
Field) - It contains profile name
ActiveUsers__c: This field will be updated.
ActiveUsers__c: This field will be updated.
I have add once custom setting value,It contains Name: Customer Portal Manager Custom(Profile Name)
Whenever creating or updating user from above mentioned Profile with the Status Active or Inactive. ActiveUsers__c is the Field will be updated.
You can add multiple profile name in custom setting.
Solution:
To avoid mixed dml operation you need to insert record with @future annotation.
Apex Trigger:
To avoid mixed dml operation you need to insert record with @future annotation.
Apex Trigger:
- trigger usertoActive on User (after insert, after update)
- {
- Set<Id> usrID=new Set<Id>();
- Set<Id> ProID=new Set<Id>();
- for(User ur:Trigger.new)
- {
- usrID.add(ur.Id);
- ProID.add(ur.ProfileId);
- }
- activeUser.userAdd(usrID,ProID);
- }
Apex Class:
- global class activeUser
- {
- @future
- public static void userAdd(Set<Id> userIds,Set<Id> prid)
- {
- Profile pro=[select Name from Profile where Id=:prid];
- List<User> user = [select Id, IsActive from user where Id in:userIds and Profile.Name=:pro.Name];
- List<ActiveUser__c> licenseUpdate=[select ActiveUsers__c from ActiveUser__c where Name=:pro.Name];
- for (ActiveUser__c au: licenseUpdate)
- {
- for(User ur:user)
- {
- if(ur.IsActive)
- {
- au.ActiveUsers__c=au.ActiveUsers__c+1;
- }
- else
- {
- au.ActiveUsers__c=au.ActiveUsers__c-1;
- }
- }
- }
- update licenseUpdate;
- }
- }
Reference: Mixed DML
No comments:
Post a Comment