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
sfdc@isha.ax1814sfdc@isha.ax1814 

Test class urgent help...

Hi 

help me out  for writing the test class for below trigger.

 

trigger createAllocationsForLead on Project__c (after insert) {

  for(Project__c newPro:Trigger.new)
  {
      
          Allocation__c all=new Allocation__c();
          all.Assigned_User__c=newPro.Project_Lead__c;
          all.Project__c=newPro.id;
          all.Start_Date__c=newPro.Actual_Start_Date__c;
          all.End_Date__c =newPro.Actual_End_Date__c;
          all.Location__c=newPro.Project_Type__c;   
          all.Role__c='Project Lead';
          insert all; 
          
      
  }
}
Best Answer chosen by Admin (Salesforce Developers) 
Subhash GarhwalSubhash Garhwal

Hi Isha,

 

you just need to create a Project__c  record with appropriate values in fields in Test class.

 

@isTest
private class Test_createAllocationsForLead {
 
  static testMethod void testSave() {

 

Project__c project = new Project__c(); 

 

Test.startTest();

 

insert project;

 

List<Allocation__c> all = [Select Id From Allocation__c];

System.assertEquals(1, all.size());

 

Test.stopTest();

}

}

 

Thanks

 

Hit the Kudos button (star) if it post helps you

 

 

 

All Answers

Subhash GarhwalSubhash Garhwal

Hi Isha,

 

you just need to create a Project__c  record with appropriate values in fields in Test class.

 

@isTest
private class Test_createAllocationsForLead {
 
  static testMethod void testSave() {

 

Project__c project = new Project__c(); 

 

Test.startTest();

 

insert project;

 

List<Allocation__c> all = [Select Id From Allocation__c];

System.assertEquals(1, all.size());

 

Test.stopTest();

}

}

 

Thanks

 

Hit the Kudos button (star) if it post helps you

 

 

 

This was selected as the best answer
Satyendra RawatSatyendra Rawat

Hi Isha,

First Modified the trigger,for best practice.

trigger createAllocationsForLead on Project__c (after insert) {
List<Allocation__c> lstApplication = new List<Allocation__c>();
  for(Project__c newPro:Trigger.new)
  {
      
          Allocation__c all=new Allocation__c();
          all.Assigned_User__c=newPro.Project_Lead__c;
          all.Project__c=newPro.id;
          all.Start_Date__c=newPro.Actual_Start_Date__c;
          all.End_Date__c =newPro.Actual_End_Date__c;
          all.Location__c=newPro.Project_Type__c;   
          all.Role__c='Project Lead';

          lstApplication .add(all);
  }

insert lstApplication;
}

 

 

and @@@@@@@@@@@@Test method @@@@@@@@

 

@isTest
private class TestcreateAllocationsForLead {
 
  static testMethod void myUnitTest() {

 

 

 

Test.startTest();

Lead newLead = new Lead();
newLead.Name = 'testLead';
newLead.Email= 'test@gmail.com';
newLead.Company = 'test company';
insert newLead;// if some more field is mandatory , set value;

 

Project__c project = new Project__c(); 

project.Actual_Start_Date__c = Date.today();

project.Actual_End_Date__c = Date.today();

project.Project_Lead__c = newLead.Id;

insert project; //// before insert if some more field is mandatory , set value;

 

List<Allocation__c> all = [Select Id Project__c From Allocation__c where Project__c=:project.Id];

System.assertEquals(1, all.size());

 

Test.stopTest();

}

}

Thanks 

Satya

Response me  if it post helps you