Skip to main content
added 509 characters in body
Source Link

Trigger : trigger UpdateUserProfileTrigger on Contact (before update) {

UpdateUserProfileTriggerHandler handler = new UpdateUserProfileTriggerHandler(); handler.userprofile(); // UpdateUserProfileTriggerHandler.updateProfile(); /* string id = ''; for(Contact c: Trigger.new){ id = c.Id;
}

if(id!=''){
     UpdateUserProfileTriggerHandler handler = new UpdateUserProfileTriggerHandler();
     handler.userprofile(id);
}*/

Trigger : trigger UpdateUserProfileTrigger on Contact (before update) {

UpdateUserProfileTriggerHandler handler = new UpdateUserProfileTriggerHandler(); handler.userprofile(); // UpdateUserProfileTriggerHandler.updateProfile(); /* string id = ''; for(Contact c: Trigger.new){ id = c.Id;
}

if(id!=''){
     UpdateUserProfileTriggerHandler handler = new UpdateUserProfileTriggerHandler();
     handler.userprofile(id);
}*/

public void userprofile(){ if (!System.isBatch() && !System.isFuture()) { UpdateUserProfileTriggerHandler.updateProfile (); }

 public void userprofile(){
        if (!System.isBatch() && !System.isFuture())
              {
                  UpdateUserProfileTriggerHandler.updateProfile ();
              }
        
    }
 
    
    @future
    public static void updateProfile(){
       System.debug('inside update profile');
        List<id> userids = new list<id>();
        
        Profile porfileMember = [SELECT Id FROM Profile WHERE Name='Member' limit 1];
        Profile porfileNonMember = [SELECT Id FROM Profile WHERE Name='NonMember' limit 1];
        List<User> Uv = [Select ProfileId,IsActive,contactId from User];
        Map<Id, User> mapusers = new Map<Id, User>();
        for(user u: Uv){
             userids.add(u.contactId);
             mapusers.put(u.contactId,u);
         }
        List<contact> cntList = [select id, Member__c  from contact where id IN:userids];   
         System.debug('before for loop update profile');
        List<User> UpdateList = new List<User>();
        for(contact cnt: cntList){
             System.debug('TestingContact-' + cnt.id);
              if(mapusers.containsKey(cnt.id)) {
              
                 User usr = mapusers.get(cnt.id);
                   
                if(cnt.Member__c == false){
                    usr.ProfileId =porfileNonMember.Id;
                    usr.IsActive = True;
                    UpdateList.add(usr);
                    
                }
               else
                {
                    usr.ProfileId  =porfileMember.Id;
                    usr.IsActive = True;
                    UpdateList.add(usr);
                    
                }
              }

public void userprofile(){ if (!System.isBatch() && !System.isFuture()) { UpdateUserProfileTriggerHandler.updateProfile (); }

}


@future
public static void updateProfile(){
   System.debug('inside update profile');
    List<id> userids = new list<id>();
    
    Profile porfileMember = [SELECT Id FROM Profile WHERE Name='Member' limit 1];
    Profile porfileNonMember = [SELECT Id FROM Profile WHERE Name='NonMember' limit 1];
    List<User> Uv = [Select ProfileId,IsActive,contactId from User];
    Map<Id, User> mapusers = new Map<Id, User>();
    for(user u: Uv){
         userids.add(u.contactId);
         mapusers.put(u.contactId,u);
     }
    List<contact> cntList = [select id, Member__c  from contact where id IN:userids];   
     System.debug('before for loop update profile');
    List<User> UpdateList = new List<User>();
    for(contact cnt: cntList){
         System.debug('TestingContact-' + cnt.id);
          if(mapusers.containsKey(cnt.id)) {
          
             User usr = mapusers.get(cnt.id);
               
            if(cnt.Member__c == false){
                usr.ProfileId =porfileNonMember.Id;
                usr.IsActive = True;
                UpdateList.add(usr);
                
            }
           else
            {
                usr.ProfileId  =porfileMember.Id;
                usr.IsActive = True;
                UpdateList.add(usr);
                
            }
          }
 public void userprofile(){
        if (!System.isBatch() && !System.isFuture())
              {
                  UpdateUserProfileTriggerHandler.updateProfile ();
              }
        
    }
 
    
    @future
    public static void updateProfile(){
       System.debug('inside update profile');
        List<id> userids = new list<id>();
        
        Profile porfileMember = [SELECT Id FROM Profile WHERE Name='Member' limit 1];
        Profile porfileNonMember = [SELECT Id FROM Profile WHERE Name='NonMember' limit 1];
        List<User> Uv = [Select ProfileId,IsActive,contactId from User];
        Map<Id, User> mapusers = new Map<Id, User>();
        for(user u: Uv){
             userids.add(u.contactId);
             mapusers.put(u.contactId,u);
         }
        List<contact> cntList = [select id, Member__c  from contact where id IN:userids];   
         System.debug('before for loop update profile');
        List<User> UpdateList = new List<User>();
        for(contact cnt: cntList){
             System.debug('TestingContact-' + cnt.id);
              if(mapusers.containsKey(cnt.id)) {
              
                 User usr = mapusers.get(cnt.id);
                   
                if(cnt.Member__c == false){
                    usr.ProfileId =porfileNonMember.Id;
                    usr.IsActive = True;
                    UpdateList.add(usr);
                    
                }
               else
                {
                    usr.ProfileId  =porfileMember.Id;
                    usr.IsActive = True;
                    UpdateList.add(usr);
                    
                }
              }
Source Link

Changing profiles based on checkbox

I have member and non-member profiles. Based on a field(checkbox) on contact object the profile should change automatically. Currently it is doing the functionality but if multiple users change the field on multiple records, profile change is not happening because it is going to queue. How can i make it work without going to queue and change profiles.

Snippet:

public void userprofile(){ if (!System.isBatch() && !System.isFuture()) { UpdateUserProfileTriggerHandler.updateProfile (); }

}


@future
public static void updateProfile(){
   System.debug('inside update profile');
    List<id> userids = new list<id>();
    
    Profile porfileMember = [SELECT Id FROM Profile WHERE Name='Member' limit 1];
    Profile porfileNonMember = [SELECT Id FROM Profile WHERE Name='NonMember' limit 1];
    List<User> Uv = [Select ProfileId,IsActive,contactId from User];
    Map<Id, User> mapusers = new Map<Id, User>();
    for(user u: Uv){
         userids.add(u.contactId);
         mapusers.put(u.contactId,u);
     }
    List<contact> cntList = [select id, Member__c  from contact where id IN:userids];   
     System.debug('before for loop update profile');
    List<User> UpdateList = new List<User>();
    for(contact cnt: cntList){
         System.debug('TestingContact-' + cnt.id);
          if(mapusers.containsKey(cnt.id)) {
          
             User usr = mapusers.get(cnt.id);
               
            if(cnt.Member__c == false){
                usr.ProfileId =porfileNonMember.Id;
                usr.IsActive = True;
                UpdateList.add(usr);
                
            }
           else
            {
                usr.ProfileId  =porfileMember.Id;
                usr.IsActive = True;
                UpdateList.add(usr);
                
            }
          }