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
sales4cesales4ce 

Trigger Help--Need to create a Custom record!

Hi,

 

I have 2 objects. One is a standard object and other is a custom object.

 

what i am trying to accomplish is, whenever a record is created on standard object, i want to create a record on custom object and populate some of the fields information in the custom object.

 

ex: Whenever a record  is created on account object, i want to create a new record in custom object with account's "Type", "Address Information" populated in Custom Object fields.

 

Can any one help me with this.

 

Thanks in advance!

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
Shailesh DeshpandeShailesh Deshpande

trigger updateCustomObject on Account(after insert)

{
    List<CustomObect> lst   = new List<Custom object>();
    Set<Id> setAccounts = new Set<Id>
 
    for (Integer i = 0; i < Trigger.new.size(); i ++)
      {
        setAccounts.add(Trigger.new[i].Id);
      }
   
    lstAccounts = [select "fields required" from account where id in: setAccounts ];

    for (Integer i = 0; i < lstAccounts.size(); i ++)
    {
        CustomObject obj = new CustomObject();
        obj.field1 = lstAcounts[i].field1;
        obj.field2 = lstAcounts[i].field2;
        ....some more fields...
   
        lstCustomObject.add(obj);
    }

    if(lstCustomObject.size > 0)
    {
        insert lstCustomObject;
    }
 
}

 

 

There may be some other optimized method for this....but i hope this helps..

All Answers

Shailesh DeshpandeShailesh Deshpande

trigger updateCustomObject on Account(after insert)

{
    List<CustomObect> lst   = new List<Custom object>();
    Set<Id> setAccounts = new Set<Id>
 
    for (Integer i = 0; i < Trigger.new.size(); i ++)
      {
        setAccounts.add(Trigger.new[i].Id);
      }
   
    lstAccounts = [select "fields required" from account where id in: setAccounts ];

    for (Integer i = 0; i < lstAccounts.size(); i ++)
    {
        CustomObject obj = new CustomObject();
        obj.field1 = lstAcounts[i].field1;
        obj.field2 = lstAcounts[i].field2;
        ....some more fields...
   
        lstCustomObject.add(obj);
    }

    if(lstCustomObject.size > 0)
    {
        insert lstCustomObject;
    }
 
}

 

 

There may be some other optimized method for this....but i hope this helps..

This was selected as the best answer
sales4cesales4ce
Thanks Shailesh, it helped me!
fishernfishern

Thanks for this it has been very helpful. Although I am getting an error at the "for" statement

 

I have added my code to help

 

 trigger updateActivityTransaction on Event(after insert)

{
    ListActivity_Transaction__c lst   = new ListActivity_Transaction__c();
    Set<Id> setEvent = new Set<Id>
 
    for (Integer i = 0; i < Trigger.new.size(); i ++)
      {
        setEvent.add(Trigger.new[i].Id);
      }
   
    lstEvent = [select Type,ActivityDate  from event where id in: setEvent ];

    for (Integer i = 0; i < lstAccounts.size(); i ++)
    {
        Activity_Transaction__c obj = new Activity_Transaction__c();
        obj.Type__c = lstEvent[i].Type;
        obj.Activity_Date__c = lstEvent[i].ActivityDate;
    
   
        lstActivity_Transaction__c.add(obj);
    }

    if(lstActivity_Transaction__c.size > 0)
    {
        insert lstActivity_Transaction__c;
    }
 
}

 

 

If there is anything you can do to point me in the right direction I would very much appreciate it

sales4cesales4ce

Hi fishern,

 

Update code:

 

trigger updateActivityTransaction on Event(after insert)

{
    List<Activity_Transaction__c> lst   = new List<Activity_Transaction__c>();
    Set<Id> setEvent = new Set<Id>
 
    for (Integer i = 0; i < Trigger.new.size(); i ++)
      {
        setEvent.add(Trigger.new[i].Id);
      }
  
    List<event> lstEvent = [select Type,ActivityDate  from event where id in: setEvent ];

    for (Integer i = 0; i < lstEvent.size(); i ++)
    {
        Activity_Transaction__c obj = new Activity_Transaction__c();
        obj.Type__c = lstEvent[i].Type;
        obj.Activity_Date__c = lstEvent[i].ActivityDate;
   
  
        lst.add(obj);
    }

    if(lst.size > 0)
    {
        insert lst;
    }
 
}

 

Reason for error:

 

there is nothing called "lstaccounts" in your for loop condition check.

Also,lstEvent should be declared as List<Event>.

 

Hope it helps

 

Thanks,

Sales4ce