+ Start a Discussion
NiknitNiknit 

Stuck in Trailhead challenge for bulk trigger,Help please

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

		list<task> tasklist = new list<task>();
    	list<opportunity> opplist = new list<opportunity>();
    
    	opplist = [select id,stagename from opportunity where stagename = 'Closed Won' and id in :trigger.newmap.keyset()];

    		for (opportunity opp : opplist){
            	if(opp.stagename == 'Closed Won'){
						task addtask = new task();
            			addtask.subject = 'Follow Up Test Task';
						addtask.WhatId = opp.Id;
						tasklist.add(addtask);
				}
		}
		update tasklist;
}


Hi,

The challenge is 

To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.

  • The Apex trigger must be called 'ClosedOpportunityTrigger'
  • With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
  • To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
  • This challenge specifically tests 200 records in one operation.

 

I feel the logic is right, but it is showing me error as : 

Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account name is required: []

 

Is there something i am doing wrong, please point out if i made any mistakes.

also. i have just started with salesforce and apex 2 weeks ago, if i am missing anythin please give explaination, it would help me alot in understanding.

 

Thanks,

Nitin 

Best Answer chosen by Niknit
jigarshahjigarshah
@Nitin Sekhar - These are the following mistakes that you need to rectify within your code.

1. The error is because you have a custom validation rule created for Opportunity on that org which mandates having an AccountId associated with an Opportunity while creating the Opportunity record. Since you do not have any Account recordId associated with an Opportunity the vaidation rule restricts you from creating the record. You can deactivate the validation rule and the trigger code should work.

I believe you must have been using this org for some other RnD however, it is recommended to have a fresh Dev org or a new Trailhead Playground created while completing the challenges.

2. In order to create Tasks you need to use the insert or Database.insert statements and not the update statement. Modify line # 16 in your code as follows
 
trigger ClosedOpportunityTrigger on Opportunity (after update, after insert) {

		if(Trigger.isAfter)
		{
		   if(Trigger.IsUpdate, Trigger.IsInsert){
		   
				list<Task> tasklist = new list<Task>();
				list<Opportunity> opplist = new list<Opportunity>();
			
				opplist = [select id,stagename 
						   from Opportunity 
						   where stagename = 'Closed Won' 
						   and id in :trigger.newmap.keyset()];

					for (opportunity opp : opplist){
					
						tasklist.add(
							new Task(
								Subject = 'Follow Up Test Task', 
								WhatId = opp.Id));
					}
				}
				
				if(!tasklist.isEmpty())
					insert tasklist;
		   }
		}//if After
}
Please mark this as the best answer and upvote it if it helps you resovle your issue.
 

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7

Please try below trigger:

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

    List<Task> taskList = new List<Task>();
    
    for(Opportunity opp : Trigger.new) {
		
		//Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
		if(Trigger.isInsert) {
			if(Opp.StageName == 'Closed Won') {
				taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
			}
		}
		
		//Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
		if(Trigger.isUpdate) {
			if(Opp.StageName == 'Closed Won' 
			&& Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
				taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
			}
		}       
    }

    if(taskList.size()>0) {        
        insert taskList;        
    }    
}
v varaprasadv varaprasad
Hi Nitin,

Just change trigger name : ClosedOpportunityTrigger

Thanks
Varaprasad
NiknitNiknit

@ Varaprasad Sorry , its a typo in the above section, in the code it is actually the right spelling for the trigger.

its not working

@ Deepak i tried your code too but not working.its showing the same error.

jigarshahjigarshah
@Nitin Sekhar - These are the following mistakes that you need to rectify within your code.

1. The error is because you have a custom validation rule created for Opportunity on that org which mandates having an AccountId associated with an Opportunity while creating the Opportunity record. Since you do not have any Account recordId associated with an Opportunity the vaidation rule restricts you from creating the record. You can deactivate the validation rule and the trigger code should work.

I believe you must have been using this org for some other RnD however, it is recommended to have a fresh Dev org or a new Trailhead Playground created while completing the challenges.

2. In order to create Tasks you need to use the insert or Database.insert statements and not the update statement. Modify line # 16 in your code as follows
 
trigger ClosedOpportunityTrigger on Opportunity (after update, after insert) {

		if(Trigger.isAfter)
		{
		   if(Trigger.IsUpdate, Trigger.IsInsert){
		   
				list<Task> tasklist = new list<Task>();
				list<Opportunity> opplist = new list<Opportunity>();
			
				opplist = [select id,stagename 
						   from Opportunity 
						   where stagename = 'Closed Won' 
						   and id in :trigger.newmap.keyset()];

					for (opportunity opp : opplist){
					
						tasklist.add(
							new Task(
								Subject = 'Follow Up Test Task', 
								WhatId = opp.Id));
					}
				}
				
				if(!tasklist.isEmpty())
					insert tasklist;
		   }
		}//if After
}
Please mark this as the best answer and upvote it if it helps you resovle your issue.
 
This was selected as the best answer