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
MjrAdminMjrAdmin 

Trigger New Record from Same Object Problem

I have a custom Object: Milsetone__c, (it is a Child of custom Object: Project__c), I want to create a new Milestone record when the current Milestone Record that has a Project_Stage__c = 'Pre-construction Engineering' and a checkbox field called: Complete__c = true.  In my sandbox it just updates the exisitng record, does not create new record?  I need help I've written several triggers, but none like this - same Object/new record, 

 

 

trigger MilestoneZoning on Milestone__c (after insert) {

List<Milestone__c>newMilestones = new List<Milestone__c>();

for(Milestone__c ms :trigger.new) {

if( (ms.Name == 'Pre-construction Engineering Milestone')
&& (ms.Complete__c == True)){
Milestone__c freshMilestone = new Milestone__c();
freshMilestone.Name = 'Pre-construction Zoning Milestone';
freshMilestone.Opportunity__c = ms.opportunity__c;
freshMilestone.Project_Stage__c = 'Pre-construction Zoning';
freshMilestone.Project__c = ms.Project__c;
freshMilestone.Project_Manager__c = ms.Project_Manager__c;


newMilestones.add(freshMilestone);

}
}

Insert newMilestones;
}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I suggest you go that route and confirm that records are matching the criteria.  If none are, your trigger is working correctly.

All Answers

bob_buzzardbob_buzzard

Your code looks okay to me - have you added in some debug to check that any records in the trigger are matching the condition?

MjrAdminMjrAdmin

no I have not.

bob_buzzardbob_buzzard

I suggest you go that route and confirm that records are matching the criteria.  If none are, your trigger is working correctly.

This was selected as the best answer
MjrAdminMjrAdmin

Thanks for your help and replies

Tzemach Aronow 4Tzemach Aronow 4
can i please get some help here.

I want to create a relationship between two contacts and have the relationship listed in the same related list on both contacts.

I created a relationship junction record using 2 lookup fields to the contact object. When I create the record it wont display the relation in the same related list on each object. In order to do that, upon creating the relationship record I need apex to duplicate the record and at the same time insert the same fields for the lookup but just switch them around, contact "A" and contact "B" should switch positions on the duplicate.

I tried writing some apex put im having trouble withe the creation of a new record, as opposed to an update on the current one.
trigger NewRelationships on Relationship__c (after insert) {

    List<relationship__c> NewRelationship = new List <relationship__c> ();

    for (relationship__c x : Trigger.new) {
        
        
           
        relationship__c NewRec = new relationship__c();
        NewRec.First_person_in_relationship__c = x.Second_person_in_relationship__c;
        NewRec.First_person_is_second_persons__c = x.Second_Person_is_first_persons__c;
        NewRec.Second_person_in_relationship__c = x.First_person_in_relationship__c;
        NewRec.Second_Person_is_first_persons__c = x.First_person_is_second_persons__c;
        
        newRelationship.add(NewRec);
        
   }
   INSERT NewRec;
   
}