You need to sign in to do that
Don't have an account?
prazon
trigger insertion problem
Guys ,
I am having the following trigger which should one row of data, instead it is instead it is inserting 4 rows. Can anybody tell me what can be the solution?
List<Flag_Reporter__c> lstFlagReported = [Select Id, Account_Flag__c from Flag_Reporter__c]; List<Flag_Reporter__c> lstFlagReporterUpdate = new List<Flag_Reporter__c>(); List<Escalation_Flag__c> lstEscalataionFlag = new List<Escalation_Flag__c>(); Integer casNum=0; Boolean updateFlag = false; Boolean insertFlag = false; Boolean mysteryFlag = false; for(Case cas:Trigger.new){ if(cas.Issue_Category__c == 'Escalated Issue'){ if(Trigger.isInsert){ for(Flag_Reporter__c flag:lstFlagReported ){ if(cas.AccountId==flag.Account_Flag__c){ insertFlag = true; if(cas.Issue_DetailsPS__c == 'Additional Surcharge' || cas.Issue_DetailsPS__c == 'ASA Issue' || cas.Issue_DetailsPS__c == 'Business Closed' || cas.Issue_DetailsPS__c == 'Partner Not Honouring Vouchers' || cas.Issue_DetailsPS__c == 'Term of Deal Dispute'){ casNum = 1; }else if(cas.Issue_DetailsPS__c == 'Difficulty/Unable to Contact Partner'){ casNum = 4; }else{ casNum = 6; } } lstEscalataionFlag.add(new Escalation_Flag__c(Flag_Reporter__c=flag.Id, Case__c=cas.Id, Number_trg__c=casNum)); } } } } try{ if(insertFlag == true) insert lstEscalataionFlag; }catch(Exception ex){ ex.getMessage(); } }
Here there is only one Flag_Reporter__c record against one Account.
By populating a map with the records related to the trigger.
You can Build the map with the keyset of the trigger records or a standard map of the recordids to the record in the query.
The first way require 1 loop to build the map. The seecond require one statement to build the map but a loop on each trigger record to find the correct record in the map it is related to:
For example Trigger on Account, finding Opportunities
Option 1: (not taking into account multiple opps)
Set<ID> accID = trigger.newMap.keySet();
Map<ID,Opportunity> mOpps = New Map<ID, Opportunity>();
for (Opportunity a : [Select Name, AccountID From Opportunity Where AccountID IN :accID])
mOpps.put(a.id,a);
for(Account a : trigger.new)
mOpps.get(a.id);
Option 2:
Set<ID> accID = trigger.newMap.keySet();
Map<ID,Opportunity> mOpps = New Map<ID, Opportunity>([Select Name, AccountID From Opportunity Where AccountID IN :accID]);
for(Account a : trigger.new){
for(Opportunity o : mOpps.values()){
if(o.AccountID == a.id){
//do something
break;
}
}
}
All Answers
Most likely because lstFlagReported is returning 4 records.
They way you have it structured, it will return ALL Falg_Reported__c records not just those specific to a single account
Hi,
What can be possible work around?
by putting flagReporter query within for(Case cas:Triggner.new) loop, will that support bulk handling?
Please tell me
Regards
Prazon
By populating a map with the records related to the trigger.
You can Build the map with the keyset of the trigger records or a standard map of the recordids to the record in the query.
The first way require 1 loop to build the map. The seecond require one statement to build the map but a loop on each trigger record to find the correct record in the map it is related to:
For example Trigger on Account, finding Opportunities
Option 1: (not taking into account multiple opps)
Set<ID> accID = trigger.newMap.keySet();
Map<ID,Opportunity> mOpps = New Map<ID, Opportunity>();
for (Opportunity a : [Select Name, AccountID From Opportunity Where AccountID IN :accID])
mOpps.put(a.id,a);
for(Account a : trigger.new)
mOpps.get(a.id);
Option 2:
Set<ID> accID = trigger.newMap.keySet();
Map<ID,Opportunity> mOpps = New Map<ID, Opportunity>([Select Name, AccountID From Opportunity Where AccountID IN :accID]);
for(Account a : trigger.new){
for(Opportunity o : mOpps.values()){
if(o.AccountID == a.id){
//do something
break;
}
}
}
Hi
tried this earlier but it is not compiling as both Case and flag_reporter is linked to Account I need to get the flag_repoter linked to a particular account.
This does not take into account the potential of multiple Flag_Reporter related to a single account. Also I am assuming that this trigger is on the case object.
try:
Thanks a lot man :)
I have this last problem
Here Actually I want once value set to 1 for casNum it should remain as 1, unless if it is set 4 then it should remain 4 or if none of them qualifies it should become 6.... but it is always getting over-written by 6 ..Please looking for your help