+ Start a Discussion
samthakur74samthakur74 

Saving custom object programmatically in salesforce

I defined a custom object (called Transaction). I populated it within a trigger. The trigger is defined after insert of Task object. After populating Transaction object i want to save it such that it shows up as a object of type Transaction in data management->storage usage.

How do i do this?

The only way I have saved custom objects is by using dataloader to import them. Not sure how to save them directly from within Sdfc apex code.

Any pointers would be appreciated.

regards Sameer

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
trigger tr_task on task (after insert) {
  Transaction__c[] t = new Transaction__c[0];
  for(task ta:trigger.new)
    t.add(new transaction__c(name=ta.subject));
  insert t;
}

 You can use more logic than this, of course, but here's a start.

All Answers

sfdcfoxsfdcfox
trigger tr_task on task (after insert) {
  Transaction__c[] t = new Transaction__c[0];
  for(task ta:trigger.new)
    t.add(new transaction__c(name=ta.subject));
  insert t;
}

 You can use more logic than this, of course, but here's a start.

This was selected as the best answer
-incuGuS--incuGuS-

To insert an Object, as the above poster said, you only need to call "insert varName;"

 

Transaction__c t = new Transaction__c();
t.Name = "My Transaction";
insert t;

 That would insert a new Transaction__c record.

Custom Objects should appear on the Data Storage page.

samthakur74samthakur74

thanks was missing the insert!