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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Help on Test Method for Apex Trigger - Urgent

Hi,

 

Can anybody help me on the test method for the Apex trigger below. I am finding difficulties while writing test cases for the conditions used:

 

trigger membership_for_loyalty_programme on Transaction_Breakup__c (before insert,before update)
{

Boolean calculate_points=false;

for(Transaction_Breakup__c tbp:trigger.new)
{
List< Loyalty_Program__c> Loyalty_Program_List;

Membership__c Membership_Selected;

Visit_Details__c vis =[Select Property_Wise_Revenue__c from Visit_Details__c where id=:tbp.Transaction_Detail_No__c];

Property_Wise_Revenue__c pwr =[Select id,No_of_Room_Nights__c, Loyalty_Programme_Record_Type__c, No_of_Stays__c,Property__c,Guest_Profiile_Id__c,Property_Name__c, Total_expenditure_against_property__c from Property_Wise_Revenue__c where id=:vis.Property_Wise_Revenue__c ];

Loyalty_Program_List = [select id, Name, Automatically_Assigned__c, Consider_Tax_for_Overall_Expense__c, Upgrade_Memberships_Automatically__c, RecordType.Name, No_of_Stays__c,No_of_Room_Nights__c, Overall_Expense__c, Duration__c from Loyalty_Program__c where Active__c = true ORDER BY Overall_Expense__c, No_of_Room_Nights__c, No_of_Stays__c ];

try
{
if(Loyalty_Program_List.size() > 0 )
{

Loyalty_Program__c Loyalty_Program_Selected=new Loyalty_Program__c(No_of_Room_Nights__c =0,Name='temp',No_of_Stays__c =0,Overall_Expense__c=0) ;

System.debug(Loyalty_Program_Selected);

for(Loyalty_Program__c Loyalty_Program:Loyalty_Program_List)
{

Date minDate =Date.Today()-(Integer.valueof(Loyalty_Program.Duration__c)*30);

AggregateResult groupedResults;

if(Loyalty_Program.Consider_Tax_for_Overall_Expense__c==true)

groupedResults = [SELECT SUM(Total_Amount_Incl_Tax__c) SumExp,SUM(No_of_Room_Nights__c) SumNights from Visit_Details__c where Property_Wise_Revenue__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

else

groupedResults = [SELECT SUM(Total_Amount_Excl_Tax__c) SumExp,SUM(Room_Nights__c) SumNights from Visit_Details__c where Property_Wise_Revenue__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

Integer number_of_stays = [SELECT COUNT() from Visit_Details__c where Property_Wise_Revenue__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

// if(Loyalty_Program.RecordType.Name=='Stay Based Membership')
{

if((number_of_stays >= Loyalty_Program.No_of_Stays__c)&&(Loyalty_Program_Selected.No_of_Stays__c<Loyalty_Program.No_of_Stays__c))
{

Loyalty_Program_Selected = Loyalty_Program ;

}

}

// if(Loyalty_Program.RecordType.Name=='Room Night based Membership')
{
if(((Decimal)groupedResults.get('SumNights') >= Loyalty_Program.No_of_Room_Nights__c)&&(Loyalty_Program_Selected.No_of_Room_Nights__c<Loyalty_Program.No_of_Room_Nights__c) )
{
Loyalty_Program_Selected = Loyalty_Program ;
}

}
// if(Loyalty_Program.RecordType.Name=='Revenue Based Membership')
{

Decimal Overall_Amount=0.0;

if((Loyalty_Program_Selected.Overall_Expense__c<Loyalty_Program.Overall_Expense__c))
{

if(Loyalty_Program.Consider_Tax_for_Overall_Expense__c==true )

Overall_Amount = (Decimal)groupedResults.get('SumExp')+tbp.Amount_Spent__c+tbp.Tax_Amount__c;

else

Overall_Amount = (Decimal)groupedResults.get('SumExp')+tbp.Amount_Spent__c;

if(Overall_Amount>=Loyalty_Program.Overall_Expense__c )
{

Loyalty_Program_Selected = Loyalty_Program ;

}

}

}
}

List< Membership__c> Membership_List =[select id,Active__c,Test__c,Loyalty_Program_Name__c,Loyalty_Program_Name__r.Id from Membership__c where Guest_Name__c =: pwr.Guest_Profiile_Id__c AND Property_Name__c =: pwr.Property__c];

if(Membership_List.size() > 0)
{
calculate_points=true;

Boolean selected =false;

for(Membership__c Membership:Membership_List)
{
if((Membership.Loyalty_Program_Name__r.Id!=Loyalty_Program_Selected.Id)&&(Loyalty_Program_Selected.Name!='temp'))
{
Membership.Active__c = false;


System.debug('BBBB');
}
else
{
Membership.Active__c = true;


System.debug('AAAAAA');

selected = true;


}

update Membership;

Membership_Selected = Membership;
}

if((!selected)&&(Loyalty_Program_Selected.Name!='temp')&&(Loyalty_Program_Selected.Automatically_Assigned__c == true))
{

Membership__c Membership_New =new Membership__c();

Membership_New.Guest_Name__c = pwr.Guest_Profiile_Id__c;

Membership_New.Property_Name__c=pwr.Property__c;

Membership_New.Loyalty_Program_Name__c =Loyalty_Program_Selected.id;

Membership_New.Active__c = true;

insert Membership_New;

Membership_Selected = Membership_New;

}

if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == false))
{

Guest_Profile__c gp =[select id,Eligible_Loyalty_Program__c,Loyalty_programme_assigned_property__c from Guest_Profile__c where id =:pwr.Guest_Profiile_Id__c];

gp.Eligible_Loyalty_Program__c =Loyalty_Program_Selected.Name;

gp.Loyalty_programme_assigned_property__c =pwr.Property_Name__c;

update gp;
}

}
else
{

if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == true))
{

Membership__c Membership_New =new Membership__c();

Membership_New.Guest_Name__c = pwr.Guest_Profiile_Id__c;

Membership_New.Property_Name__c=pwr.Property__c;

Membership_New.Test__c ='test';

Membership_New.Loyalty_Program_Name__c =Loyalty_Program_Selected.id;

Membership_New.Active__c = true;

insert Membership_New;

calculate_points=true;

Membership_Selected = Membership_New;


}
if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == false))
{

calculate_points=false;

Guest_Profile__c gp =[select id,Eligible_Loyalty_Program__c,Loyalty_programme_assigned_property__c from Guest_Profile__c where id =:pwr.Guest_Profiile_Id__c];

gp.Eligible_Loyalty_Program__c =Loyalty_Program_Selected.Name;

gp.Loyalty_programme_assigned_property__c =pwr.Property_Name__c;

update gp;

}
}



Membership__c Membership_Active =[select id,Active__c,Test__c,Loyalty_Program_Name__c,Loyalty_Program_Name__r.Id from Membership__c where Guest_Name__c =: pwr.Guest_Profiile_Id__c AND Property_Name__c =: pwr.Property__c AND Active__c =: true];




if(calculate_points)
{
Point_Conversion__c pc;
try
{

System.debug(Membership_Selected.Loyalty_Program_Name__c);
pc =[Select id, Active__c,Service_Type__c,Split_Beverage__c,Split_by_Service_Type__c,Split_Room_Charges__c,Amount_in_Consideration__c,Point_Calculation_By__c, Calculate_Discounts__c, Calculate_Points__c, Calculate_Points_for_Tax_Amount__c,
Consider_Discount__c, Discount_Consideration__c, Property_Name__c, Revenue_Code__c, Revenue_Type__c, Revenue_Type_Formula__c, X1_point__c, Amount__c, Points__c, Discount_for_Total_Amount__c, Dicount_Percent__c, Applicable_From__c, Beverage_Amount_for_Points__c, Beverage_Discount__c, Beverage_Points__c, Food_Amount_for_Points__c, Food_Discount__c, Food_Points__c, Liquor_Amount_for_Points__c, Liquor_Discount__c, Liquor_Points__c, Other_Amount_for_Points__c, Other_Discount__c, Other_Points__c, Plan_Amount_for_Points__c, Plan_Discount__c, Plan_Points__c, Room_Amount_for_Points__c, Room_Discount__c, Room_Points__c, Soft_Drink_Amount_for_Points__c, Soft_Drink_Discount__c, Soft_Drink_Points__c, Tax_Amount_for_Points__c, Tax_Discount__c, Tax_Points__c, Tobacco_Amount_for_Points__c, Tobacco_Discount__c, Tobacco_Points__c from Point_Conversion__c where Loyalty_Program_Name__c =:Membership_Active .Loyalty_Program_Name__c AND Active__c=:true];





if(pc.Split_by_Service_Type__c ==true)
{


if(pc.Amount_in_Consideration__c =='Bill Amount inc. Tax')
{


if(pc.Calculate_Points_for_Tax_Amount__c==true)
{

if((pc.Service_Type__c.contains('Food'))&&(pc.Calculate_Points__c==true)&&(pc.Food_Amount_for_Points__c!=0) )
{
tbp.Points_for_Food__c =tbp.Points_for_Food__c+((tbp.Amount_for_Food__c+tbp.Tax_for_Food__c)*(pc.Food_Points__c/pc.Food_Amount_for_Points__c)) ;

}


// ------------------------------Beverage-----------------------------

if( (tbp.Service_Type__c.contains('Beverage'))&&(pc.Calculate_Points__c==true)&&(pc.Beverage_Amount_for_Points__c!=0) )
{
tbp.Points_for_Beverage__c =tbp.Points_for_Beverage__c+((tbp.Amount_for_Beverage__c+tbp.Tax_for_Beverage__c)*(pc.Beverage_Points__c/pc.Beverage_Amount_for_Points__c)) ;

}


// ------------------------------Liquor-----------------------------

if( (tbp.Service_Type__c.contains('Liquor'))&&(pc.Calculate_Points__c==true)&&(pc.Liquor_Amount_for_Points__c!=0) )
{
tbp.Points_for_Liquor__c =tbp.Points_for_Liquor__c+((tbp.Amount_for_Liquor__c+tbp.Tax_for_Liquor__c)*(pc.Liquor_Points__c/pc.Liquor_Amount_for_Points__c)) ;

}


// ------------------------------Soft_Drink-----------------------------

if( (tbp.Service_Type__c.contains('Soft Drinks'))&&(pc.Calculate_Points__c==true)&&(pc.Soft_Drink_Amount_for_Points__c!=0) )
{
tbp.Points_for_Soft_Drink__c =tbp.Points_for_Soft_Drink__c+((tbp.Amount_for_Soft_Drink__c+tbp.Tax_for_Soft_Drink__c)*(pc.Soft_Drink_Points__c/pc.Soft_Drink_Amount_for_Points__c)) ;

}


// ------------------------------Tobacco-----------------------------

if( (tbp.Service_Type__c.contains('Tobacco'))&&(pc.Calculate_Points__c==true)&&(pc.Tobacco_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tobacco__c =tbp.Points_for_Tobacco__c+((tbp.Amount_for_Tobacco__c+tbp.Tax_for_Tobacco__c)*(pc.Tobacco_Points__c/pc.Tobacco_Amount_for_Points__c)) ;

}

// ------------------------------Others-----------------------------
if( (tbp.Service_Type__c.contains('Others'))&&(pc.Calculate_Points__c==true)&&(pc.Other_Amount_for_Points__c!=0) )
{
tbp.Points_for_Others__c =tbp.Points_for_Others__c+((tbp.Amount_for_Others__c+tbp.Tax_for_Others__c)*(pc.Other_Points__c/pc.Other_Amount_for_Points__c)) ;
}

// ------------------------------Room-----------------------------
if( (tbp.Service_Type__c.contains('Room'))&&(pc.Calculate_Points__c==true)&&(pc.Room_Amount_for_Points__c!=0) )
{
tbp.Points_for_Room__c =tbp.Points_for_Room__c+((tbp.Amount_for_Room__c )*(pc.Room_Points__c/pc.Room_Amount_for_Points__c)) ;
}

// ------------------------------Plan-----------------------------
if( (tbp.Service_Type__c.contains('Plan'))&&(pc.Calculate_Points__c==true)&&(pc.Plan_Amount_for_Points__c!=0) )
{
tbp.Points_for_Plan__c =tbp.Points_for_Others__c+((tbp.Amount_for_Plan__c )*(pc.Plan_Points__c/pc.Plan_Amount_for_Points__c)) ;
}


}

if(pc.Calculate_Points_for_Tax_Amount__c==false)
{


if((pc.Service_Type__c.contains('Food'))&&(pc.Calculate_Points__c==true)&&(pc.Food_Amount_for_Points__c!=0) )
{
tbp.Points_for_Food__c =tbp.Points_for_Food__c+((tbp.Amount_for_Food__c )*(pc.Food_Points__c/pc.Food_Amount_for_Points__c)) ;

}

// ------------------------------Beverage-----------------------------

if( (tbp.Service_Type__c.contains('Beverage'))&&(pc.Calculate_Points__c==true)&&(pc.Beverage_Amount_for_Points__c!=0) )
{
tbp.Points_for_Beverage__c =tbp.Points_for_Beverage__c+((tbp.Amount_for_Beverage__c )*(pc.Beverage_Points__c/pc.Beverage_Amount_for_Points__c)) ;

}

// ------------------------------Liquor-----------------------------

if( (tbp.Service_Type__c.contains('Liquor'))&&(pc.Calculate_Points__c==true)&&(pc.Liquor_Amount_for_Points__c!=0) )
{
tbp.Points_for_Liquor__c =tbp.Points_for_Liquor__c+((tbp.Amount_for_Liquor__c )*(pc.Liquor_Points__c/pc.Liquor_Amount_for_Points__c)) ;

}


// ------------------------------Soft_Drink-----------------------------

if( (tbp.Service_Type__c.contains('Soft Drinks'))&&(pc.Calculate_Points__c==true)&&(pc.Soft_Drink_Amount_for_Points__c!=0) )
{
tbp.Points_for_Soft_Drink__c =tbp.Points_for_Soft_Drink__c+((tbp.Amount_for_Soft_Drink__c )*(pc.Soft_Drink_Points__c/pc.Soft_Drink_Amount_for_Points__c)) ;

}


// ------------------------------Tobacco-----------------------------

if( (tbp.Service_Type__c.contains('Tobacco'))&&(pc.Calculate_Points__c==true)&&(pc.Tobacco_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tobacco__c =tbp.Points_for_Tobacco__c+((tbp.Amount_for_Tobacco__c )*(pc.Tobacco_Points__c/pc.Tobacco_Amount_for_Points__c)) ;

}

// ------------------------------Room-----------------------------
if( (tbp.Service_Type__c.contains('Room'))&&(pc.Calculate_Points__c==true)&&(pc.Room_Amount_for_Points__c!=0) )
{
tbp.Points_for_Room__c =tbp.Points_for_Room__c+((tbp.Amount_for_Room__c )*(pc.Room_Points__c/pc.Room_Amount_for_Points__c)) ;
}

// ------------------------------Plan-----------------------------
if( (tbp.Service_Type__c.contains('Plan'))&&(pc.Calculate_Points__c==true)&&(pc.Plan_Amount_for_Points__c!=0) )
{
tbp.Points_for_Plan__c =tbp.Points_for_Others__c+((tbp.Amount_for_Plan__c )*(pc.Plan_Points__c/pc.Plan_Amount_for_Points__c)) ;
}


// ------------------------------Tax-----------------------------
if( (pc.Calculate_Points__c==true)&&(pc.Tax_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tax__c =tbp.Points_for_Tax__c+((tbp.Amount_for_Tax__c )*(pc.Tax_Points__c/pc.Tax_Amount_for_Points__c)) ;
}

}


}

}
else
{
if((pc.Amount_in_Consideration__c=='Bill Amount inc. Tax')&&(pc.Calculate_Points__c==true)&&(pc.Amount__c!=0) )
{

tbp.Points_Earned__c =tbp.Points_Earned__c+((tbp.Amount_Spent__c+tbp.Tax_Amount__c)*(pc.Points__c/pc.Amount__c)) ;

}

if((pc.Amount_in_Consideration__c=='Bill Amount exl. Tax')&&(pc.Calculate_Points__c==true)&&(pc.Amount__c!=0) )
{
tbp.Points_Earned__c =tbp.Points_Earned__c+((tbp.Amount_Spent__c)*(pc.Points__c/pc.Amount__c)) ;

}
}

}
catch(Exception e)
{
//tbp.addError(String.valueOf( e.getMessage() ));
}

}

}
}
catch(Exception e)
{
//tbp.addError(e.getMessage()+'123');

}
}
}

Navatar_DbSupNavatar_DbSup

Hi,

 

You have to create the Transaction_Breakup__c  data in such a way that it consist that information on which you have made the condition inside the if. Create a record for object Visit_Details__c and pass the id of Transaction_Breakup__c    which you have inserted. You have to create the record for objects Property_Wise_Revenue__c and Loyalty_Program_List with proper data and at the end update Transaction_Breakup__c. Make sure that while creating a test record you are putting the correct value that you are going to used inside the if condition.

Try the below code as reference:

@isTest

private class testTriggerinsert_Contact_Activity

{

      public static testMethod void unitTestinsert_Contact_Activity()

        {

 Transaction_Breakup__c t=new Transaction_Breakup__c(name='test', other mendatory fields);

 insert t;

 Visit_Details__c v=new Visit_Details__c(name='test',other mendatory fields);

 insert v;

 Loyalty_Program_List l=new Loyalty_Program_List(name='test',other mendatory field);

 insert l;

 update t;

  

 }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I tried using the above method, but we have a small issue. Here we are not giving any conditions for the transaction breakup object, instead the details in transaction breakup is comparing the field values with the loyalty program values in the if condition. I got a coverage of 19% but not able to proceed further.

 

Can you please help me more on the same.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Jain,

 

Can you please make me understand how to write a test method for the below line of code:

 

if(Point_Conversion.Revenue_Code__c!=null)
Revenue_Master_Id_String   = 'AND Revenue_Code__c=\''+ Point_Conversion.Revenue_Code__c+'\'  ';

 

if(Point_Conversion.X1_point__c==null)
Point_Conversion.X1_point__c=0;