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
kitsunebravekitsunebrave 

How to Include a list of elements from a trigger

Hi,

I would like to pass a list of attributes from an SObject inside the after insert trigger.

Below is my code:
 
trigger Milestone_Tasks_Creation_Trigger on Milestone1_Milestone__c (after insert) {
    
    
    List<Milestone1_Task__c> tasks = new List<Milestone1_Task__c>();
    
    List<String> names = new List<String>{'Charter', 'Discovery', 'Impact Analysis', 'Design', 
    									  'Risk Assessment/Contigency Plan', 'User Acceptance Testing', 
    									  'Producition Approval', 'Survey'};
    
    for(Milestone1_Milestone__c milestone : Trigger.New){
    	//Milestone1_Milestone__c milestone = Trigger.New;
    	// Filling out Apporval Task:Impact, test, all rest
    	//for example:
    	
    	Milestone1_Task__c task = new Milestone1_Task__c(Milestone1_Milestone__c = milestone.ID);
    	task.Name = names;
    	task.Complete__c = false;
    	task.Priority__c = '0';
    
    	tasks.add(task);
    }
    insert tasks;
}

 
pconpcon
I'm not sure what you are asking here.  Can you please provide a little more information.  Are you trying to create a task for every single name in your list?  If so, you'll want to interate over all of them.
 
trigger Milestone_Tasks_Creation_Trigger on Milestone1_Milestone__c (after insert) {
    List<Milestone1_Task__c> tasks = new List<Milestone1_Task__c>();

    List<String> names = new List<String> {
        'Charter',
        'Discovery',
        'Impact Analysis',
        'Design',
        'Risk Assessment/Contigency Plan',
        'User Acceptance Testing',
        'Producition Approval',
        'Survey'
    };

    for (Milestone1_Milestone__c milestone : Trigger.New) {
        for (String name : names) {
            tasks.add(new Milestone1_Task__c(
                Milestone1_Milestone__c = milestone.Id,
                Name = name,
                Complete__c = false,
                Priority__c = '0'
            );
        }
    }

    if (!tasks.isEmpty()) {
        insert tasks;
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors
kitsunebravekitsunebrave
Hi, pcon,
Thanks for your quick answer.
The final answer is that I was reviewing Milestone PM open source project, to include after insert trigger a Milestone PM Object with all Tasks. The Object itself milestone cannot reproduce any tasks inside, so I already created it this functionality but from existing Id. The version of my code had been tested 64% CO using specific Id from another Object created but I need to build automatic process to run milestone with tasks automatically. See below details,  I included this Id, but I need this snippet capable to create without request an existing Id but the current Milestone Id after inserted.

Thanks pcon.
 
...
List<Milestone1_Task__c> task_temp = [Select Id, Name from Milestone1_Task__c Where Project_Milestone__c = 'a0i550000002dYy'];
    List<Milestone1_Task__c> tasks = new List<Milestone1_Task__c>();
...
pconpcon
I'm still not sure what you are tyring to do.  You can simply update that query to use a variable for the Id instead of hardcoding it.
kitsunebravekitsunebrave
This is an after insert trigger between Milestone and Task Object. I need to create a linked list of Task Object inside the Milestone Object If I delete that object Milestone from salesforce, the Id will delete as well. Then I need another better solution for my trigger I believe that I can retrieve an Id after insert a Milestone Object and reuse this milestone for the rest of my seudocode. It is possible or not using trigger. Thanks.