Skip to main content
added 181 characters in body
Source Link
Dana Griff
  • 1.6k
  • 11
  • 31
  • 59

The second assert fails, and seems like the opportunity owner is not changed at all. in the UI - When the account records transferred to another user, how can i simulatethe associated open opportunities automatically transferred to the new account owner change like they do inwithout firing the UI? opportunity trigger.

(Whenever an account owner is changed, all the opportunities owners are changed to the same one, didnt happen here)

how can i simulate the same process in a test class?

The second assert fails, and seems like the opportunity owner is not changed at all, how can i simulate the account owner change like they do in the UI? (Whenever an account owner is changed, all the opportunities owners are changed to the same one, didnt happen here)

The second assert fails, and seems like the opportunity owner is not changed at all. in the UI - When the account records transferred to another user, the associated open opportunities automatically transferred to the new account owner without firing the opportunity trigger.

(Whenever an account owner is changed, all the opportunities owners are changed to the same one, didnt happen here)

how can i simulate the same process in a test class?

added 79 characters in body; edited tags; edited title
Source Link
Dana Griff
  • 1.6k
  • 11
  • 31
  • 59

writing Simulating Account Owner Change in test for future methodclass

The second assert fails, and seems like the opportunity owner is not changed backat all, andhow can i simulate the futureaccount owner change like they do in the UI? (Whenever an account owner is not running. i dont really know howchanged, all the opportunities owners are changed to run this future method (looked at SF documentationthe same one, didnt happen here).

writing test for future method

The second assert fails, and seems like the opportunity owner is not changed back, and the future is not running. i dont really know how to run this future method (looked at SF documentation).

Simulating Account Owner Change in test class

The second assert fails, and seems like the opportunity owner is not changed at all, how can i simulate the account owner change like they do in the UI? (Whenever an account owner is changed, all the opportunities owners are changed to the same one, didnt happen here)

added 341 characters in body
Source Link
Dana Griff
  • 1.6k
  • 11
  • 31
  • 59

that's the test i wrote: (UPDATED AFTER YOUR SUGGESTIONS)

@isTest private static void testavoidOpportunityOwnerChangeOnOwnerChange()
    {
        Account accountTest = TestUtil.getAccountByName('AccountTestAvoid');
        insert accountTest; 
        
        Opportunity testOpp = TestUtil.getOpportunityByNameAndStage('TestOne', 'Customer Confirmed Quote', accountTest.id);
        testOpp.type = 'Renewal Business';
        insert testOpp;
        
        Account acc = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity opp = [SELECT id,name,ownerId from opportunity limit 1];
        
        Profile p = [SELECT Id FROM Profile where name ='system administrator' limit 1]; 
        User usr = TestUtil.getUserByNameAndProfileId('test',p.Id);
        insert usr ;
        
        accountTest.ownerId = usr.id;
        
        Test.startTest();
         update accountTest;
        Test.stopTest();
     
        Account accRes = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity oppRes = [SELECT id,name,ownerId from opportunity limit 1];
        
        System.debug('accRes.OwnerId: ' + accRes.OwnerId);
        System.debug('acc.OwnerId: ' + acc.OwnerId);
        System.debug('oppRes.OwnerId: ' + oppRes.OwnerId);
        System.debug('opp.OwnerId: ' + opp.OwnerId);
        System.AssertNotEquals(accRes.IdOwnerId,acc.IdOwnerId);
        System.AssertNotEquals(oppRes.IdOwnerId,opp.IdOwnerId);
    }

that's the test i wrote:

@isTest private static void testavoidOpportunityOwnerChangeOnOwnerChange()
    {
        Account accountTest = TestUtil.getAccountByName('AccountTestAvoid');
        insert accountTest; 
        
        Opportunity testOpp = TestUtil.getOpportunityByNameAndStage('TestOne', 'Customer Confirmed Quote', accountTest.id);
        testOpp.type = 'Renewal Business';
        insert testOpp;
        
        Account acc = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity opp = [SELECT id,name,ownerId from opportunity limit 1];
        
        Profile p = [SELECT Id FROM Profile where name ='system administrator' limit 1]; 
        User usr = TestUtil.getUserByNameAndProfileId('test',p.Id);
        insert usr ;
        
        accountTest.ownerId = usr.id;
        update accountTest;
     
        Account accRes = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity oppRes = [SELECT id,name,ownerId from opportunity limit 1];
        
        
        System.AssertNotEquals(accRes.Id,acc.Id);
        System.AssertNotEquals(oppRes.Id,opp.Id);
    }

that's the test i wrote: (UPDATED AFTER YOUR SUGGESTIONS)

@isTest private static void testavoidOpportunityOwnerChangeOnOwnerChange()
    {
        Account accountTest = TestUtil.getAccountByName('AccountTestAvoid');
        insert accountTest; 
        
        Opportunity testOpp = TestUtil.getOpportunityByNameAndStage('TestOne', 'Customer Confirmed Quote', accountTest.id);
        testOpp.type = 'Renewal Business';
        insert testOpp;
        
        Account acc = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity opp = [SELECT id,name,ownerId from opportunity limit 1];
        
        Profile p = [SELECT Id FROM Profile where name ='system administrator' limit 1]; 
        User usr = TestUtil.getUserByNameAndProfileId('test',p.Id);
        insert usr ;
        
        accountTest.ownerId = usr.id;
        
        Test.startTest();
         update accountTest;
        Test.stopTest();
     
        Account accRes = [SELECT Id, Name,OwnerID from Account Where name = 'AccountTestAvoid' limit 1];
        Opportunity oppRes = [SELECT id,name,ownerId from opportunity limit 1];
        
        System.debug('accRes.OwnerId: ' + accRes.OwnerId);
        System.debug('acc.OwnerId: ' + acc.OwnerId);
        System.debug('oppRes.OwnerId: ' + oppRes.OwnerId);
        System.debug('opp.OwnerId: ' + opp.OwnerId);
        System.AssertNotEquals(accRes.OwnerId,acc.OwnerId);
        System.AssertNotEquals(oppRes.OwnerId,opp.OwnerId);
    }
Source Link
Dana Griff
  • 1.6k
  • 11
  • 31
  • 59
Loading