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
Nitin KunalNitin Kunal 

help writing this trigger

I've to write a trigger on "Line_Item__c" object and

field are: Name(Text), Unit Price(Currency), Unit Sold(number), Total Price(Formula:unit_price_c*Unit_sold__c),

 Invoice Statement(Master-Detail:Invoice_Statement__c)

 

Second Object is "Invoice_Statement__c"

and fields are: Name(AutoNumber), Invoice Value(Roll-up summary:Sum of Total_Price__c field of Line_Item__c),

Account__c(Look-up field: Account) 

 

Third Object is "Account"

and fields are: Name(Text), Account_Balance__c(Currency)

 

Now i have to write trigger on "Line_item__c" object to insert total price into Account_Balance__c field of Account.

Please help me if anyone know how to do this.

jiah.choudharyjiah.choudhary
@Nitin,

Do you want to write trigger on insert of Line_Item__c only? or update as well? Is there any condition to be checked before inserting total price or blindly insert into Account_Balance__c?
Nitin KunalNitin Kunal

on update also and no condition is there.

jiah.choudharyjiah.choudhary

@Nitin,

I have written a rough code for only insert case. Please do accordingly for an update case.

 

Trigger InsertTotalPriceIntoAccount on Line_Item__c(after insert, after update)
{
//To store Invoice_Statement__c Ids
Set<Id> setInvoiceStatementIds = new Set<Id>();
for(Line_Item__c objLineItem: Trigger.new)
{
setInvoiceStatementIds.add(objLineItem.Invoice_Statement__c);
}

//To store Total price for each account
Map<Id, Integer> MapAccountId_TotalPrice = new Map<Id, Integer>();
for(Invoice_Statement__c objInvoiceStatement : [Select Id,Invoice_Value__c, Account__c from Invoice_Statement__c where Id IN: setInvoiceStatementIds])
{
MapAccountId_TotalPrice.put(objInvoiceStatement.Account__c, objInvoiceStatement.Invoice_Value__c);
}


//List of account to be updated
List<Account> lstAccount = new List<Account>();
for(Account objAccount : [Select Id, Account_Balance__c from Account where Id IN: MapAccountId_TotalPrice.keySet()])
{
objAccount.Account_Balance__c = (typecast based on data type)MapAccountId_TotalPrice.get(objAccount.Id);
lstAccount.add(objAccount);
}

update lstAccount;
}

 

 

 

Nitin KunalNitin Kunal
objAccount.Account_Balance__c = (typecast based on data type)MapAccountId_TotalPrice.get(objAccount.Id);

What will come here in (typecast based on data type). I'm new in salesforce so please tell me how to write it.
jiah.choudharyjiah.choudhary
@Nitin,

Use data type of Account_Balance__c in (). like (Currency)