You need to sign in to do that
Don't have an account?
Gauri Gaikwad 13
need to check trigger code is right
I check my code coverage of trigger is 100%, apex class is 100%, but need to check my trigger fucntion is correct or not.Plz help.
--------------
trigger:
trigger PenDiscountRangeTrigger on Parker_pen__C(before insert)
{
PenDiscountRangeClass.ApplyDiscount(trigger.new);
}
------
apex class:
public class PenDiscountRangeClass{
public static void ApplyDiscount(list<Parker_pen__C> VarPenList)
{
for(Parker_pen__C VarP: VarPenList)
{
if (VarP.Price__c>=100)
{
VarP.Price__c = VarP.Price__c- 30;
}
else if (VarP.Price__c<100 && VarP.Price__c>= 70)
{
VarP.Price__c= VarP.Price__c- 20;
}
else if (VarP.Price__c<70 && VarP.Price__c>= 40)
{
VarP.Price__c= VarP.Price__c- 10;
}
else if (VarP.Price__c<40)
{
VarP.Price__c= VarP.Price__c- 5;
}
}
}
}
----
Test class:
@isTest
class PenDiscountRangeClassTest {
@isTest
static void ApplyDiscountA()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=101;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(71, VarP2.Price__C);
}
@isTest
static void ApplyDiscountB()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=71;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(51, VarP2.Price__C);
}
@isTest
static void ApplyDiscountC()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=40;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(30, VarP2.Price__C);
}
@isTest
static void ApplyDiscountD()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=30;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(25, VarP2.Price__C);
}
}
--------------
trigger:
trigger PenDiscountRangeTrigger on Parker_pen__C(before insert)
{
PenDiscountRangeClass.ApplyDiscount(trigger.new);
}
------
apex class:
public class PenDiscountRangeClass{
public static void ApplyDiscount(list<Parker_pen__C> VarPenList)
{
for(Parker_pen__C VarP: VarPenList)
{
if (VarP.Price__c>=100)
{
VarP.Price__c = VarP.Price__c- 30;
}
else if (VarP.Price__c<100 && VarP.Price__c>= 70)
{
VarP.Price__c= VarP.Price__c- 20;
}
else if (VarP.Price__c<70 && VarP.Price__c>= 40)
{
VarP.Price__c= VarP.Price__c- 10;
}
else if (VarP.Price__c<40)
{
VarP.Price__c= VarP.Price__c- 5;
}
}
}
}
----
Test class:
@isTest
class PenDiscountRangeClassTest {
@isTest
static void ApplyDiscountA()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=101;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(71, VarP2.Price__C);
}
@isTest
static void ApplyDiscountB()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=71;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(51, VarP2.Price__C);
}
@isTest
static void ApplyDiscountC()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=40;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(30, VarP2.Price__C);
}
@isTest
static void ApplyDiscountD()
{
//create record
Parker_pen__C VarP= New Parker_pen__C();
VarP.Name='Govind';
VarP.Price__c=30;
//insert record
insert VarP;
//trigger called
//add new box with new value
Parker_pen__C VarP2= New Parker_pen__C();
VarP2= [select price__c from Parker_Pen__C where id=: VarP.id];
//check new value is expected value
system.assertEquals(25, VarP2.Price__C);
}
}
As per the code the functionality you are looking is below.
When ever Parker_pen__C record is inserted if Price__c>=100 then Price__c is modified as Price__c-30 and so on..
If you are looking for the same then the code which you wrote is correct.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,