You need to sign in to do that
Don't have an account?
Ian Lin 439
Is below code bad code ?how to make it a good code.
trigger TaxCalaculation on OrderItem (before insert,before update) {
product2 a;
//List<orderItem> a=[select product2.family from orderItem where product2id IN :Trigger.newMap.keySet()];
//List<OrderItem> li=new List<OrderItem>();
for(OrderItem o:Trigger.new)
{
a=[select family from product2 where id= :o.Product2Id];
//o=[select Product2.family from orderItem where product2id= :Trigger.new.product2id];
//o=[select product2.family from orderItem where product2Id IN :Trigger.new];
if(a.family=='Electronics')
{
o.taxRate__c=0.02;
}
else if(a.family=='FootWear')
{
o.taxRate__c=0.05;
}
else if(a.family=='Clothing')
{
o.taxRate__c=0.07;
}
o.finalRate__c=o.UnitPrice*o.Quantity*o.taxRate__c;
system.debug(o.taxRate__c);
system.debug(a.Family);
System.debug(o.finalRate__c);
}
}
product2 a;
//List<orderItem> a=[select product2.family from orderItem where product2id IN :Trigger.newMap.keySet()];
//List<OrderItem> li=new List<OrderItem>();
for(OrderItem o:Trigger.new)
{
a=[select family from product2 where id= :o.Product2Id];
//o=[select Product2.family from orderItem where product2id= :Trigger.new.product2id];
//o=[select product2.family from orderItem where product2Id IN :Trigger.new];
if(a.family=='Electronics')
{
o.taxRate__c=0.02;
}
else if(a.family=='FootWear')
{
o.taxRate__c=0.05;
}
else if(a.family=='Clothing')
{
o.taxRate__c=0.07;
}
o.finalRate__c=o.UnitPrice*o.Quantity*o.taxRate__c;
system.debug(o.taxRate__c);
system.debug(a.Family);
System.debug(o.finalRate__c);
}
}
Added some comments in your code where improvement is required:
You can use the below code which I have refactored:
I have not compiled it leaving that job to you. Let me know if any further information is required.
If you like the answer then please mark is as solved.
Regards, Ayuhs.
Hi Ian,
Please find the solution, Is below code bad code ?how to make it a good code.
You have not need to query product becouse productid is present in orderitem.
You can do you code simply like this
Please let me know it is working or not.
If you find this soltuion is helpful for you please mark best answer.
Thanks
Another suggestion would with naming. Possibily rename your trigger to something that can be find and comprehended easily, like OrderItemTrigger, and DON'T use abbreveiation like "a" and "o", it makes your code unreadable.