• sriram k 15
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 3
    Replies
trigger Count_Contact_Records on Contact (after insert,after update,after delete,after undelete) {   
   
 
    set<id> accIds = new set<id>();
    if(trigger.isafter && (trigger.isinsert || trigger.isupdate || trigger.isundelete)){
        for(contact con : Trigger.new){
            if(con.accountid != null){
                accIds.add(con.accountid);
            }
        }
    }
    if(trigger.isafter && (trigger.isupdate || trigger.isdelete)){
        for(contact con : Trigger.old){
            if(con.accountid != null){
                accIds.add(con.accountid);
            }
        }
    }
    if(accIds != null){
      list<Account> lstaccs =[select id,name,Total_child_records__c,(select id, name from contacts) from Account where id in :accIds];
        for(account acc : lstaccs)
        {
            acc.Total_child_records__c = acc.contacts.size();
        }
        update lstaccs;
    
    }



        
}
Trigger Class >>>>>>>>

rigger TriggerOnUser on User (before update) {

    HandlerClass_User.userisDeactivated(trigger.new, trigger.oldMap);
}

Handler Class >>>>>>>>>

public class HandlerClass_User {

    public static void userisDeactivated(list<User> newUser, Map<Id,User>OldMap){
        set<Id> setOfDeactivedIds = new Set<Id>();
        Map<Id,list<PermissionSetAssignment>> userPSAMap = new Map<Id,list<PermissionSetAssignment>>();        
        list<PermissionSetAssignment> PSAuserDelete = new list<PermissionSetAssignment>();        
        for(User u : newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                setOfDeactivedIds.add(u.id);
                system.debug('Deactived User Id is'+u.id);
            }
        }
        
        list<PermissionSetAssignment> listOfPremissionSetAssign = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment
                                                Where AssigneeId IN : setOfDeactivedIds AND Permissionset.IsOwnedByProfile =false];
        List<PermissionSetLicenseAssign> permSetsLiceUsers = [SELECT Id, AssigneeId from PermissionSetLicenseAssign where AssigneeId IN: setOfDeactivedIds];
        List<GroupMember> grpMemUsers = [SELECT Id, GroupId, UserOrGroupId FROM GroupMember WHERE UserorGroupId IN : setOfDeactivedIds]; 
        for(User u:newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                for(PermissionSetAssignment psa : listOfPremissionSetAssign){
                    if(psa.AssigneeId == u.id){
                        if(userPSAMap.containsKey(u.id)){
                            userPSAMap.get(u.id).add(psa);
                        }
                        else{
                            userPSAMap.put(u.id, new list<PermissionSetAssignment> {psa});
                        }
                    }
                }
                
               }
        }
        
        system.debug('Get user Id and PSA'+userPSAMap);
        
        for(User u: newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                if(userPSAMap.containsKey(u.id)){
                    PSAuserDelete.addAll(userPSAMap.get(u.id));
                }
                              
            }
        }
        system.debug('List Of PSA to Delete for Users'+PSAuserDelete);
        
    
    if(!PSAuserDelete.isEmpty()){
        delete PSAuserDelete;
    }
    
        
}
}

Test Class >>>>>>>>>>>>

@isTest
public class TestHandlerClass_User {

    @isTest
    public static void TestDeactiveUser(){
        Profile p = [SELECT Id,Name FROM Profile where Name='sys Admin'];
        UserRole u1 =[SELECT Id,Name FROM UserRole where Name='Manager'];
        
        User u = new User(UserName='kallam.salesforce132@gmail.com', Alias = 'rkall', Email='kallam.salesforce12@gmail.com', 
                          EmailEncodingKey='UTF-8', LastName='Kallam', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US',ProfileId = p.Id,UserRoleId =u1.id,TimeZoneSidKey='America/Los_Angeles',isActive =true);
       // insert u;
        list<User> lu = new List<User>();
        lu.add(u);   
        insert lu;
        system.debug('list users'+lu);
        system.debug('list users'+lu[0].id);
         Id AssiId = lu[0].id;
        Map<Id,User> Mu = new Map<Id,User>();
        Mu.put(lu[0].id, lu[0]);
        system.debug('Map Key'+ Mu.keySet());        
        
        list<PermissionSetAssignment> lpsa = [SELECT Id, AssigneeId, PermissionSetId,PermissionSet.Name FROM PermissionSetAssignment where PermissionSet.Name like '%Assign%'];
          List<PermissionSetAssignment> psl = new List<PermissionSetAssignment>();
            PermissionSetAssignment psa =new PermissionSetAssignment(PermissionSetId = lpsa[0].PermissionSetId, AssigneeId = lu[0].Id);
            psl.add(psa);
            insert psl;  
        
        lu[0].IsActive= false;
        update lu;
        
      //  HandlerClass_User hc = new HandlerClass_User();
        HandlerClass_User.userisDeactivated(lu,Mu);
       List<PermissionSetAssignment> lpsa1 = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment where AssigneeId =: Mu.keySet() ];
         system.debug('****'+lpsa1.size());
         System.assertEquals(lpsa1.isEmpty(), true);
            
        
         
    }
    
}


 
Apex Code>>>>>>>>>

public class HandlerClass_User {

    public static void userisDeactivated(list<User> newUser, Map<Id,User>OldMap){
        set<Id> setOfDeactivedIds = new Set<Id>();
        Map<Id,list<PermissionSetAssignment>> userPSAMap = new Map<Id,list<PermissionSetAssignment>>();        
        list<PermissionSetAssignment> PSAuserDelete = new list<PermissionSetAssignment>();        
        for(User u : newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                setOfDeactivedIds.add(u.id);
                system.debug('Deactived User Id is'+u.id);
            }
        }
        
        list<PermissionSetAssignment> listOfPremissionSetAssign = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment
                                                Where AssigneeId IN : setOfDeactivedIds AND Permissionset.IsOwnedByProfile =false];
        List<PermissionSetLicenseAssign> permSetsLiceUsers = [SELECT Id, AssigneeId from PermissionSetLicenseAssign where AssigneeId IN: setOfDeactivedIds];
        List<GroupMember> grpMemUsers = [SELECT Id, GroupId, UserOrGroupId FROM GroupMember WHERE UserorGroupId IN : setOfDeactivedIds]; 
        for(User u:newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                for(PermissionSetAssignment psa : listOfPremissionSetAssign){
                    if(psa.AssigneeId == u.id){
                        if(userPSAMap.containsKey(u.id)){
                            userPSAMap.get(u.id).add(psa);
                        }
                        else{
                            userPSAMap.put(u.id, new list<PermissionSetAssignment> {psa});
                        }
                    }
                }
                
               }
        }
        
        system.debug('Get user Id and PSA'+userPSAMap);
        
        for(User u: newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                if(userPSAMap.containsKey(u.id)){
                    PSAuserDelete.addAll(userPSAMap.get(u.id));
                }
                              
            }
        }
        system.debug('List Of PSA to Delete for Users'+PSAuserDelete);
        
    
    if(!PSAuserDelete.isEmpty()){
        delete PSAuserDelete;
    }
    
        
}
}



Test Coverage>>>>>>>>>>>

@isTest
public class TestHandlerClass_User {

    @isTest
    public static void TestDeactiveUser(){
        Profile p = [SELECT Id,Name FROM Profile where Name='sys Admin'];
        UserRole u1 =[SELECT Id,Name FROM UserRole where Name='Manager'];
        
        User u = new User(UserName='kallam.salesforce132@gmail.com', Alias = 'rkall', Email='kallam.salesforce12@gmail.com', 
                          EmailEncodingKey='UTF-8', LastName='Kallam', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US',ProfileId = p.Id,UserRoleId =u1.id,TimeZoneSidKey='America/Los_Angeles',isActive =true);
        insert u;
        list<User> lu = new List<User>();
        lu.add(u);        
        Map<Id,User> Mu = new Map<Id,User>();
        Mu.put(u.id, u);
        
        
        list<PermissionSetAssignment> lpsa = [SELECT Id, AssigneeId, PermissionSetId,PermissionSet.Name FROM PermissionSetAssignment where PermissionSet.Name like '%Assign%'];
          List<PermissionSetAssignment> psl = new List<PermissionSetAssignment>();
            PermissionSetAssignment psa =new PermissionSetAssignment(PermissionSetId = lpsa[0].PermissionSetId, AssigneeId = u.Id);
            psl.add(psa);
            insert psl;  
        
        u.IsActive= false;
        update u;
        
      //  HandlerClass_User hc = new HandlerClass_User();
        HandlerClass_User.userisDeactivated(lu,Mu);
         System.assertEquals(psl.isEmpty(), true);
            
        
         
    }
    
}

 

Trigger> 

trigger TriggerOnUserToAddPermissionSet on User (After insert) {

    if(trigger.isAfter && trigger.isInsert){
        UserHandlerClass.AfterInsert(trigger.new);
    }
}

public class UserHandlerClass {

    public static void AfterInsert(list<user> listOfUsers){
        set<Id> setOfUserIds = new Set<Id>();
        String PermissionSetName = 'Assign_Lead_Permissions';
        for(User us : listOfUsers){
            if(us.IsActive == true && us.Profile.Name == 'sys Admin'){
                setOfUserIds.add(us.Id);
                system.debug('Users are added to Set');
            }
        }
        
        PermissionSet permissionSetId = [select id,Name from PermissionSet where name ='Assign_Lead_Permissions'];
        
   
        list<PermissionSetAssignment> listOfPermissionSetAssignmentToUsers = new list<PermissionSetAssignment>();
        
        list<User> listOfUser = [SELECT Id,Name from User where Id IN:setOfUserIds ];
        
        for(User u:listOfUser ){
             
            PermissionSetAssignment Assign = new PermissionSetAssignment();
                Assign.AssigneeId = u.Id;
                Assign.PermissionSetId = permissionSetId.id;
                listOfPermissionSetAssignmentToUsers.add(Assign);
                system.debug('Assigned to Users');
            }
        if(!listOfPermissionSetAssignmentToUsers.isEmpty()){
            
            insert listOfPermissionSetAssignmentToUsers;
        }
        
        
    }
}

I am getting error with this Query. Can Anyone help to fetch details?

Select Id,Name,Amount from Opportunity where createdDate = Today AND CreatedById = UserInfo.getUserId()

trigger duplication_Rules_Contact on Contact (before insert,before update,after undelete) {

    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
    if(trigger.isAfter && trigger.isUndelete){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
}

public class ContactTriggerHepler {

    
    public static void duplicateRules(List<Contact> licon){
        set<String> NewEmail = new set<String>();
        set<String> ExistingEmail = new Set<String>();
        set<String> NlastName = new set<String>();
        set<String> ExistingLastName = new set<String>();
        for(Contact con : licon){
            if(con.Email != null)  {
                NewEmail.add(con.Email);
            }
            if(con.LastName != null){
                NlastName.add(con.LastName);
            }
        }
        
        list<Contact> existingContact = [SELECT Id,Email,Phone,LastName from Contact where (Email =: NewEmail AND Email != null) AND
                                                                                  (LastName =: NlastName AND LastName != null) ];
        for(Contact co : existingContact){
            ExistingEmail.add(co.Email);
            ExistingLastName.add(co.LastName);
        }
        
        for(Contact  c : licon){
            if(ExistingEmail.contains(c.email)){
                c.addError('Duplicate Email in Contact');
            }
            else{
                ExistingEmail.add(c.Email);
            }
        }
        for(Contact  c : licon){
            if(ExistingLastName.contains(c.LastName)){
                c.addError('Duplicate LastName in Contact');
            }
            else{
                ExistingLastName.add(c.LastName);
            }
        }
    }
}
Trigger Class >>>>>>>>

rigger TriggerOnUser on User (before update) {

    HandlerClass_User.userisDeactivated(trigger.new, trigger.oldMap);
}

Handler Class >>>>>>>>>

public class HandlerClass_User {

    public static void userisDeactivated(list<User> newUser, Map<Id,User>OldMap){
        set<Id> setOfDeactivedIds = new Set<Id>();
        Map<Id,list<PermissionSetAssignment>> userPSAMap = new Map<Id,list<PermissionSetAssignment>>();        
        list<PermissionSetAssignment> PSAuserDelete = new list<PermissionSetAssignment>();        
        for(User u : newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                setOfDeactivedIds.add(u.id);
                system.debug('Deactived User Id is'+u.id);
            }
        }
        
        list<PermissionSetAssignment> listOfPremissionSetAssign = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment
                                                Where AssigneeId IN : setOfDeactivedIds AND Permissionset.IsOwnedByProfile =false];
        List<PermissionSetLicenseAssign> permSetsLiceUsers = [SELECT Id, AssigneeId from PermissionSetLicenseAssign where AssigneeId IN: setOfDeactivedIds];
        List<GroupMember> grpMemUsers = [SELECT Id, GroupId, UserOrGroupId FROM GroupMember WHERE UserorGroupId IN : setOfDeactivedIds]; 
        for(User u:newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                for(PermissionSetAssignment psa : listOfPremissionSetAssign){
                    if(psa.AssigneeId == u.id){
                        if(userPSAMap.containsKey(u.id)){
                            userPSAMap.get(u.id).add(psa);
                        }
                        else{
                            userPSAMap.put(u.id, new list<PermissionSetAssignment> {psa});
                        }
                    }
                }
                
               }
        }
        
        system.debug('Get user Id and PSA'+userPSAMap);
        
        for(User u: newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                if(userPSAMap.containsKey(u.id)){
                    PSAuserDelete.addAll(userPSAMap.get(u.id));
                }
                              
            }
        }
        system.debug('List Of PSA to Delete for Users'+PSAuserDelete);
        
    
    if(!PSAuserDelete.isEmpty()){
        delete PSAuserDelete;
    }
    
        
}
}

Test Class >>>>>>>>>>>>

@isTest
public class TestHandlerClass_User {

    @isTest
    public static void TestDeactiveUser(){
        Profile p = [SELECT Id,Name FROM Profile where Name='sys Admin'];
        UserRole u1 =[SELECT Id,Name FROM UserRole where Name='Manager'];
        
        User u = new User(UserName='kallam.salesforce132@gmail.com', Alias = 'rkall', Email='kallam.salesforce12@gmail.com', 
                          EmailEncodingKey='UTF-8', LastName='Kallam', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US',ProfileId = p.Id,UserRoleId =u1.id,TimeZoneSidKey='America/Los_Angeles',isActive =true);
       // insert u;
        list<User> lu = new List<User>();
        lu.add(u);   
        insert lu;
        system.debug('list users'+lu);
        system.debug('list users'+lu[0].id);
         Id AssiId = lu[0].id;
        Map<Id,User> Mu = new Map<Id,User>();
        Mu.put(lu[0].id, lu[0]);
        system.debug('Map Key'+ Mu.keySet());        
        
        list<PermissionSetAssignment> lpsa = [SELECT Id, AssigneeId, PermissionSetId,PermissionSet.Name FROM PermissionSetAssignment where PermissionSet.Name like '%Assign%'];
          List<PermissionSetAssignment> psl = new List<PermissionSetAssignment>();
            PermissionSetAssignment psa =new PermissionSetAssignment(PermissionSetId = lpsa[0].PermissionSetId, AssigneeId = lu[0].Id);
            psl.add(psa);
            insert psl;  
        
        lu[0].IsActive= false;
        update lu;
        
      //  HandlerClass_User hc = new HandlerClass_User();
        HandlerClass_User.userisDeactivated(lu,Mu);
       List<PermissionSetAssignment> lpsa1 = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment where AssigneeId =: Mu.keySet() ];
         system.debug('****'+lpsa1.size());
         System.assertEquals(lpsa1.isEmpty(), true);
            
        
         
    }
    
}


 

Trigger> 

trigger TriggerOnUserToAddPermissionSet on User (After insert) {

    if(trigger.isAfter && trigger.isInsert){
        UserHandlerClass.AfterInsert(trigger.new);
    }
}

public class UserHandlerClass {

    public static void AfterInsert(list<user> listOfUsers){
        set<Id> setOfUserIds = new Set<Id>();
        String PermissionSetName = 'Assign_Lead_Permissions';
        for(User us : listOfUsers){
            if(us.IsActive == true && us.Profile.Name == 'sys Admin'){
                setOfUserIds.add(us.Id);
                system.debug('Users are added to Set');
            }
        }
        
        PermissionSet permissionSetId = [select id,Name from PermissionSet where name ='Assign_Lead_Permissions'];
        
   
        list<PermissionSetAssignment> listOfPermissionSetAssignmentToUsers = new list<PermissionSetAssignment>();
        
        list<User> listOfUser = [SELECT Id,Name from User where Id IN:setOfUserIds ];
        
        for(User u:listOfUser ){
             
            PermissionSetAssignment Assign = new PermissionSetAssignment();
                Assign.AssigneeId = u.Id;
                Assign.PermissionSetId = permissionSetId.id;
                listOfPermissionSetAssignmentToUsers.add(Assign);
                system.debug('Assigned to Users');
            }
        if(!listOfPermissionSetAssignmentToUsers.isEmpty()){
            
            insert listOfPermissionSetAssignmentToUsers;
        }
        
        
    }
}

trigger duplication_Rules_Contact on Contact (before insert,before update,after undelete) {

    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
    if(trigger.isAfter && trigger.isUndelete){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
}

public class ContactTriggerHepler {

    
    public static void duplicateRules(List<Contact> licon){
        set<String> NewEmail = new set<String>();
        set<String> ExistingEmail = new Set<String>();
        set<String> NlastName = new set<String>();
        set<String> ExistingLastName = new set<String>();
        for(Contact con : licon){
            if(con.Email != null)  {
                NewEmail.add(con.Email);
            }
            if(con.LastName != null){
                NlastName.add(con.LastName);
            }
        }
        
        list<Contact> existingContact = [SELECT Id,Email,Phone,LastName from Contact where (Email =: NewEmail AND Email != null) AND
                                                                                  (LastName =: NlastName AND LastName != null) ];
        for(Contact co : existingContact){
            ExistingEmail.add(co.Email);
            ExistingLastName.add(co.LastName);
        }
        
        for(Contact  c : licon){
            if(ExistingEmail.contains(c.email)){
                c.addError('Duplicate Email in Contact');
            }
            else{
                ExistingEmail.add(c.Email);
            }
        }
        for(Contact  c : licon){
            if(ExistingLastName.contains(c.LastName)){
                c.addError('Duplicate LastName in Contact');
            }
            else{
                ExistingLastName.add(c.LastName);
            }
        }
    }
}