• Febe Nagy
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
Can anyone help please, I think I'm just getting lost  and going in circles with this test class.  No matter how I compare the new value with the old I still can't get the test to cover the comparison part in my trigger.  Please help me fix this.
*****************************************************************************************************
@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();

      	List<User> usrList = new List<User>();
        User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1];
        User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1];
         
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser11@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        
        User testUser4 = new User(alias = 'TstUsr2',Firstname='tst2', email='newuserDP2@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst112', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser112@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = 'idNumer02');
        //insert testUser1;
        usrList.add(testUser1);
        usrList.add(testUser4);
        insert usrList;
       
        System.runAs(testUser1){
            
            Account acc = new Account(Name = 'Febe01', 
                                      OwnerId = 'idNumer01',
                                      Previous_Account_Owner__c = 'idNumer01');
            insert acc;
            system.assert(acc.Previous_Account_Owner__c == acc.OwnerId);
                       
            acc.OwnerId = testUser1.Id;
            acc.Previous_Account_Owner__c = 'idNumer01';
            update acc;
            //system.assertEquals('idNumer01', acc.Previous_Account_Owner__c);
            system.assert(acc.Previous_Account_Owner__c != acc.OwnerId);
            
            system.debug('New account owner :  ' + acc.OwnerId + 'Old Account owner: ' + acc.Previous_Account_Owner__c);
            
            
        }
        
       List<Account> lstNewAcc = [select id, name, ownerId from Account where ownerId =:testUser2.Id];
       List<Account> lstOldAcc = [select id, name, ownerId from Account where ownerId =:testUser1.Id];
       map<Id, Account> oldmap = new map<Id, Account>();
       map<Id, Account> newmap = new map<Id, Account>();
            
            for(Account oldAccts : lstOldAcc)
            {
                oldmap.put(oldAccts.Id, oldAccts); 
            }
            
            for(Account newAct : lstNewAcc)
            {
                Account oldAc = oldmap.get(newAct.Id);
                if(newAct.OwnerId != oldAc.OwnerId){
                    newAct.Previous_Account_Owner__c = oldAc.OwnerId;
                    system.assert(newAct.OwnerId != oldAc.OwnerId);
                   	newmap.put(newAct.Id, newAct); 
                }
                
            }
            
            system.debug('***********************************  Account oldmap: '+ oldmap);
         test.stopTest();
    }
}

***************************************************************************************************
This is the trigger: 
trigger AccOwnerChange on Account (before update) {
        
        if(INTLStaticObjectHelper.isAccOwnerChangeExecuted){
            return;
        }
        INTLStaticObjectHelper.isAccOwnerChangeExecuted = true;
        Set<Id> setUserIds = new Set<Id>();
        Map<Id, User> mapUserObj = new Map<Id, User>();
        
        
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                setUserIds.add(a.OwnerId);
                setUserIds.add(oldObj.ownerId);
            }
                
        }
        system.debug('test ###' + setUserIds);
        if(setUserIds.size()>0){
            List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true];
            for(User usrObj : lstUser){
                mapUserObj.put(usrObj.Id, usrObj);
            }
        }
        system.debug('test ###' + mapUserObj);
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                a.Previous_Account_Owner__c = oldObj.ownerId;
                if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){
                    a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId;
                }
                if(mapUserObj.get(a.ownerId) != null){
                    a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId;
                }
            }
        }
Hi,

Can someone help, I have a low code coverage and it is because I can't get the test class to check if the account owner is the same as the old account owner.  Can someone tell me how can I do this when I'm only using the account object?  Thank you for your help.
trigger AccOwnerChange on Account (before update) {
        
        if(ObjectHelper.isAccOwnerChangeExecuted){
            return;
        }
        ObjectHelper.isAccOwnerChangeExecuted = true;
        Set<Id> setUserIds = new Set<Id>();
        Map<Id, User> mapUserObj = new Map<Id, User>();
        
        
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                setUserIds.add(a.OwnerId);
                setUserIds.add(oldObj.ownerId);
            }
                
        }
        system.debug('test ###' + setUserIds);
        if(setUserIds.size()>0){
            List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true];
            for(User usrObj : lstUser){
                mapUserObj.put(usrObj.Id, usrObj);
            }
        }
        system.debug('test ###' + mapUserObj);
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                a.Previous_Account_Owner__c = oldObj.ownerId;
                if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){
                    a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId;
                }
                if(mapUserObj.get(a.ownerId) != null){
                    a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId;
                }
            }
        }
**********************************************************************************************
Test class:

@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();

      
        User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1];
        User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1];
         
    
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', localesidkey='en_US', profileid = 'xxxx', timezonesidkey='America/Los_Angeles', username='newuser11@testorg.com', UserRoleId = 'xxxx',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        insert testUser1;
        
        System.runAs(testUser1){
            
            Account acc = new Account(Name = 'Febe01', OwnerId = testUser2.Id);
            insert acc;
            acc.OwnerId = testUser1.Id;
            //acc.Name = 'tester01';
            //acc.OwnerId = 'xxxx';
            //acc.Previous_Account_Owner__c = testUser2.Id;
            //acc.Previous_Account_Owner_s_Manager__c = testUser1.ManagerId;
            //acc.New_Account_Owner_s_Manager__c = testUser2.ManagerId;
            update acc;
            testUser1.Id = testUser3.Id;
            acc.OwnerId = testUser1.Id;
            update acc;
            update testUser1;
            
        }
       
         test.stopTest();
    }
}

 Here is my code trigger and test
Hi,

Can someone help, I keep hitting the same error, insert failed. DML operation on setup object is not permitted after you have updated a non-setup object(or vice versa: User, original object: Account:[].  I am trying to test that the account owner is the same, that the account owner is not the same anymore, so the account owner has changed. Help please.  This is my test class:

@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();
        
	Account acc = new Account(Name = 'test01', OwnerId = 'xxxxxxx001');

    insert acc;
      
        User testUser2 = [Select Id from User where IsActive = true limit 1];
        User testUser3 = [Select Id from User where IsActive = true limit 1];
         
    
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', localesidkey='en_US', profileid = 'xxxxxxx006', timezonesidkey='America/Los_Angeles', username='newuser11@testorg.com', UserRoleId = 'xxxxxxx006',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        insert testUser1;
        
        System.runAs(testUser1){
        
        acc.OwnerId = testUser1.Id;
        acc.OwnerId = 'xxxxxxx001';
        acc.Previous_Account_Owner__c = 'xxxxxxx001';
        acc.Previous_Account_Owner_s_Manager__c = testUser1.ManagerId;
        acc.New_Account_Owner_s_Manager__c = testUser2.ManagerId;
        acc.OwnerId = testUser2.Id;
        }
        update acc;
        /*System.runAs(u){
			Account acc1 = new Account();
            acc1.OwnerId = 'xxxxxxx001';
            acc1.Name = 'Test091';
            insert acc1;
            
            acc= [select id, ownerId from Account where id=:acc.id];
            system.assert(acc1.OwnerId == acc.OwnerId);
        }*/
        test.stopTest();
    }
}
Can anyone help please, I think I'm just getting lost  and going in circles with this test class.  No matter how I compare the new value with the old I still can't get the test to cover the comparison part in my trigger.  Please help me fix this.
*****************************************************************************************************
@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();

      	List<User> usrList = new List<User>();
        User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1];
        User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1];
         
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser11@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        
        User testUser4 = new User(alias = 'TstUsr2',Firstname='tst2', email='newuserDP2@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst112', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser112@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = 'idNumer02');
        //insert testUser1;
        usrList.add(testUser1);
        usrList.add(testUser4);
        insert usrList;
       
        System.runAs(testUser1){
            
            Account acc = new Account(Name = 'Febe01', 
                                      OwnerId = 'idNumer01',
                                      Previous_Account_Owner__c = 'idNumer01');
            insert acc;
            system.assert(acc.Previous_Account_Owner__c == acc.OwnerId);
                       
            acc.OwnerId = testUser1.Id;
            acc.Previous_Account_Owner__c = 'idNumer01';
            update acc;
            //system.assertEquals('idNumer01', acc.Previous_Account_Owner__c);
            system.assert(acc.Previous_Account_Owner__c != acc.OwnerId);
            
            system.debug('New account owner :  ' + acc.OwnerId + 'Old Account owner: ' + acc.Previous_Account_Owner__c);
            
            
        }
        
       List<Account> lstNewAcc = [select id, name, ownerId from Account where ownerId =:testUser2.Id];
       List<Account> lstOldAcc = [select id, name, ownerId from Account where ownerId =:testUser1.Id];
       map<Id, Account> oldmap = new map<Id, Account>();
       map<Id, Account> newmap = new map<Id, Account>();
            
            for(Account oldAccts : lstOldAcc)
            {
                oldmap.put(oldAccts.Id, oldAccts); 
            }
            
            for(Account newAct : lstNewAcc)
            {
                Account oldAc = oldmap.get(newAct.Id);
                if(newAct.OwnerId != oldAc.OwnerId){
                    newAct.Previous_Account_Owner__c = oldAc.OwnerId;
                    system.assert(newAct.OwnerId != oldAc.OwnerId);
                   	newmap.put(newAct.Id, newAct); 
                }
                
            }
            
            system.debug('***********************************  Account oldmap: '+ oldmap);
         test.stopTest();
    }
}

***************************************************************************************************
This is the trigger: 
trigger AccOwnerChange on Account (before update) {
        
        if(INTLStaticObjectHelper.isAccOwnerChangeExecuted){
            return;
        }
        INTLStaticObjectHelper.isAccOwnerChangeExecuted = true;
        Set<Id> setUserIds = new Set<Id>();
        Map<Id, User> mapUserObj = new Map<Id, User>();
        
        
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                setUserIds.add(a.OwnerId);
                setUserIds.add(oldObj.ownerId);
            }
                
        }
        system.debug('test ###' + setUserIds);
        if(setUserIds.size()>0){
            List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true];
            for(User usrObj : lstUser){
                mapUserObj.put(usrObj.Id, usrObj);
            }
        }
        system.debug('test ###' + mapUserObj);
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                a.Previous_Account_Owner__c = oldObj.ownerId;
                if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){
                    a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId;
                }
                if(mapUserObj.get(a.ownerId) != null){
                    a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId;
                }
            }
        }
Hi,

Can someone help, I have a low code coverage and it is because I can't get the test class to check if the account owner is the same as the old account owner.  Can someone tell me how can I do this when I'm only using the account object?  Thank you for your help.
trigger AccOwnerChange on Account (before update) {
        
        if(ObjectHelper.isAccOwnerChangeExecuted){
            return;
        }
        ObjectHelper.isAccOwnerChangeExecuted = true;
        Set<Id> setUserIds = new Set<Id>();
        Map<Id, User> mapUserObj = new Map<Id, User>();
        
        
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                setUserIds.add(a.OwnerId);
                setUserIds.add(oldObj.ownerId);
            }
                
        }
        system.debug('test ###' + setUserIds);
        if(setUserIds.size()>0){
            List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true];
            for(User usrObj : lstUser){
                mapUserObj.put(usrObj.Id, usrObj);
            }
        }
        system.debug('test ###' + mapUserObj);
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                a.Previous_Account_Owner__c = oldObj.ownerId;
                if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){
                    a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId;
                }
                if(mapUserObj.get(a.ownerId) != null){
                    a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId;
                }
            }
        }
**********************************************************************************************
Test class:

@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();

      
        User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1];
        User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1];
         
    
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', localesidkey='en_US', profileid = 'xxxx', timezonesidkey='America/Los_Angeles', username='newuser11@testorg.com', UserRoleId = 'xxxx',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        insert testUser1;
        
        System.runAs(testUser1){
            
            Account acc = new Account(Name = 'Febe01', OwnerId = testUser2.Id);
            insert acc;
            acc.OwnerId = testUser1.Id;
            //acc.Name = 'tester01';
            //acc.OwnerId = 'xxxx';
            //acc.Previous_Account_Owner__c = testUser2.Id;
            //acc.Previous_Account_Owner_s_Manager__c = testUser1.ManagerId;
            //acc.New_Account_Owner_s_Manager__c = testUser2.ManagerId;
            update acc;
            testUser1.Id = testUser3.Id;
            acc.OwnerId = testUser1.Id;
            update acc;
            update testUser1;
            
        }
       
         test.stopTest();
    }
}

 Here is my code trigger and test
I am trying to create a trigger that will count activities (tasks) on leads.
My code is below....

When saving I am getting the following error....

Error: Compile Error: unexpected token: '}' at line 30 column 1

But if I remove the curly bracket I get a different error

Error: Compile Error: unexpected token: '<EOF>' at line 30 column 0


trigger TaskUpdateLead on Task (after delete, after insert, after undelete, after update)
{
Set<ID> LeadIds = new Set<ID>();

//We only care about tasks linked to Leads.

String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();

//Add any Lead ids coming from the new data

if (Trigger.new != null) {
    for (Task t : Trigger.new) {
     if (t.WhatId != null && string.valueOf(t.whatId).startsWith(leadprefix) )
         {LeadIds.add(t.whatId);
      }
   }
}
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)

if (Trigger.old != null) {
    for (Task t : Trigger.old) {
     if (t.WhatId != null && String.valueOf(t.whatId).startsWith(leadprefix) )
         { LeadIds.add(t.whatId);
      }
   }
}
if (LeadIds.size() > 0)

Lead.Activity_Count__c.updateLeadCount<LeadIds>
}


Any guidance is very much appreciated.