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
NarcissuNarcissu 

Can I deploy my trigger from Sandbox to Production site without any tests?

Hi,

 

I just created a new trigger and am trying to push it to the production site. As I know I cannot do it unless I have 75% test coverage. However, I have tested it in Sandbox and it worked the way I need. I think it will work without code test.

 

So is it possible that I can deploy my trigger from Sandbox to Production site without any tests?

 

Any responds are much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Howler MonkeyHowler Monkey

If your goal is to get some level of test coverage, you basically need a test class that inserts an account with required fields, then insert a case with required fields referencing that account.  Looks something like this:

 

@isTest

private class TestTriggerSupport {

 

   

   static testMethod void myUnitTest()

    {

     Account a1 = new Account(Name = 'Test Method');

     insert a1;

     Case c1 = new Case(Origin = 'Web', Subject ='Test Method', Status = 'New', AccountId = a1.id);

     insert c1;

    }

}

 

Note that this gets you test coverage, but doesn't test bulk inserts.

 

Also, I think this line of code in the loop of the trigger.new cases will cause your code to violate apex governor limits once you go over 100 records, as it is a query within the loop and will run for every case in a bulk trigger scenario:



   for(Account at:[Select id,Last_Update__c From Acco​unt where id =: c.accountid])

 

You'll need to build a list or set of Accounts outside the loop that is used in lieu of this query.



All Answers

CaitlinMCaitlinM

No, even though you know your code will work, you still need to write a test for it or the deployment will fail. 

NarcissuNarcissu

Thanks. Is there any test method samples I can use for the following code?

 

trigger Caseupdate on Case(after insert)
{
Case b = new Case();
Set<id> AccountIDs = new set<id>();
list<Account> AcctList = new list<Account>();
for(case c : Trigger.new)
{
for(Account at:[Select id,Last_Update__c From Account where id =: c.accountid])
{
at.Last_Update__c = date.valueof(c.createddate);
AcctList.add(at);
}
}
update AcctList;
}

NarcissuNarcissu

By the way, I usually upload the package from third party so I also have classes and triggers in my production. However, I ran all tests and I got 46% coverage which is far from 75%. So I am wondering if the package from thirdf party could be wrong?

Howler MonkeyHowler Monkey

If your goal is to get some level of test coverage, you basically need a test class that inserts an account with required fields, then insert a case with required fields referencing that account.  Looks something like this:

 

@isTest

private class TestTriggerSupport {

 

   

   static testMethod void myUnitTest()

    {

     Account a1 = new Account(Name = 'Test Method');

     insert a1;

     Case c1 = new Case(Origin = 'Web', Subject ='Test Method', Status = 'New', AccountId = a1.id);

     insert c1;

    }

}

 

Note that this gets you test coverage, but doesn't test bulk inserts.

 

Also, I think this line of code in the loop of the trigger.new cases will cause your code to violate apex governor limits once you go over 100 records, as it is a query within the loop and will run for every case in a bulk trigger scenario:



   for(Account at:[Select id,Last_Update__c From Acco​unt where id =: c.accountid])

 

You'll need to build a list or set of Accounts outside the loop that is used in lieu of this query.



This was selected as the best answer