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
MikeCloudMikeCloud 

Apex Trigger Error Help

Need help with ERROR – Trigger Code and Test Code Below -Creating a "Project" Milestone1_Project__c record when an Opp custom field - Start_Project_Site_Id_Stage__c = Yes - This field is dependent upon Stage = "Closed Won" on the opportunity.

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateM1Project: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ProjectMilestone: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.ProjectMilestone: line 16, column 1: []

Trigger.CreateM1Project: line 16, column 1: [] Stack Trace Class.CreateM1Project_test.myTestMethod: line 14, column 1

 

 

TRIGGER CODE

 

trigger CreateM1Project on Opportunity (after insert, after update) {

   

   

   List<Milestone1_Project__c> NewProjects = new List<Milestone1_Project__c>();

 

    for (Opportunity opp: trigger.new)  {

       

        if (opp.Start_Project_Site_Id_Stage__c == 'Yes') {

       

            Milestone1_Project__c freshProject = new Milestone1_Project__c();

            freshProject.Name = opp.Name;

            freshProject.Opportunity__c = opp.Id;

            NewProjects.add(freshProject);

        }

    }

    insert NewProjects;

       

   }

 

 

TEST CODE

 

@IsTest(SeeAllData=false)

public class CreateM1Project_test{

 

    static testMethod void myTestMethod() {

 

        Opportunity opp =

            new Opportunity(Name='Test',

            StageName='Closed Won',

            CloseDate=system.today(),

            Tower_Height__c='100',

            Start_Project_Site_Id_Stage__c='Yes');

           

        Test.startTest();

            insert opp;

        Test.stopTest();

       

    }

}

Best Answer chosen by Admin (Salesforce Developers) 
SaraagSaraag

I would change it to insert (Highlighted in yellow)

 

rigger ProjectMilestone on Milestone1_Project__c (after insert) {
    
    List<Milestone1_Milestone__c> listMilestones = new List<Milestone1_Milestone__c>();
    
    for (Milestone1_Project__c mProject  : trigger.new) {
    
        Milestone1_Milestone__c mMilestone1 = new Milestone1_Milestone__c();
        mMilestone1.Name = 'Milestone 1';
        mMilestone1.Project__c = mproject.Id;
        mMilestone1.Project_Stage__c = 'Site Identification';
        
        listMilestones.add (mMilestone1);
        }
        
        if (listMilestones.isEmpty() == false) {
           Database.insert(listMilestones);
           }

All Answers

SaraagSaraag

What does "Trigger.ProjectMilestone" do? can you post the code for it?

MikeCloudMikeCloud

I have the trigger code and test code below error message

KrishHariKrishHari

The code you have listed is for 'CreateM1Project'. Can you list the trigger code for 'Trigger.ProjectMilestone'?

 

Regards,

HK.

MikeCloudMikeCloud

Sorry long weekend away from this, I forgot this was marked active, it works when this code is inactive, HOWEVER this is what I also want to do: trigger a Milestone1_Milestone__c whenever a Milestone1_Project__c is created, here is the code:

 

trigger ProjectMilestone on Milestone1_Project__c (after insert) {
    
    List<Milestone1_Milestone__c> listMilestones = new List<Milestone1_Milestone__c>();
    
    for (Milestone1_Project__c mProject  : trigger.new) {
    
        Milestone1_Milestone__c mMilestone1 = new Milestone1_Milestone__c();
        mMilestone1.Name = 'Milestone 1';
        mMilestone1.Project__c = mproject.Id;
        mMilestone1.Project_Stage__c = 'Site Identification';
        
        listMilestones.add (mMilestone1);
        }
        
        if (listMilestones.isEmpty() == false) {
           Database.update(listMilestones);
           }

 

Thanks,

Mike

SaraagSaraag

You're creating new Milestone_1_Milestone__c records for MileStone1_Project__c. Shouldn't it be Database.Insert? 

 

 

MikeCloudMikeCloud

Once a "Project" Object record is created I want the first "Milestone"Object  record created.  So my code creating a "Project" when my "Opp" record requirements are met, that trigger works fine, but once I activate this code it fails? What is the right way to accomplish this?

SaraagSaraag

I would change it to insert (Highlighted in yellow)

 

rigger ProjectMilestone on Milestone1_Project__c (after insert) {
    
    List<Milestone1_Milestone__c> listMilestones = new List<Milestone1_Milestone__c>();
    
    for (Milestone1_Project__c mProject  : trigger.new) {
    
        Milestone1_Milestone__c mMilestone1 = new Milestone1_Milestone__c();
        mMilestone1.Name = 'Milestone 1';
        mMilestone1.Project__c = mproject.Id;
        mMilestone1.Project_Stage__c = 'Site Identification';
        
        listMilestones.add (mMilestone1);
        }
        
        if (listMilestones.isEmpty() == false) {
           Database.insert(listMilestones);
           }

This was selected as the best answer
MikeCloudMikeCloud

Thanks, they both work now.

 

Mike