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
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12 

Insert trigger to create child on parent creation (ID field issue)

Dear all,

I have been playing around and looking for a solution to my problem on these forums for quite a bit now without success (it is worth mentioning I am new to the developer side of SF). Here is what I am trying to accomplish:

Users in our system create a record project__c (master object). The trigger then is meant to create a record for Selling_Team__c (child), using the project owner (or the running user's ID if the first does not work) to populate "User__c" (look-up field). My problem is that I cannot get the ID into the child record without error - "INVALID_FIELD_FOR_INSERT_UPDATE". I have tried several ways with map(), list(), etc. but have not yet managed to get this done. Without the ID insert for User__c, the trigger works fine (child record gets created with information for role__c).

Any help on this would be highly appreciated! (please see below for details of trigger)
Many thanks,

Daniel


-------
trigger CreateSellingTeam on ts2__Project_Job__c  (after insert)
{
//Creates set for Project records
   list<Selling_Team__c> STList = new list<Selling_Team__c>{};

//Loop for new record
  for (ts2__Project_Job__c Pro :trigger.new)
    {STList.add(new Selling_Team__c (
        Role__c = 'Lead CSP',
        User__c = UserInfo.getUserID()
    ));
    } {
    insert STList;
    try { insert STList; } catch (Exception Ex) { system.debug(Ex);
                                                }
                }
}

Satish_SFDCSatish_SFDC
Hi,
Please see if this helps.

http://help.salesforce.com/apex/HTViewSolution?id=000073334&language=en_US

Regards,
Satish Kumar
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12
Hi Satish,

Thank you for your prompt response, but I am afraid the link does not fully describe my situation. In your link, the problem comes from a erroneous attempt to update a field on the parent record, whereas my problem is to get the ID value from the parent record into the newly created child record.

....
for (ts2__Project_Job__c Pro :trigger.new)
    {STList.add(new Selling_Team__c (
        Role__c = 'Lead CSP',
        User__c = UserInfo.getUserID()
...


Regards,
Daniel
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12
Edit to the above: the error message i am getting is as follows  "first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]"