+ Start a Discussion
rebvijkumrebvijkum 

Trigger to create new record

Trigger on a object Article_Feedback__c triggers whenever a record is created, and gets Article_ID__c,Comments__c,CreatedById,Name FROM Article_Feedback__c and creates a new record in Core_Benefit__Feed obj.

So far i got:
My Code:
trigger createfeed on Article_Feedback__c (after insert,after update) {
    public Core_Benefit__Feed feedobj = new Core_Benefit__Feed();
    Public Article_Feedback__c FeedItem;
   
    FeedItem=[SELECT Article_ID__c,Comments__c,CreatedById,Name FROM Article_Feedback__c];
   
    if(Trigger.isAfter){
        if(Trigger.isInsert || Trigger.isUpdate){
       
        //feedobj.ParentId=FeedItem.Article_ID__c;
        //feedobj.Title=FeedItem.Name;
        //feedobj.InsertedById=FeedItem.CreatedById;
        //feedobj.Body=FeedItem.Comments__c;
        //feedobj.Type='Textpost';
        }
    }
}
PratikPratik (Salesforce Developers) 
Hi,

You can refer to the sample code to create the contact when account is created.


trigger createcontact on account (after insert) {
   
 
    List<contact> conlist= new list<contact>();
    List<account> acclist= [select id, name,industry from account where id IN:trigger.newmap.keyset()];
   
    for(account acc:acclist)
   
    {
      contact newcon= new contact(lastname='pm',accountid=acc.id,firstname=acc.name );
      conlist.add(newcon);
     
    }
        insert conlist;

}


Thanks,
Pratik
rebvijkumrebvijkum
I'm getting an Error: Compile Error: Field is not writeable: Core_Benefit__Feed.ParentId.
can u tell me where i'm doing wrong. The trigger has to fire everytime when a record is created in Article_Feedback__c object and has to create a record in Core_Benefit__Feed object


trigger createfeed on Article_Feedback__c (after insert,after update) {
     List<Core_Benefit__Feed> feedobj = new List<Core_Benefit__Feed>();
   
    for (Article_Feedback__c o : Trigger.new) {
    if(Trigger.isAfter){
        if(Trigger.isInsert || Trigger.isUpdate){
        Core_Benefit__Feed v=new Core_Benefit__Feed();
        v.ParentId=o.Article_ID__c;
        v.Title=o.Name;
        v.InsertedById=o.CreatedById;
        v.Body=o.Comments__c;
        v.Type='Textpost';
        feedobj.add(v);
        }
       
    }
}
try {
        insert feedobj;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }


}