+ Start a Discussion
Daniel Hermann 10Daniel Hermann 10 

Trigger to create a custom object record when a new opportunity is made.

When a new opportunity is created I need a trigger that will create a new record in a custom object "Project Requirements." When creating a new Project Requirement record the only required fields are the name of the project requirement(default field) and the opportunity (Master-Detail field). When i create a new opportunity it does not seem to fire this trigger even though it is active. Any insight would be greatly appreciated. I have attempted to pass values to the "Opportunity" field or the "Name" field but that results in either a compilation error or the trigger still does not work.

trigger Project_Requirement on Opportunity (after insert) {
    List <Project_Requirement__c> reqToInsert = new List <Project_Requirement__c>();
    for (Opportunity o : Trigger.new) {
        Project_Requirement__c r = new Project_Requirement__c ();
        reqToInsert.add(r);
    }
    try {
        insert reqToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}
Best Answer chosen by Daniel Hermann 10
srlawr uksrlawr uk
If Opportunity is a master-detail you will need to pass a value into it. If you are getting a compilation error, is the field name maybe not the same as simply "opportunity" ?

I would imagine something like this would work, based on standard naming:
 
trigger Project_Requirement on Opportunity (after insert) {
    List <Project_Requirement__c> reqToInsert = new List <Project_Requirement__c>();
    for (Opportunity o : Trigger.new) {
        Project_Requirement__c r = new Project_Requirement__c (Opportunity__c=o.Id);
        reqToInsert.add(r);
    }
    try {
        insert reqToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

FYI - you know you could totally do this with Process builder too right? Then you don't need code/tests/release headaches...?!!

All Answers

srlawr uksrlawr uk
If Opportunity is a master-detail you will need to pass a value into it. If you are getting a compilation error, is the field name maybe not the same as simply "opportunity" ?

I would imagine something like this would work, based on standard naming:
 
trigger Project_Requirement on Opportunity (after insert) {
    List <Project_Requirement__c> reqToInsert = new List <Project_Requirement__c>();
    for (Opportunity o : Trigger.new) {
        Project_Requirement__c r = new Project_Requirement__c (Opportunity__c=o.Id);
        reqToInsert.add(r);
    }
    try {
        insert reqToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

FYI - you know you could totally do this with Process builder too right? Then you don't need code/tests/release headaches...?!!
This was selected as the best answer
Daniel Hermann 10Daniel Hermann 10
Thank you so much! That did the trick. I then passed a specific value for the opportunity name to the required name field so it did not match the Opportunity ID. This was my first attempt at a trigger so thank you for the assistance!