function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dcgb2332dcgb2332 

Need test class for trigger on Opportunity Owner

Here is my code, if someone could please help me with a test class, I would greatly appreciate it!

`trigger setOpportunityOwner on Task (before insert, before update) {
  Set<Id> oppIds = new Set<Id>();
  for ( Task newTask : Trigger.new ) {
    if ( newTask.WhatId != null && String.valueOf( newTask.WhatId ).startsWith( '006' ) ) {
      oppIds.add( newTask.WhatId );
    }
  }
  Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Owner.Name FROM Opportunity WHERE Id IN :oppIds]);
  for(Task newTask: Trigger.new) {
    if ( oppMap.containsKey( newTask.WhatId ) ) {
      newTask.Opp_Owner__c = oppMap.get( newTask.WhatId ).Owner.Name;
    }
  }
}`
Best Answer chosen by dcgb2332
Ajay K DubediAjay K Dubedi
Hi,

Trigger Test Class -  This test class has 100% code coverage.
 
@isTest
public class setOpportunityOwner 
{
    public TestMethod static void setOpportunityOwner_Method()
    {
        Opportunity opp = New Opportunity();
        opp.name = 'Hello';
        opp.stageName = 'Prospecting';
        opp.CloseDate = Date.today();
        insert opp;
        Task tk = New Task();
        tk.WhatId = opp.Id;
        tk.Subject = 'Other';
        tk.status = 'Not Started';
        tk.description = 'New  Work';
        Test.startTest();
        Database.SaveResult str = database.insert(tk , False);
        System.assertEquals(True, str.isSuccess());
        Test.stopTest();
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 

All Answers

Ajay K DubediAjay K Dubedi
Hi,

Trigger Test Class -  This test class has 100% code coverage.
 
@isTest
public class setOpportunityOwner 
{
    public TestMethod static void setOpportunityOwner_Method()
    {
        Opportunity opp = New Opportunity();
        opp.name = 'Hello';
        opp.stageName = 'Prospecting';
        opp.CloseDate = Date.today();
        insert opp;
        Task tk = New Task();
        tk.WhatId = opp.Id;
        tk.Subject = 'Other';
        tk.status = 'Not Started';
        tk.description = 'New  Work';
        Test.startTest();
        Database.SaveResult str = database.insert(tk , False);
        System.assertEquals(True, str.isSuccess());
        Test.stopTest();
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 
This was selected as the best answer
dcgb2332dcgb2332
Thank you! So I input it as an Apex Class and both the trigger and class have %100 coverage now, however, when I go to deploy via change set, it says no coverage. I checked under Apex Class the org coverage and it’s saying 5% I don’t understand why? Code Coverage: 5% Your overall code coverage is currently 5%. To deploy code to production, you must have at least 75%.
lnallurilnalluri
Hi,

Make sure you have selected Run Specified Tests during deployment.

​​​​​​​Run Specified Tests: Only specified test classes will run with this option. You to provide specified tests in comma-separated list.
dcgb2332dcgb2332
Thanks! Do you need to include the apxc extension after the class name?
lnallurilnalluri
Not needed. Just the class name.