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
prazonprazon 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

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

Starz26Starz26

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

prazonprazon

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

Starz26Starz26

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;

      }

  } 

 

 }

This was selected as the best answer
prazonprazon

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.

 

Set<ID> lstCaseIDs = trigger.newMap.keySet();
    List<Case> lstCases = [Select c.AccountId from Case c where c.Id IN:lstCaseIDs];
    Map<ID,Flag_Reporter__c> mFlags = new Map<ID,Flag_Reporter__c>();
    
    for(Case mcas:lstCases){
        Flag_reporter__c flg =[Select Id,Account_Flag__c from Flag_Reporter__c where Account_Flag__c = mcas.Account.Id];
        mFlags.put(flg.Id,flg);
    }

 

 

Starz26Starz26

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:

 

    Set<ID> acctID = New Set<ID>();
    for(Case c : trigger.new)
          acctID.add(c.AccountID);


    Map<ID,Flag_Reporter__c> mFlags = new Map<ID,Flag_Reporter__c>();
    
    for(Flag_Reporter__c fr:[Select Id,Account_Flag__c from Flag_Reporter__c where Account_Flag__c IN :acctID]){         
         mFlags.put(fr.Account_Flag__c,fr);
    }

    For (Case c : trigger.new){

        //Use this to get the correct Flag_reporter Value
        mFlags.get(c.AccountID)

        //DO STUFF
}

 

prazonprazon

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

 

For (Case c : trigger.new){
        casNum = 0;
        if(Trigger.isInsert){
            insertFlag = true;
            if(c.Issue_Category__c == 'Escalated Issue'){
                
                Flag_Reporter__c fr= mFlags.get(c.AccountID);
                if(c.Issue_DetailsPS__c == 'Additional Surcharge' || c.Issue_DetailsPS__c == 'ASA Issue' || c.Issue_DetailsPS__c == 'Business Closed' || c.Issue_DetailsPS__c == 'Partner Not Honouring Vouchers' || c.Issue_DetailsPS__c ==  'Term of Deal Dispute'){
                    casNum = 1;  
                }
                if(c.Issue_DetailsPS__c == 'Difficulty/Unable to Contact Partner'){
                    if(casNum == 0)
                        casNum = 4;                                
                }
                if(casNum == 0)
                    casNum = 6;
                
                lstEscalataionFlag.add(new Escalation_Flag__c(Flag_Reporter__c=fr.Id, Case__c=c.Id, Number_trg__c=casNum));
                
            }
        }