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
Anjali Joshi 22Anjali Joshi 22 

creating new detail record through trigger

I have 2 custom objects in maste detail relation .I am trying to create new record on detail object as soon as master record is created. so i am using after insert trigger on master. ONe field in detail is fetching values from master. If value is not present in master i wrote code in another class to create new record and insert it.I can see in debug new object created with ID and name.
ASA it inserts i am trying to assign that value to create new record in trigger but i am not able to use it and getting error as invalid ID.
I checked in system the record which i saw in debug is not created in system,as trigger didn't complete.
I want that newly created record(created through another class) to be saved and trigger should be able to use it while creating detail new record.
Can someone help on this?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Anjali,

I found the below links where in a similar implementation is done where in they are updating the field on detail object.

>> https://developer.salesforce.com/forums/?id=906F00000008ykJIAQ

And in the below link there is a discussion on inserting record via trigger I think you can use this as reference for your implentation.

>> https://salesforce.stackexchange.com/questions/161332/trigger-inserting-master-detail-objects

In case if you are following with a trigger please do consider to follow the best practices of writing a trigger so that it can be used without breaking in the future.

I hope these were helpful in your implementation and your query. In case if they helped can you please mark this as the best answer so that they can be used by others in the future and also helps in keeping our community clean.

Regards,
Anutej