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
XIOXIO 

Apex trigger is a mixing field data with other records field data

Hello Developers,

I have the Apex Trigger below ​that is mixing the fileds data with the other records data when you edit from the Visualforcepage. Screenshots below. Notice the Action Item field in the Before screenshot compared to the Action Item field in the After screenshot. The data mixed after edit and this should not be happening. Any assistance will be greatly appreciated!

Before
Before
After
After
 
trigger InitiativeHandling on Initiatives__c(before update) {

    
    list<Id> rlist1=new list<Id> ();
    list<id> rlist=new list<id> ();list<String> summm;String sss='';

    for(Initiatives__c i : trigger.new)
        
    {
        
        rlist1.add(i.id);     
    }
    
    
  list<Relationship_to_Initiative_Outcome_Chain__c> ini= [select id,Progress__c from  Relationship_to_Initiative_Outcome_Chain__c  where Initiative_Title__c in: rlist1];
    
 for(Relationship_to_Initiative_Outcome_Chain__c riiP:ini){ 
      
     sss+= riiP.Progress__c + '\n' ;
    
    }    
     
   list<Relationship_to_Initiative_DM__c > rti= [select id,Progress__c from  Relationship_to_Initiative_DM__c where Initiative_Title__c in: rlist1];
   for(Relationship_to_Initiative_DM__c riiP1:rti){ 
    
     sss+= riiP1.Progress__c + '\n' ;
    }

    list<Relationship_to_Initiative_Research__c> rir= [select id,Progress__c from  Relationship_to_Initiative_Research__c where Initiative_Title__c in: rlist1];  
    for(Relationship_to_Initiative_Research__c riiP2:rir){ 
    
     sss+= riiP2.Progress__c + '\n' ;
 
    }
    
 for(Initiatives__c i : trigger.new)
        
    {   
            
        i.Progress__c=sss;

    
    }
    list<Id> rlist2=new list<Id> ();
    list<id> rlist3=new list<id> ();list<String> summmm;String sss2='';

    for(Initiatives__c i : trigger.new)
        
    {
        
        rlist2.add(i.id);     
    }   
   list<Relationship_to_Initiative_Outcome_Chain__c> ini1= [select id,Action_Item_ScriptRICHTEXT__c  from  Relationship_to_Initiative_Outcome_Chain__c  where Initiative_Title__c in: rlist2];   
 for(Relationship_to_Initiative_Outcome_Chain__c riiAIS:ini1){ 
      
     sss2+= riiAIS.Action_Item_ScriptRICHTEXT__c + '<br/>';
    
    }  
       list<Relationship_to_Initiative_DM__c > rti1= [select id,Action_Item_ScriptRICHTEXT__c from  Relationship_to_Initiative_DM__c where Initiative_Title__c in: rlist2];
    for(Relationship_to_Initiative_DM__c riiAIS1:rti1){ 
    
     sss2+= riiAIS1.Action_Item_ScriptRICHTEXT__c + '<br/>';
    }   
      list<Relationship_to_Initiative_Research__c> rir1= [select id,Action_Item_ScriptRICHTEXT__c from  Relationship_to_Initiative_Research__c where Initiative_Title__c in: rlist2];  
    for(Relationship_to_Initiative_Research__c riiAIS2:rir1){ 
    
    sss2+= riiAIS2.Action_Item_ScriptRICHTEXT__c + '<br/>';     
}
 for(Initiatives__c i : trigger.new)
        
    {        
        i.Action_ItemsRICHTEXT__c=sss2;    
    
    }
    list<Id> rlist4=new list<Id> ();
    list<id> rlist5=new list<id> ();list<String> summmmm;String sss3='';

    for(Initiatives__c i : trigger.new)
        
    {
        
        rlist4.add(i.id);     
    }   
   list<Relationship_to_Initiative_Outcome_Chain__c> ini2= [select id,Timeframe__c  from  Relationship_to_Initiative_Outcome_Chain__c  where Initiative_Title__c in: rlist4];   
 for(Relationship_to_Initiative_Outcome_Chain__c riiTF:ini2){ 
      
     sss3+= riiTF.Timeframe__c + '\n' ;
    
    }  
       list<Relationship_to_Initiative_DM__c > rti2= [select id,Timeframe__c from  Relationship_to_Initiative_DM__c where Initiative_Title__c in: rlist4];
    for(Relationship_to_Initiative_DM__c riiTF1:rti2){ 
    
     sss3+= riiTF1.Timeframe__c + '\n' ;
    }   
      list<Relationship_to_Initiative_Research__c> rir2= [select id,Timeframe__c from  Relationship_to_Initiative_Research__c where Initiative_Title__c in: rlist4 ];  
    for(Relationship_to_Initiative_Research__c riiTF2:rir2){ 
    
    sss3+= riiTF2.Timeframe__c + '\n' ;   
}
 for(Initiatives__c i : trigger.new)
        
    {        
        i.Timeframe__c=sss3;    
    
    }
    list<Id> rlist6=new list<Id> ();
    list<id> rlist7=new list<id> ();list<String> summmmmm;String sss4='';

    for(Initiatives__c i : trigger.new)
        
    {
        
        rlist6.add(i.id);     
    }   
    
   list<Relationship_to_Initiative_Outcome_Chain__c> ini3= [select id,Owner__c  from  Relationship_to_Initiative_Outcome_Chain__c  where Initiative_Title__c in: rlist6];   
   for(Relationship_to_Initiative_Outcome_Chain__c riiO:ini3){ 
      
   sss4+= riiO.Owner__c + '\n' ;
    
    }  
    
    list<Relationship_to_Initiative_DM__c > rti3= [select id,Owner__c from  Relationship_to_Initiative_DM__c where Initiative_Title__c in: rlist6];
    for(Relationship_to_Initiative_DM__c riiO1:rti3){ 
    
     sss4+= riiO1.Owner__c + '\n' ;
     
    }   
    
    list<Relationship_to_Initiative_Research__c> rir3= [select id,Owner__c from  Relationship_to_Initiative_Research__c where Initiative_Title__c in: rlist6];  
    for(Relationship_to_Initiative_Research__c riiO2:rir3){ 
    
    sss4+= riiO2.Owner__c + '\n' ;   
}
    for(Initiatives__c i : trigger.new)
        
    {        
        i.Owner__c=sss4;    
    
    }
    }

 
Alain CabonAlain Cabon
Hi,

It lacks the code of the visualforce page and the associated controller or/and apex extensions. 
The mixed data are "saved" by the code as given below (probably). That's not your complete problem perhaps (?).
// for(Initiatives__c i : trigger.new)  
// {        
//        i.Action_ItemsRICHTEXT__c=sss2;     
// }

Suppress or transform the code above into comments the code ( adding // ) and the before/after data will remain the same for the "Action Items" (it lacks the VFP to be sure).

Regards