You need to sign in to do that
Don't have an account?
triigger, object updates
want to update another object say 1."account_master__c" from 2nd"ATM_transacction__c".writing after insert trigger in 2nd object.please look at the code.
trigger atm2 on ATM_Transaction__c (after insert)
{
for(atm_transaction__c atm: trigger.new)
{
aggregateResult a=[select sum(amount__c) s from atm_transaction__c where
account_id__c=:atm.account_id__c];
double i=(double)a.get('s');
account_master__c am=new account_master__c();
am=[select id, balance__c from account_master__c where
account_idm__c=:atm.accout_id__c]
if(am.balance__c > i){
am.balance__c=am.balance__c -i;
}
//update am;
}
update am;
}
where i wenr wrong.
thanks in advance.
Can you not have the master detail relationship for the account_master__c and balance__c.
this would help you to use the standard roll-up summary fields in order to get the balance amount without writing the code ?
Let me know if this helps.
All Answers
Is there any specific condtion on which you want to update the other object ? If so list the condition.
Also normally you should not query within the for loops.
Sanjay, just I wanted to update the object"account_master__c" field-"balance__c". Whenever a user inserts the record in
object"atm_transaction__c" in field "amount__c".
For example, if user inserts/draw 20,000 from atm_transaction__c, and balance should be updated by subtracting of 20,000 from Balance__c;if say balance is 40,000, so it should be updated as 20000.thanks much.
Can you not have the master detail relationship for the account_master__c and balance__c.
this would help you to use the standard roll-up summary fields in order to get the balance amount without writing the code ?
Let me know if this helps.
Sanjay, Here i can only do is SUM,MIN,MAX and COUNT of a field of related object, but my task is subtracting drawn
amount from balance and make update the field.
With Mastr_detail relationship,I can only have the same id field which helps me to acces or retrive records throgh SOQL queries from the objects.
will write soon.thaks for your valuable tips and suggsions.
Incase if you need the trigger, below is the one.