+ Start a Discussion
Vishnu7700Vishnu7700 

Need to improve test coverage from 25% to 100%

Hi,

I had trigger on account to update accountowner on realted list once changed.

Trigger

trigger reassignRelatedContactsnActivitesnOpportunities on Account (after update , after insert) {
 
      Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
      Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
      Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
      Contact[] contactUpdates = new Contact[0]; //Contact sObject to hold OwnerId updates
      Opportunity[] opportunityUpdates = new Opportunity[0]; //Opportunity sObject to hold OwnerId updates
      Task[] taskUpdates = new Task[0]; //Task sObject to hold updates
      Event[] eventUpdates = new Event[0]; //Event sObject to hold updates
     
      for (Account a : Trigger.new) { //for all records
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
            newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
            accountIds.add(a.Id); //add the Account Id to the set
         }
      }
     
      if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
         for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts), (SELECT Id, OwnerId FROM Tasks),(SELECT Id, OwnerId FROM Events),(SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts Tasks , Events and Opportunities for updated Accounts
            String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
            String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
            for (Contact c : act.Contacts) { //for all contacts
               if (c.OwnerId == oldOwnerId) { //if the contact is assigned to the old account Owner
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId); //create a new Contact sObject
                  contactUpdates.add(updatedContact); //add the contact to our List of updates
               }
            }
            for (Task t : act.Tasks) { //for all tasks
                if(t.OwnerId == oldOwnerId){ //if the task is assigned to the old account owner
                    Task updatedTask = new Task(Id = t.Id, OwnerId = newOwnerId); //create a new Task sObject
                    taskUpdates.add(updatedTask); //add the task to our List of updates
                }
            }
            for (Event e : act.Events){//for all Events
                if(e.OwnerId ==oldOwnerId){ //if the event is assigned to the old account Owner
                    Event updatedEvent = new Event(Id= e.Id, OwnerId = newOwnerId); //create a new Event sObject
                    eventUpdates.add(updatedEvent); //add the event to our List of Updates
                }
            }
            for (Opportunity o : act.Opportunities) { //for all opportunities
               if (o.OwnerId == oldOwnerId) { //if the opportunity is assigned to the old account Owner
                  Opportunity updatedOpportunity = new Opportunity(Id = o.Id, OwnerId = newOwnerId); //create a new Opportunity sObject
                  opportunityUpdates.add(updatedOpportunity); //add the opportunity to our List of updates
               }
            }
         }
         update contactUpdates; //update the Contacts
         update taskUpdates; //update the Tasks
         update eventUpdates; //update the Events
         update opportunityUpdates; //update the Opportunities
      }
   
}

 

Test Method (25%):

@isTest
 public class reassignRelatedContactsAndOppTest{
   static testMethod void test(){
   User thisUser = [ Select Id from User where Id = :UserInfo.getUserId() ]; 
   
   Account acc = new Account(name='abc test',BillingState = 'UK',Phone = '96589658',Website = 'www.abc.com');
   insert acc;
   
   Contact con = new Contact(lastname = 'abctest',OwnerId = acc.Id, Phone = '89563254');
   insert con;
   //reassignRelatedContactsAndOpp rrco = new reassignRelatedContactsAndOpp();
   }
 }

Any help is highly appricated.

 

Regards,

 

souvik9086souvik9086

@isTest
 public class reassignRelatedContactsAndOppTest{
   static testMethod void test(){
   User thisUser = [ Select Id from User where Id = :UserInfo.getUserId() ]; 
   
   Account acc = new Account(name='abc test',BillingState = 'UK',Phone = '96589658',Website = 'www.abc.com');
   insert acc;
   
   Contact con = new Contact(AccountId=acc.Id,lastname = 'abctest',OwnerId = thisUser.Id, Phone = '89563254');
   insert con;

Date mydate = System.Today();

DateTime mydateTime = System.now();

Opportunity opp = new Opportunity(AccountId=acc.Id,Stage= 'Prospecting',OwnerId = thisUser.Id, CloseDate=mydate);
   insert opp;

Event e = new Event(AccountId=acc.Id,Subject = 'Email',StartDateTimemydateTime , EndDateTime =mydateTime +1);
   insert e;

Task t = new Task(WhatId=acc.Id,Subject = 'Email',Status = 'Not started', Priority ='Normal');
   insert t;

update acc;
   }
 }

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Vishnu7700Vishnu7700

hi Tried with above code  but still showing 25 % code coverage.

Thanks,

souvik9086souvik9086

Can you mark which portions are coming red?

 

Thanks

Vishnu7700Vishnu7700

trigger reassignRelatedContactsnActivitesnOpportunities on Account (after update , after insert) {
 
      Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
      Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
      Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
      Contact[] contactUpdates = new Contact[0]; //Contact sObject to hold OwnerId updates
      Opportunity[] opportunityUpdates = new Opportunity[0]; //Opportunity sObject to hold OwnerId updates
      Task[] taskUpdates = new Task[0]; //Task sObject to hold updates
      Event[] eventUpdates = new Event[0]; //Event sObject to hold updates
     
      for (Account a : Trigger.new) { //for all records
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
            newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
            accountIds.add(a.Id); //add the Account Id to the set
         }
      }
     
      if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
         for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts), (SELECT Id, OwnerId FROM Tasks),(SELECT Id, OwnerId FROM Events),(SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts Tasks , Events and Opportunities for updated Accounts
            String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
            String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
            for (Contact c : act.Contacts) { //for all contacts
               if (c.OwnerId == oldOwnerId) { //if the contact is assigned to the old account Owner
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId); //create a new Contact sObject
                  contactUpdates.add(updatedContact); //add the contact to our List of updates
               }
            }
            for (Task t : act.Tasks) { //for all tasks
                if(t.OwnerId == oldOwnerId){ //if the task is assigned to the old account owner
                    Task updatedTask = new Task(Id = t.Id, OwnerId = newOwnerId); //create a new Task sObject
                    taskUpdates.add(updatedTask); //add the task to our List of updates
                }
            }
            for (Event e : act.Events){//for all Events
                if(e.OwnerId ==oldOwnerId){ //if the event is assigned to the old account Owner
                    Event updatedEvent = new Event(Id= e.Id, OwnerId = newOwnerId); //create a new Event sObject
                    eventUpdates.add(updatedEvent); //add the event to our List of Updates
                }
            }
            for (Opportunity o : act.Opportunities) { //for all opportunities
               if (o.OwnerId == oldOwnerId) { //if the opportunity is assigned to the old account Owner
                  Opportunity updatedOpportunity = new Opportunity(Id = o.Id, OwnerId = newOwnerId); //create a new Opportunity sObject
                  opportunityUpdates.add(updatedOpportunity); //add the opportunity to our List of updates
               }
            }
         }
         update contactUpdates; //update the Contacts
         update taskUpdates; //update the Tasks
         update eventUpdates; //update the Events
         update opportunityUpdates; //update the Opportunities
      }
   
}

souvik9086souvik9086

Try like this

 

@isTest
public class reassignRelatedContactsAndOppTest{
static testMethod void test(){
User thisUser = [ Select Id from User where Id = :UserInfo.getUserId() ];
User nextUser = [ Select Id from User where ID != thisUser.Id LIMIT 1 ];
Account acc = new Account(name='abc test',BillingState = 'UK',Phone = '96589658',Website = 'www.abc.com',OwnerId = thisUser.Id);
insert acc;

Contact con = new Contact(AccountId=acc.Id,lastname = 'abctest',OwnerId = thisUser.Id, Phone = '89563254');
insert con;
Date mydate = System.Today();
DateTime mydateTime = System.now();
Opportunity opp = new Opportunity(AccountId=acc.Id,Stage= 'Prospecting',OwnerId = thisUser.Id, CloseDate=mydate);
insert opp;
Event e = new Event(WhatId=acc.Id,Subject = 'Email',StartDateTime= mydateTime , EndDateTime =mydateTime +1);
insert e;
Task t = new Task(WhatId=acc.Id,Subject = 'Email',Status = 'Not started', Priority ='Normal');
insert t;
acc.OwnerId = nextUser.Id;
update acc;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Vishnu7700Vishnu7700

Tried as you said but no luck.Still it is 25% coverage

souvik9086souvik9086

Same red? Are there any failures coming?

 

Thanks

Vishnu7700Vishnu7700

Same red.