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
DevelopementDevelopement 

Issue with this Trigger

Hi,

This Trigger copies some field data from IDea to my custom object. But now when I modify the exisitng record in Idea it creates a new entry instead of updating the 

existing record in my custom object. 

 

Please help.

 

trigger insertfielddata on Idea(after insert,after update) {

    Set<String> ideaTitle= new Set<String>();
    Set<String> setIdea = new Set<String>();
    List<Idea> listIdea = new List<Idea>();
    Map<String, Commerical_Task_Group__c> mapCtg = new Map<String, Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listCtg = new List<Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listUpdtCtg = new List<Commerical_Task_Group__c>();
    
    if (Trigger.isInsert){
        for (Idea Ideas : Trigger.new)
        {
           if(Ideas.ap__c == true){
              Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = Ideas.title,Source__c=Ideas.Source__c,BR_ID__c=Ideas.Idea__c);
              listCtg.add(ctg);
           }
        }
        insert listCtg;
    }
    if (Trigger.isUpdate){
        for (Idea Ideas : Trigger.new)
        {
            
             if( Ideas.ap__c == true){
              listIdea .add(Ideas );
              setIdea.add(Ideas.Title);
                              
             }                       
        }
        listCtg  = [select Name, Source__c ,BR_ID__c from Commerical_Task_Group__c where  name in :setIdea];
        for( Commerical_Task_Group__c ctgtmp : listCtg ){
            mapCtg.put(ctgtmp.name,ctgtmp);
        }
        for( Idea ideatmp : listIdea  ){
            if( mapCtg.get(ideatmp.Title) != null ){
                 Commerical_Task_Group__c uptCTG = mapCtg.get(ideatmp.Title);
                 
                 uptCTG.Source__c = ideatmp.Source__c;   
                 uptCTG.BR_ID__c = ideatmp.Idea__c;               
                 listUpdtCtg.add(uptCTG);
            }else{
                 Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = ideatmp.title,Source__c=Ideatmp.Source__c,BR_ID__c=Ideatmp.Idea__c);
                 listUpdtCtg.add(ctg);
            }
        }
        upsert listUpdtCtg;
     
    }
     
}

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

This may be because of the reason that it is executing the else part in the update section(blue colored text part)

 

trigger insertfielddata on Idea(after insert,after update) {

    Set<String> ideaTitle= new Set<String>();
    Set<String> setIdea = new Set<String>();
    List<Idea> listIdea = new List<Idea>();
    Map<String, Commerical_Task_Group__c> mapCtg = new Map<String, Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listCtg = new List<Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listUpdtCtg = new List<Commerical_Task_Group__c>();
    
    if (Trigger.isInsert){
        for (Idea Ideas : Trigger.new)
        {
           if(Ideas.ap__c == true){
              Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = Ideas.title,Source__c=Ideas.Source__c,BR_ID__c=Ideas.Idea__c);
              listCtg.add(ctg);
           }
        }
        insert listCtg;
    }
    if (Trigger.isUpdate){
        for (Idea Ideas : Trigger.new)
        {
            
             if( Ideas.ap__c == true){
              listIdea .add(Ideas );
              setIdea.add(Ideas.Title);
                              
             }                       
        }
        listCtg  = [select Name, Source__c ,BR_ID__c from Commerical_Task_Group__c where  name in :setIdea];
        for( Commerical_Task_Group__c ctgtmp : listCtg ){
            mapCtg.put(ctgtmp.name,ctgtmp);
        }
        for( Idea ideatmp : listIdea  ){
            if( mapCtg.get(ideatmp.Title) != null ){
                 Commerical_Task_Group__c uptCTG = mapCtg.get(ideatmp.Title);
                 
                 uptCTG.Source__c = ideatmp.Source__c;   
                 uptCTG.BR_ID__c = ideatmp.Idea__c;               
                 listUpdtCtg.add(uptCTG);
            }else{
                 Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = ideatmp.title,Source__c=Ideatmp.Source__c,BR_ID__c=Ideatmp.Idea__c);
                 listUpdtCtg.add(ctg);
            }
        }
        upsert listUpdtCtg;
     
    }
     
}

 

Please check that by using System debug in which condition it is entering.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

souvik9086souvik9086

This may be because of the reason that it is executing the else part in the update section(blue colored text part)

 

trigger insertfielddata on Idea(after insert,after update) {

    Set<String> ideaTitle= new Set<String>();
    Set<String> setIdea = new Set<String>();
    List<Idea> listIdea = new List<Idea>();
    Map<String, Commerical_Task_Group__c> mapCtg = new Map<String, Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listCtg = new List<Commerical_Task_Group__c>();
    List<Commerical_Task_Group__c> listUpdtCtg = new List<Commerical_Task_Group__c>();
    
    if (Trigger.isInsert){
        for (Idea Ideas : Trigger.new)
        {
           if(Ideas.ap__c == true){
              Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = Ideas.title,Source__c=Ideas.Source__c,BR_ID__c=Ideas.Idea__c);
              listCtg.add(ctg);
           }
        }
        insert listCtg;
    }
    if (Trigger.isUpdate){
        for (Idea Ideas : Trigger.new)
        {
            
             if( Ideas.ap__c == true){
              listIdea .add(Ideas );
              setIdea.add(Ideas.Title);
                              
             }                       
        }
        listCtg  = [select Name, Source__c ,BR_ID__c from Commerical_Task_Group__c where  name in :setIdea];
        for( Commerical_Task_Group__c ctgtmp : listCtg ){
            mapCtg.put(ctgtmp.name,ctgtmp);
        }
        for( Idea ideatmp : listIdea  ){
            if( mapCtg.get(ideatmp.Title) != null ){
                 Commerical_Task_Group__c uptCTG = mapCtg.get(ideatmp.Title);
                 
                 uptCTG.Source__c = ideatmp.Source__c;   
                 uptCTG.BR_ID__c = ideatmp.Idea__c;               
                 listUpdtCtg.add(uptCTG);
            }else{
                 Commerical_Task_Group__c ctg = new  Commerical_Task_Group__c(Name = ideatmp.title,Source__c=Ideatmp.Source__c,BR_ID__c=Ideatmp.Idea__c);
                 listUpdtCtg.add(ctg);
            }
        }
        upsert listUpdtCtg;
     
    }
     
}

 

Please check that by using System debug in which condition it is entering.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
DevelopementDevelopement

But when I removed the else part it's not updating anything in my custom object when I make change on Idea record.

souvik9086souvik9086

Yes that means it was entering the else part and that is the reason why it is not updating but inserting new custom object record.

 

 if( mapCtg.get(ideatmp.Title) != null ){

}

else{

//Entering this part

}

 

Please check this

mapCtg.get(ideatmp.Title) so that it contains value.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

DevelopementDevelopement

My getting crazy now...not able to find....

 

Is my looping wrong somewhere?

DevelopementDevelopement

Thanks!

Actually I figured it out :)

Team WorksTeam Works

Hi I tried to work on this but not able to make it work.Is there a relationship between custom Obj and Idea? When i update Idea Title the Title is not found in the existing Cusatom  Obj records and hence always go to else statement..Development please suggest how you make it work...Many Thanks