You need to sign in to do that
Don't have an account?
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();
}
}
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
What does "Trigger.ProjectMilestone" do? can you post the code for it?
I have the trigger code and test code below error message
The code you have listed is for 'CreateM1Project'. Can you list the trigger code for 'Trigger.ProjectMilestone'?
Regards,
HK.
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
You're creating new Milestone_1_Milestone__c records for MileStone1_Project__c. Shouldn't it be Database.Insert?
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?
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);
}
Thanks, they both work now.
Mike