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
sales4s.comsales4s.com 

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.


Best Answer chosen by Admin (Salesforce Developers) 
sanjaypatidarsanjaypatidar

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

sanjaypatidarsanjaypatidar

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.

sales4s.comsales4s.com

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.

sanjaypatidarsanjaypatidar

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.

This was selected as the best answer
sales4s.comsales4s.com

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. 

 

 

sanjaypatidarsanjaypatidar

Incase if you need the trigger, below is the one.

Trigger UpdateTransactions on Transactions__c (After Insert, After Update, After Delete) 
    {
    Set<Id> AccountIds = new Set<Id>();
    list<Achieved__c> updateAccounts = new List<Account>();
    List<Sobject> transactions;

    if (Trigger.isAfter) 
        {
        if (Trigger.isInsert || Trigger.isUpdate) 
            {    
            for(ATM_Transaction__c atm: trigger.new)
                {
                AccountIds.add(atm.Account__c);
                }
            }
        if (Trigger.isDelete) 
            {
            for(ATM_Transaction__c atm: trigger.old)
                {
                AccountIds.add(atm.Account__c);
                }
            }
        }
            
    transactions = [select account__c ar1, sum(amount__c) s from ATM_Transaction__c where account__c IN:AccountIds GROUP BY account__c];
    system.debug('Total -'+transactions);

    
    for (Sobject so:transactions)  
        {
        system.debug('Inside Loop - '+so);
        AggregateResult ar = (AggregateResult) so;
        Integer counter = Integer.valueOf(ar.get('s'));
        Id AccountId = String.valueOf(ar.get('ar1'));
        Account updatetransactions = new Account (Id = AccountId);
        updatetransactions.Balance__c = counter;
        updateAccounts.add(updatetransactions);
        }
          
    if(!updateAccounts.isEmpty())
        {
        try
            {
            update updateAccounts;
            }
        catch(Exception e)
            {
            system.debug('Exception - '+e);
            }
        }
    }