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
Sreenu Reddy 16Sreenu Reddy 16 

creation / update task description based on lookup field " most recent campaign "


hi viewers: i have two fields in task obj most recent campaign (lookup to campaign)  and description , i want to update the description  as mosot resend campaign (lookup to campaign ) in task.. using before trigger. i wrote some logic  updateDescription on Task (Before insert,Before Update) . i am getting fallowing error when i am create record. any one help me .


{

            set<id> setcmpnid =new set<id>();
            
            for(task tsk:trigger.new)
              {
                String wId = tsk.WhatId;
                
                if(wId!=null && !setcmpnid.contains(tsk.WhatId))
                { 
                  setcmpnid.add(tsk.WhatId);
                  system.debug('++++++ the setcmpnid  value is +++++++'+setcmpnid);
                } 
            }
            
            if(setcmpnid.size()>0) {
            
            list<campaign> tskcmpn= [Select id, Description from campaign where id in :setcmpnid ];
            
            Map<id,campaign> cmpn= new Map<id,campaign>();
               for(campaign cpn : tskcmpn)
              {
                  cmpn.put(cpn.Id,cpn); 
                   system.debug('++++++ the campaign id value is +++++++'+cmpn);
              }
            
              for (task tsk:trigger.new) {
                                
                String wId = tsk.WhatId; 
            
               if(wId!=null) { 
                  
               campaign thiscpn = cmpn.get(tsk.WhatId);  
               
               if(thiscpn != null){
               
               tsk.most_Resent_Campaign__c = thiscpn.Description;
            
               system.debug('++++++ the tsk.most_Resent_Campaign__c value is +++++++'+tsk.most_Resent_Campaign__c);
               
                  }            
                }       
            
              }
            
            }
         }
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateDescription caused an unexpected exception, contact your administrator: updateDescription: execution of BeforeUpdate caused by: System.StringException: Invalid id: sfdc salesforce sfdc : Trigger.updateDescription: line 37, column 1
Sreenu Reddy 16Sreenu Reddy 16
 I solution for above question use the bellow code :
trigger  updateDescription on Task (Before insert,Before Update) {

            set<id> setcmpnid =new set<id>();
            
            for(task tsk:trigger.new)
              {
                String wId = tsk.WhatId;
                
                if(wId!=null && !setcmpnid.contains(tsk.WhatId))
                { 
                  setcmpnid.add(tsk.WhatId);
                  setcmpnid.add(tsk.most_Resent_Campaign__c);
                  
                } 
                
            }  system.debug('@@@@@@@ the setcmpnid  value is +++++++'+setcmpnid.size());
            
            
            if(setcmpnid.size()>0) {
            system.debug('++++++++cmpn size==='+setcmpnid.size());
            
            list<campaign> tskcmpn= [Select id,Description from campaign where id in :setcmpnid ];
            
            Map<id,campaign> cmpn= new Map<id,campaign>();
               for(campaign cpn : tskcmpn)
              {
                  cmpn.put(cpn.Id,cpn); 
                   system.debug('++++++ the campaign id value is +++++++'+cmpn);
              }
            
              for (task tsk:trigger.new) {
                                
                String wId = tsk.WhatId; 
            
               if(wId!=null) { 
                  
               campaign thiscpn = cmpn.get(tsk.WhatId);  
               
               if(thiscpn != null){
               
               tsk.campaig_Description__c = thiscpn.Description;
            
               system.debug('++++++ the tsk.most_Resent_Campaign__c value is +++++++'+tsk.most_Resent_Campaign__c);
               
                  }            
                }       
            
              }
            
            }
         }