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
kirankumarreddy punuru 10kirankumarreddy punuru 10 

How to copy data from a standard object to a custom object

Hi ,

  I am trying to copy data from a standard object to a custom object through trigger and i have written one trigger for this please help in this trigger wheather this is correct or i need to change .My trigger is:
 

trigger chatterComments on FeedComment (after insert) 
{    List<FeedComment__c> customObj = new List<FeedComment__c>();
    List<FeedComment> standardObj = new List<FeedComment>();
   lststandardObj =[Select id,CommentBodyfrom FeedComment];
    
    for(FeedComment fc:lststandardObj){
        FeedComment__c  fc1 = new FeedComment__c();
        fc1.CommentBody__c = lststandardObj.CommentBody;
        customObj.add(fc1);
    }
    if(customObj.size()>0){
      insert  customObj;  
    }
      
}
Prashanth KrishnamurthyPrashanth Krishnamurthy
  • Can you make this a future call?
  • Prefer using a batch (Apex/other) in place of a trigger if the insert can wait
JeffreyStevensJeffreyStevens
I don't think you want to do a SOQL on the entire FeedComment object.  In fact - I don't think you need to SOQL that object at all.  You want to just add the records to FeedComment__c that fired the trigger - correct?

If that's the case, then remove the lstandardobjec =[Select .... line.  Chang the for loop to this...
for(FeedComment fc :trigger.new) {
kirankumarreddy punuru 10kirankumarreddy punuru 10
Hi Jeffrey,
              Thanks for your comment , yes i need to copy standard object data to  a custom object that' s it for that no need to write query ?
JeffreyStevensJeffreyStevens
correct.  See when you write a trigger, the trigger.new is a list<FeedComment__c> records that fired the trigger.
kirankumarreddy punuru 10kirankumarreddy punuru 10
Hi i have modified the above trigger:
Is this is correct
trigger ChatterTrigger on FeedComment (after insert) {
{   List<FeedComment__c> customObj = new List<FeedComment__c>();
    
     for(FeedComment fc:Trigger.new){
     FeedComment__c  fc1 = new FeedComment__c();
      fc1.Body__c = fc.CommentBody;
      customObj.add(fc1);
    }
    if(customObj.size()>0){
      insert  customObj;  
    }
      
}

}