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
MS123456MS123456 

how to write this use case trigger.....pls any help




 Use Case:
whenever a new transaction is perforned successfully then update the customer object balance field based on --
if transaction type= deposit,
then balance = balance+amount;

if transaction type= withdraw,
then balance = balance-amount; 
 
 Note- Customer and Transaction have lookup detail relationship
ManidManid
hai mayur i am also in learning stage. i have some idea with that i implemented this if anything wrong i am also know that where i assume wrong. you have get some clarity in the fields which object fields are they and type is a picklist field in the transaction object. i assume amount field is trasactions object and balance is customer.

you have to take after insert trigger b/c after sucessful transaction.

list<customer__c> bulkc=new list<customer__c>();
for(transaction__c amt:trigger.new){
if(cts.type=='deposit'){
customer__c deposit=new customer__c();
deposit.id=cts.customer__c;
deposit.balance__c=deposit.balance__c+cts.amount__c;
bulk.add(deposit);
}
else
{
customer__c wtdrw=new customer__c();
wtdrw.id=cts.customer__c();
wtdrw.balance__c=wtdrw.balance__c-cts.amount__c;
bulk.add(wtdrw);
}
}
update bulkc;
}

check it works or not
Shiva RajendranShiva Rajendran
Hi Mayur,
Could you please elaborate your question. Is balance,amount are fields of customer object?
Also what do new transaction is perforned successfully stands for? Is it while inserting a new transaction record or updating a record because trigger can be written only for update or insert operation.

Please elaborate your question.

Thanks and Regards,
Shiva RV
SFDC TortoiseSFDC Tortoise

Hi Shiva

Balance is customer fields, Transaction Type is picklist in  Transaction object 
 

hemant ghaturkar 18hemant ghaturkar 18
Trigger UpdateBalance on transaction__c (after insert,after update)
{
set<id>setCustomerId=new set<id>();
for(transaction__c trasList:trigger.new)
{
setCustomerId.add(trasList.customer__C);
}

Map<id,customer__c>custList=new Map<id,customer__c>([select id,balance__c from Customer__c where id IN: setCustomerId]);
List<customer__c>cList=new List<customer__c>();
customer__c cust=new customer__c();
for(transaction__c trasList:trigger.new)
{
if(custList.containsKey(trasList.customer__c)){
if(trasList.Transaction_Type__c=='Deposit'){
cust.Id = trasList.customer__c;
cust.Balance__c=custList.get(trasList.customer__c).Balance__c+trasList.Amount__c; 
cList.add(cust);
}
else
{
if(custList.get(trasList.customer__c).Balance__c > trasList.Amount__c)
{
cust.Id = trasList.customer__c;
cust.Balance__c=custList.get(trasList.customer__c).Balance__c-trasList.Amount__c; 
cList.add(cust);
}
else
{
trasList.Amount__c.addError('insuficiant fund Available'); 
}
}
}
}
update cList;
}