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
Mr. WMr. W 

Testing Triggers

I'm brand new to Apex and need to test triggers to deploy to production.  I have two triggers that SFDCFOX helped me write and they are working just as required in Sandbox, but now need to get them into production. 

 

I'm researching similar questions to hopefully do it myself but appreciate your help!!!

     

First Trigger:

 

trigger AccTrigger on Account (after update) {
    update [select id from contact where accountid in :trigger.new and duplicate_account_address__c = true];
}

 

 

 

Second Trigger:

 

trigger ConTrigger on Contact( before insert, before update ) {
   map< id, account > accs = new map< id, account >();
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
         accs.put( c.accountid, null );
      }
   }
   accs.putAll( [select id, billingstreet, billingcity, billingstate, billingpostalcode, billingcountry from account where id in :accs.keyset()]);
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
          c.mailingstreet = accs.get( c.accountid ).billingstreet;
          c.mailingcity = accs.get( c.accountid ).billingcity;
          c.mailingstate = accs.get( c.accountid ).billingstate;
          c.mailingpostalcode = accs.get( c.accountid ).billingpostalcode;
          c.mailingcountry = accs.get( c.accountid ).billingcountry;
      }
   }
}

Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu
try this,
@isTest(SeeAllData=true)
private class MyTestClass1 {
@isTest static void testtrigger() {
Id aid = [Select Id from Account limit 1].id;
Contact con = new Contact();
con.Lastname = 'test';
con.AccountId = aid;
con.duplicate_account_address__c = true;
insert con;

Account acc = [Select Name from Account where id =:aid];
acc.name = 'testupdate';
update acc;
}
}

All Answers

Jia HuJia Hu
try this,
@isTest(SeeAllData=true)
private class MyTestClass1 {
@isTest static void testtrigger() {
Id aid = [Select Id from Account limit 1].id;
Contact con = new Contact();
con.Lastname = 'test';
con.AccountId = aid;
con.duplicate_account_address__c = true;
insert con;

Account acc = [Select Name from Account where id =:aid];
acc.name = 'testupdate';
update acc;
}
}
This was selected as the best answer
Mr. WMr. W

The Class works great.  I ran the test and now my triggers are 100% covered.  

 

However when i deploy the change set that includes them to Production it fails saying:

 

"Test Coverage of selected Apex Tirgger is 0% at least 1% test coverage is required"  

 

 

Could you offer some basic insight on how I test and deploy now?

Jia HuJia Hu
Have you added the Test class in your Change Set?