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
Salesforce_user007Salesforce_user007 

Test class for apex triger.

 i hav written a trigger ,can anyone help to write the test class:

 

trigger Industry on Account (after update) {
            
    List<Account> clientList = new List<Account>();
       
       for(Account a: Trigger.New){
        if((Trigger.oldMap.get(a.Id).Indus<> Trigger.newMap.get(a.Id).Indus ||
           (Trigger.oldMap.get(a.Id).S__c <> Trigger.newMap.get(a.Id).S__c) 
                  {
                clientList.add(a);
        }   
    }  
    if(!clientList.isEmpty()){         
             updateIndustry.industrySector(clientList);  
      }
   
}

 

Thanks.

Salesforce_user007Salesforce_user007

Below is my test class ,i am getting 0% test coverage.Please let me know where i went wrong:

 

@isTest
private class TestIndustry
{

static testMethod void myUnitTest()
{

Account c=new Account();
c.Industry='Final';

c.S__c='aaa';
update c;

}

}

KapilCKapilC

Hi,

 

Try this code...

 

@isTest
private class TestIndustry{
  static testMethod void myUnitTest(){
   Account acc = new Account(S__c='aaa',Industry='Final');
   insert acc;
   
   acc.S__c='bbb';
    update acc;
   }
}

 

[If you got answer from my post please mark it as solution.]
Thanks,
Kapil
Salesforce_user007Salesforce_user007

Hi,

Still i am getting 0% code coverage.

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try this

@isTest

Private class Industry1{

static testMethod void AccountExt_Test1()
{
 Account acc = new Account();
 acc.name='test';
 acc.Industry='Banking';
 insert acc;
 acc.name='test1';
  acc.Industry='Biotechnology';
  update acc;

}}

 

 

Regards,

Rajesh.

 

 

Salesforce_user007Salesforce_user007

Hi Rajesh ,

 

Thanks for ur reply ...... but still 0% code coverage....

Rajesh SriramuluRajesh Sriramulu

Hi

 

But for me showing 100% code coverage.Let me know r u running in Eclipse or User Interface.

 

Regards,

Rajesh

Rajesh SriramuluRajesh Sriramulu

Hi

 

Goto->setup->Develop->Apex Classes->select ur test class->Run Test.

 

When u click on Run test search ur trigger and click the percentage beside trigger,then it will show some lines are blue and some red lines.which means red lines that are not covered.

 

Let me know if any query.

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

@SRS8 i got your point but due to some issue i am not able to view the Debug log for test class.

Rajesh SriramuluRajesh Sriramulu

Hi

 

No need debug log u just follow what i have post previously.See ur tiggger with 100%.

 

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

I am getting 0% test coverage.

Rajesh SriramuluRajesh Sriramulu

Hi

 

I surprised same code for me showing 100% but to u it show 0%.

 

Did u follow this Goto->setup->Develop->Apex Classes->select ur test class->Run Test.and search ur trigger,can u send the screen shot after u click Run Test of ur test class.

 

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

I am getting "test failure" Error message :line 4, column 9: Method does not exist or incorrect signature: [GrantAccess].grantAccess(LIST<Project__c>).

 

But this particular field is not related to the object that i am reffering for the trigger test class.I am not sure y i am getting this error??

 

Thanks.

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

Could u post ur updated trigger and test class.

 

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

@Rajesh

 

I have sent update trigger and test class to you .Please check ur Private Message.

 

 

Thanks.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this , if officer is picklist datatype then put the picklist two different value in it.

 

@isTest
private class TestupdateClientIndustrySector 
{
static testMethod void myupdateClientIndustrySector() 
{
Account acc=new Account(); 

acc.Industry='Financial';
acc.Sub_Industry__c ='Finance';
acc.Sector__c ='Companies';
acc.Short_Description__c ='Hello';
acc.Sub_Sector__c='';
acc.Name = 'Venture';

acc.Officer__='test';
insert acc; 

acc.Industry='Industrials';
acc.Sub_Industry__c ='Industrials';
acc.Sector__c ='Electronics';
acc.Officer__='test11';
update acc;

}
}

 

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

Hi,

Still i am getting 0% test coverage.

 

 

Thanks.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Did u get any errors.

 

Let me know.

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

Same error as i have mentioned before:

 

"test failure" Error message :line 4, column 9: Method does not exist or incorrect signature: [GrantAccess].grantAccess(LIST<Project__c>).

Rajesh SriramuluRajesh Sriramulu

Hi

 

U added some classes w.r.t  methods in this plese check that methods are exist are or not.And I change the trigger in blue color which is correct or not let u check that. Then Run ur test class.

 

trigger updateIndustry on Account (after update) {
            
    List<Account> clientList = new List<Account>()

;
    list<Account> updatedOfficrsList = new List<Account>(); 
   
    // to update the related Deals on update of the Industry/Sector fields on Account

    for(Account cln: Trigger.New){
        if((Trigger.oldMap.get(cln.Id).Industry <> Trigger.newMap.get(cln.Id).Industry) ||
           (Trigger.oldMap.get(cln.Id).Sub_Industry__c <> Trigger.newMap.get(cln.Id).Sub_Industry__c) ||
           (Trigger.oldMap.get(cln.Id).Sector__c <> Trigger.newMap.get(cln.Id).Sector__c) ||
           (Trigger.oldMap.get(cln.Id).Sub_Sector__c <> Trigger.newMap.get(cln.Id).Sub_Sector__c)) {
                clientList.add(cln);
        }   
    }  
    if(!clientList.isEmpty()){         
       //to update the related Deals fields
       updateIndustry.industrySectorUpdate(clientList);  
       
         }
   
     // on change of Client's  Officer
   
    for(Account client: Trigger.New){
       if(Trigger.oldMap.get(client.Id).Officer__c <> Trigger.newMap.get(client.Id).Officer__c || 
       Trigger.oldMap.get(client.Id).Officer_2__c <> Trigger.newMap.get(client.Id).Officer_2__c ){
           updatedOfficrsList.add(client);
           
       }  
       if(updatedOfficrsList.size() > 0){
                     DealTeamInsertion.updateOfficerChange(updatedOfficrsList);
       }  
   }
   
}

 

 

Regards,

Rajesh.

Salesforce_user007Salesforce_user007

still 0% coverage..

Rajesh SriramuluRajesh Sriramulu

Hi

 

I tried with ur triggger that u sent getting the 100% code coverage but u didn't get I don't know why and try to see the dependencies of the trigger by clicking on Show Dependencies and try to solve that error.U will get. :)

 

Regards,

Rajesh.