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
YumiYumi 

Need Help with Apex Class

I am brand new to Apex and wrote my first trigger. a very simple one - and it works perfectly but a I'm not sure where to even start with writing an apex test class..

 

My trigger is setup to update a lookup field based on the value of another lookup field. (basically so the user can select the "account" from a record, hit save, and have the "parent account" automatically populate into the other lookup field. Rather than having to select a value for both lookup fields.)

 

Here is the trigger:

 

trigger UpdateManagementCompanyField on Transfer__c (before insert, before update) {for(Transfer__c a:Trigger.new){a.Management_Company__c = a.Management_Company_ID__c;}

}

 

Any help would be greatly appreciated!

 

 

hitzhitz

Hi, YUMI

 

Try this.....!!

 

@isTest
private class TestTransferTrigger {
    
    static testMethod void testcase1() {
    
        Transfer__c objtra = new Transfer__c();
        objtra.name = 'transfer1';
        objtra.Management_Company_ID__c = 'Mgt-00001';
        insert  objtra;
    
    }
}

 

Hope This Helps for you....!!!

 

YumiYumi

Wow. Thank you Hitesh!!

 

:smileyhappy:

me_helpme_help

Hi Hitz,

I just started with APEX and VF.

i wrote the following trigger and am not able to write test class for that.

Can you please help me out with this??

 

 

TRIGGER

 

trigger increaseCount on Employee__c (after insert) 
{
List<Employee__c> EmployeeToUpdate = new List<Employee__c>{};
for(Employee__c emp : Trigger.new)
  {
  Company__c company =[ select Number_of_Employees__c from Company__c where Id= :emp.Company__c ];
    if (company.Number_of_Employees__c==Null)
   {
   company.Number_of_Employees__c=1;
   }
   else
   {company.Number_of_Employees__c =company.Number_of_Employees__c+1;}
   update company;
  }
}

hitzhitz

Hi,

 

just try to insert new record in your test class which satisfies your trigger code condition.

just follow below link

http://wiki.developerforce.com/index.php/How_to_Write_Good_Unit_Tests

 

 

@isTest
private class TestincreaseCountTrigger {

    static testMethod void testcase1() {

             // ToDo

             ---- your logic --- [insert new Employee]

    }


}