You need to sign in to do that
Don't have an account?
MS123456
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
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
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
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
Hi Shiva
Balance is customer fields, Transaction Type is picklist in Transaction object
{
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;
}