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
Achilles21Achilles21 

Trigger Help

Hi ,

 

Can anybody guide me through the following scenario.

 

I want to insert into an object on the save action of another object. For the same , I have created a trigger ( after insert ).

How can I insert the data that's getting saved to another object ?

 

Best Answer chosen by Admin (Salesforce Developers) 
TrinayTrinay

Hi,

 

   I hope it will be useful for you,

 

insert data to another object:

trigger createRecord on UserRecord__c(after insert)
{
  //list variable for second object
  List<loginInfo__c> child = new List<loginInfo__c>();
 
  for (UserRecord__c ur : Trigger.new)
  {
       loginInfo__c li = new loginInfo__c();
       li.Name = ur.Name;
       li.UserRecord__c = ur.id;
       child.add(li);
  }
       insert child;
}

 

 

All Answers

TrinayTrinay

Hi,

 

   I hope it will be useful for you,

 

insert data to another object:

trigger createRecord on UserRecord__c(after insert)
{
  //list variable for second object
  List<loginInfo__c> child = new List<loginInfo__c>();
 
  for (UserRecord__c ur : Trigger.new)
  {
       loginInfo__c li = new loginInfo__c();
       li.Name = ur.Name;
       li.UserRecord__c = ur.id;
       child.add(li);
  }
       insert child;
}

 

 

This was selected as the best answer
Achilles21Achilles21

Hi ,

 

Thanks for the reply. But i m afraid this is not working !

 

PrakashbPrakashb

Can you provide us what error you are getting??

Achilles21Achilles21

Hi ,

 

I am not getting any such error but the insertion is not happening either . The following is the modified code according to the previous comment. Here , as you can see i m trying to insert into ManuOrder__c custom object after the insertion into Order__c object.

 

But the records are not getting created.

 

trigger SendOrder on Order__c (after insert)

{
List<ManuOrder__c> rec = new List<ManuOrder__c>();
for(Order__c o:Trigger.new)

{
ManuOrder__c mo = new ManuOrder__c();
mo.catagory__c = o.catagory__c;
rec.add(mo);
}
insert rec;
}

PrakashbPrakashb

How are you inserting the Order records. Did you check the debug logs??

Achilles21Achilles21

I m just inserting them manually . Creating a new Order. Haven't checked the debug logs though ..

PrakashbPrakashb

Can you check the debug log as i feel you might be missing to insert some fields on the object.

Achilles21Achilles21
25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
12:18:49.114 (114142000)|EXECUTION_STARTED
12:18:49.114 (114181000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000B9zO|SendOrder on Order trigger event AfterInsert for [a0I90000006IFsG]
12:18:49.115 (115248000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
12:18:49.115 (115287000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
12:18:49.115 (115494000)|SYSTEM_METHOD_ENTRY|[6]|LIST<Order__c>.iterator()
12:18:49.115 (115732000)|SYSTEM_METHOD_EXIT|[6]|LIST<Order__c>.iterator()
12:18:49.115 (115759000)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
12:18:49.115 (115788000)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
12:18:49.116 (116106000)|SYSTEM_METHOD_ENTRY|[11]|LIST<ManuOrder__c>.add(Object)
12:18:49.116 (116134000)|SYSTEM_METHOD_EXIT|[11]|LIST<ManuOrder__c>.add(Object)
12:18:49.116 (116144000)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
12:18:49.116 (116155000)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
12:18:49.116 (116237000)|DML_BEGIN|[13]|Op:Insert|Type:ManuOrder__c|Rows:1
12:18:49.138 (138775000)|DML_END|[13]
12:18:49.406 (138874000)|CUMULATIVE_LIMIT_USAGE
12:18:49.406|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of script statements: 5 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

12:18:49.406|CUMULATIVE_LIMIT_USAGE_END

12:18:49.138 (138923000)|CODE_UNIT_FINISHED|SendOrder on Order trigger event AfterInsert for [a0I90000006IFsG]
12:18:49.138 (138931000)|EXECUTION_FINISHED
12:18:49.144 (144017000)|EXECUTION_STARTED
12:18:49.144 (144025000)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:01I90000000iwxG
12:18:49.162 (162285000)|WF_RULE_EVAL_BEGIN|Assignment
12:18:49.162 (162307000)|WF_RULE_EVAL_BEGIN|Response
12:18:49.162 (162316000)|WF_RULE_EVAL_BEGIN|Workflow
12:18:49.162 (162332000)|WF_CRITERIA_BEGIN|[Order: O-0012 a0I90000006IFsG]|Send Quatation|01Q9000000015LM|ON_CREATE_ONLY
12:18:49.163 (163188000)|WF_RULE_FILTER|[Order : Quantity greater than 0.0]
12:18:49.163 (163210000)|WF_RULE_EVAL_VALUE|2
12:18:49.163 (163214000)|WF_CRITERIA_END|true
12:18:49.164 (164220000)|WF_SPOOL_ACTION_BEGIN|Workflow
12:18:49.166 (166465000)|WF_FIELD_UPDATE|[Order: O-0012 a0I90000006IFsG]|Field:Order: Order Status|Value:Pending (Waiting for Quatation)|Id=04Y90000000DKsh|CurrentRule:Send Quatation (Id=01Q9000000015LM)
12:18:49.166 (166491000)|WF_ACTION| Field Update: 1;
12:18:49.166 (166502000)|WF_RULE_EVAL_BEGIN|Escalation
12:18:49.166 (166507000)|WF_RULE_EVAL_END
12:18:49.179 (179257000)|WF_ACTIONS_END| Field Update: 1;
12:18:49.179 (179273000)|CODE_UNIT_FINISHED|Workflow:01I90000000iwxG
12:18:49.179 (179281000)|EXECUTION_FINISHED

 PFA the debug log for the insertion .

PrakashbPrakashb

From your debug log, its clear the record is getting created. Did you actually check whether the record is getting created or not.

 

I dont see any issue with your code or with the debug log.

TrinayTrinay

Hi,

  I hope ur record get insertd. u need to do is

  select  ManuOrder__c tab in your account. then press Go button in the view. now u can see the inserted records.