You need to sign in to do that
Don't have an account?

apex trigger after update ID
HI,
I have an object "Financial_Transaction__c" and I create an instance of that object, it triggers the creation of 2 instances of the object "Accounting_Move__c".
Now I don't know how to proceed if I want to update the transaction, so that it updates as well the accounting_move without creating new one.
I know I need {after update}, then I need to find first the Id of the accounting_Move created? That's when I'm stuck, should I put the SELECT before or after the for? I've tried a few things, but I always get errors?
Thanks for your advices.
I have an object "Financial_Transaction__c" and I create an instance of that object, it triggers the creation of 2 instances of the object "Accounting_Move__c".
Now I don't know how to proceed if I want to update the transaction, so that it updates as well the accounting_move without creating new one.
trigger FinancialTransaction on Financial_Transaction__c (after insert) {
List<Accounting_Move__c> listaccountingmove = new List<Accounting_Move__c>();
for (Financial_Transaction__c ofintran : trigger.New) {
Accounting_Move__c oAccounting_Move = new Accounting_Move__c();
oAccounting_Move.Accounting_Move_Date__c = ofintran.Date__c;
oAccounting_Move.Accounting_Move_Description__c = ofintran.Transaction_Description__c;
oAccounting_Move.Financial_Transaction__c = ofintran.Id;
Accounting_Move__c oAccounting_Move2 = oAccounting_Move.clone();
oAccounting_Move.From_Account__c = ofintran.From_Account__c;
oAccounting_Move.Transfer_Account__c = ofintran.To_Account__c;
oAccounting_Move.Transaction_Amount__c = - ofintran.Transaction_Amount__c;
listaccountingmove.add(oAccounting_Move);
oAccounting_Move2.From_Account__c = ofintran.To_Account__c;
oAccounting_Move2.Transfer_Account__c = ofintran.From_Account__c;
oAccounting_Move2.Transaction_Amount__c = ofintran.Transaction_Amount__c;
listaccountingmove.add(oAccounting_Move2);
}
insert listaccountingmove;
List<Accounting_Move__c> listaccountingmove = new List<Accounting_Move__c>();
for (Financial_Transaction__c ofintran : trigger.New) {
Accounting_Move__c oAccounting_Move = new Accounting_Move__c();
oAccounting_Move.Accounting_Move_Date__c = ofintran.Date__c;
oAccounting_Move.Accounting_Move_Description__c = ofintran.Transaction_Description__c;
oAccounting_Move.Financial_Transaction__c = ofintran.Id;
Accounting_Move__c oAccounting_Move2 = oAccounting_Move.clone();
oAccounting_Move.From_Account__c = ofintran.From_Account__c;
oAccounting_Move.Transfer_Account__c = ofintran.To_Account__c;
oAccounting_Move.Transaction_Amount__c = - ofintran.Transaction_Amount__c;
listaccountingmove.add(oAccounting_Move);
oAccounting_Move2.From_Account__c = ofintran.To_Account__c;
oAccounting_Move2.Transfer_Account__c = ofintran.From_Account__c;
oAccounting_Move2.Transaction_Amount__c = ofintran.Transaction_Amount__c;
listaccountingmove.add(oAccounting_Move2);
}
insert listaccountingmove;
I know I need {after update}, then I need to find first the Id of the accounting_Move created? That's when I'm stuck, should I put the SELECT before or after the for? I've tried a few things, but I always get errors?
Thanks for your advices.
I guess my problem is more on how to find the 2 moves to update as it seems like I need to refer to the financial_transaction__c object for that.
Accouting_Move__C move1 = SELECT[Id FROM Accounting_Move__c WHERE Financial_Transaction__c = ofintran.Id Limit 1]
this doesn't really work, and I don't know how to differentiate between the 2 move, except that their Id will be one apart.
Thanks
trigger FinancialTransactionTrigger on Financial_Transaction__c (after update) {
Map<Id,Id> mapFTAM = new Map<Id,Id>();
List<Accounting_Move__c> lstToInsert = new List<Accounting_Move__c>();
//fetch all existing Accounting Move records associated with triggered Financial Transactions
List<Accounting_Move__c> lstAM = [select Id,Financial_Transaction__c from Accounting_Move__c where Financial_Transaction__c IN :trigger.new];
for(Accounting_Move__c am : lstAM){
mapFTAM.put(am.Financial_Transaction__c,am.Id);//store Accounting Move and Financial Transaction mapping.
}
for(Financial_Transaction__c ft : trigger.new){
Accounting_Move__c tempAM;
if(mapFTAM.containsKey(ft.Id)){
tempAM = new Accounting_Move__c(Id=mapFTAM.get(ft.Id));//if mapping exists simply refer to existing Accounting Move object
}else{
tempAM = new Accounting_Move__c();// if mapping does not exists, create a new object
}
tempAM.From_Account__c = ft.From_Account__c;
lstToInsert.add(tempAM);
}
if(!lstToInsert.isEmpty())
upsert lstToInsert;
}
Incase you have multiple Accounting Move with one Financial Transaction, use list as map values to store multiple ids like mapFTPM.put(am.Financial_Transaction__c,new List<Id>{pm1.id,pm2.id....}).
Hope this helps.
Regards
Sachin
Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon
trigger FinancialTransaction on Financial_Transaction__c ( after insert, after update )
{
Map<Id,List<Accounting_Move__c>> map_Moves = new Map<Id,List<Accounting_Move__c>>();
for ( Accounting_Move__c move :
[ SELECT Id, Accounting_Move_Date__c, Accounting_Move_Description__c,
Financial_Transaction__c, From_Account__c, Transfer_Account__c,
Transaction_Amount__c
FROM Accounting_Move__c
WHERE Financial_Transaction__c IN :trigger.new
]
)
{
if ( !map_Moves.containsKey( move.Financial_Transaction__c ) )
{
map_Moves.put( move.Financial_Transaction__c, new List<Accounting_Move__c>() );
}
map_Moves.get( move.Financial_Transaction__c ).add( move );
}
List<Accounting_Move__c> listaccountingmove = new List<Accounting_Move__c>();
for ( Financial_Transaction__c ofintran : trigger.new )
{
List<Accounting_Move__c> list_Moves = map_Moves.get( ofintran.Id );
Accounting_Move__c oAccounting_Move =
list_Moves != null && list_Moves.size() >= 1 ? list_Moves[0] : new Accounting_Move__c();
oAccounting_Move.Accounting_Move_Date__c = ofintran.Date__c,
oAccounting_Move.Accounting_Move_Description__c = ofintran.Transaction_Description__c,
oAccounting_Move.Financial_Transaction__c = ofintran.Id
oAccounting_Move.From_Account__c = ofintran.From_Account__c;
oAccounting_Move.Transfer_Account__c = ofintran.To_Account__c;
oAccounting_Move.Transaction_Amount__c = - ofintran.Transaction_Amount__c;
listaccountingmove.add( oAccounting_Move );
Accounting_Move__c oAccounting_Move2 =
list_Moves != null && list_Moves.size() >= 2 ? list_Moves[1] : new Accounting_Move__c();
oAccounting_Move2.Accounting_Move_Date__c = ofintran.Date__c,
oAccounting_Move2.Accounting_Move_Description__c = ofintran.Transaction_Description__c,
oAccounting_Move2.Financial_Transaction__c = ofintran.Id
oAccounting_Move2.From_Account__c = ofintran.To_Account__c;
oAccounting_Move2.Transfer_Account__c = ofintran.From_Account__c;
oAccounting_Move2.Transaction_Amount__c = ofintran.Transaction_Amount__c;
listaccountingmove.add( oAccounting_Move2 );
}
upsert listaccountingmove;
}
</pre>