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
husna azizhusna aziz 

trigger to calculate bill

Hi everyone
suppose we have two objects 1.item and 2.bill
object item has 4 fields
field4-bill(look up relationship with custom object bill).
now bill is another custom object with fields
customer name and
total amount
now i need to write trigger to calculate total amount in the bill object

example we buy 2 soaps of $10
and 2 shirts of $100
so total amount would be 20 + 200 =220

anyone plz help..
Hello, Please note the below steps.

Built a Master-Detail relationship between Bill and Item.(Bill- Parent, Item - Child)
Then, create a formula field on Item as Total Line Item Cost, with formula as (Quantity * Price)
Now, create a roll up summary field on Bill Object, which aggregares Total Line Item Cost and sums it up.

Hope this helps. Kindly mark it as best answer if this resolves your problem.
syeda syedasyeda syeda
Hi susna aziz,

           use the code below and try it yourself.

trigger itemandbill on item__c(before insert,before update){
list<bill__c> listofbill=new list<bill__c>();
bill__c b=new bill__c();

i didn't execute the code, you can check and let me know if it helpful to you

husna azizhusna aziz
Hi syeda syeda
 actually i want to write trigger in bill__c object
husna azizhusna aziz
Hi hemantjain
we alredy have lookup relation b/w item and bill.

One question here, can a line item exist without a bill record? If not, i would recommend to convert the lookup relationship to master-detail. Else, you will have to write the trigger on Item object itself. If you write a trigger on Bill object, you cannot fire a event on insertion/deletion/updation of Item record.

The value on Bill should be updated only when the item record is being actioned. So a trigger on Item object would be required here.