• Chris Edwards
  • NEWBIE
  • 60 Points
  • Member since 2014


  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 1
    Likes Given
  • 2
    Questions
  • 8
    Replies
Hello developer heroes!

I'm working through the Apex modules on Trailhead and can't seem to get past this one: https://developer.salesforce.com/en/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk.

Hopefully this doesn't read like a 'please complete the course for me' kinda post, but I have written a trigger that I believe meets the criteria but it isn't passing the check, so I wanted to seek the guidance of the experts.

The challenge is to do this:

Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.

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.


And here is the trigger I have come up with, which compiles OK and stands up to a manual (though admittedly unbulkified) test:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List<Task>();
    
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new]){
                    
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                  WhatId = opp.Id));
       
    }

    if(taskList.size()>0){
        
        insert taskList;
        
    }
    
}
I have tried replacing the SOQL with a straightforward 'for (Opportunity opp : Trigger.new)' and having the taskList.add inside an IF that checks for Closed Won - no luck. I also thought about checking to see if the stage was being changed to Closed Won, rather than the trigger firing on every edit, but I don't think this is what the module is asking for.

Where do you think I'm going wrong?

Huge thanks in advance!
Hi all. My first question over here in the foreign climes of the dev boards - go easy on me, I am but a mere admin. :-)

I have written a trigger which stops an account being deleted if one of its field values is not null. This works as expected. I have written the test class, which is at 100% coverage but one of my asserts is failing. The asserts around the delete being blocked work fine - it is the negative case, where the field is null and so where the delete should go ahead, that is not working.

Here is my test class, with the delete and failing assert highlighted:
 
@IsTest
public class PreventAccountDeletionTest {

    private static testMethod void testDeleteSuccess()
    {
           Account acc=new Account(Name='Test Account');
           insert acc;
           
           System.assertNotEquals(acc.Id, null);
           System.assertEquals(acc.Some_Field__c, null);

           delete acc;
                 
           System.assertEquals(acc.IsDeleted, true);
        }

    private static testMethod void testDeleteFail()
    {
           Account acc=new Account(Name='Test Account', Some_Field__c='ABCDEFGHIJKLMNOP');
           insert acc;
  
           try
           {
              delete acc;
              // should throw an exception - the following assertion will cause an error if the code carries on
              System.assert(false);
           }
           catch (DMLException e)
           {
               // expected
               System.assert(e.getMessage().contains('You cannot delete an Account which is linked to the external system.'));
           }
        
        System.assertEquals(acc.IsDeleted, false);
        
    }

}

Any suggestions? Is there something I don't know about deleting in a test class? As the record is (hopefully) in the recycle bin do I need to use something like the equivalent of ALL ROWS? Or any other obvious thing that I'm missing?

Oh and let me know if you want to see the trigger code. Many thanks in advance!
Hello developer heroes!

I'm working through the Apex modules on Trailhead and can't seem to get past this one: https://developer.salesforce.com/en/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk.

Hopefully this doesn't read like a 'please complete the course for me' kinda post, but I have written a trigger that I believe meets the criteria but it isn't passing the check, so I wanted to seek the guidance of the experts.

The challenge is to do this:

Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.

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.


And here is the trigger I have come up with, which compiles OK and stands up to a manual (though admittedly unbulkified) test:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List<Task>();
    
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new]){
                    
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                  WhatId = opp.Id));
       
    }

    if(taskList.size()>0){
        
        insert taskList;
        
    }
    
}
I have tried replacing the SOQL with a straightforward 'for (Opportunity opp : Trigger.new)' and having the taskList.add inside an IF that checks for Closed Won - no luck. I also thought about checking to see if the stage was being changed to Closed Won, rather than the trigger firing on every edit, but I don't think this is what the module is asking for.

Where do you think I'm going wrong?

Huge thanks in advance!
Hello developer heroes!

I'm working through the Apex modules on Trailhead and can't seem to get past this one: https://developer.salesforce.com/en/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk.

Hopefully this doesn't read like a 'please complete the course for me' kinda post, but I have written a trigger that I believe meets the criteria but it isn't passing the check, so I wanted to seek the guidance of the experts.

The challenge is to do this:

Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.

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.


And here is the trigger I have come up with, which compiles OK and stands up to a manual (though admittedly unbulkified) test:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List<Task>();
    
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new]){
                    
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                  WhatId = opp.Id));
       
    }

    if(taskList.size()>0){
        
        insert taskList;
        
    }
    
}
I have tried replacing the SOQL with a straightforward 'for (Opportunity opp : Trigger.new)' and having the taskList.add inside an IF that checks for Closed Won - no luck. I also thought about checking to see if the stage was being changed to Closed Won, rather than the trigger firing on every edit, but I don't think this is what the module is asking for.

Where do you think I'm going wrong?

Huge thanks in advance!
Hi all. My first question over here in the foreign climes of the dev boards - go easy on me, I am but a mere admin. :-)

I have written a trigger which stops an account being deleted if one of its field values is not null. This works as expected. I have written the test class, which is at 100% coverage but one of my asserts is failing. The asserts around the delete being blocked work fine - it is the negative case, where the field is null and so where the delete should go ahead, that is not working.

Here is my test class, with the delete and failing assert highlighted:
 
@IsTest
public class PreventAccountDeletionTest {

    private static testMethod void testDeleteSuccess()
    {
           Account acc=new Account(Name='Test Account');
           insert acc;
           
           System.assertNotEquals(acc.Id, null);
           System.assertEquals(acc.Some_Field__c, null);

           delete acc;
                 
           System.assertEquals(acc.IsDeleted, true);
        }

    private static testMethod void testDeleteFail()
    {
           Account acc=new Account(Name='Test Account', Some_Field__c='ABCDEFGHIJKLMNOP');
           insert acc;
  
           try
           {
              delete acc;
              // should throw an exception - the following assertion will cause an error if the code carries on
              System.assert(false);
           }
           catch (DMLException e)
           {
               // expected
               System.assert(e.getMessage().contains('You cannot delete an Account which is linked to the external system.'));
           }
        
        System.assertEquals(acc.IsDeleted, false);
        
    }

}

Any suggestions? Is there something I don't know about deleting in a test class? As the record is (hopefully) in the recycle bin do I need to use something like the equivalent of ALL ROWS? Or any other obvious thing that I'm missing?

Oh and let me know if you want to see the trigger code. Many thanks in advance!
I have created a VF Page to set the Finish Location for my Flow.  However, when I attempt to use the button, I am receiving an error - URL No Longer Exists.  Any ideas?

<apex:page id="theTransferPage" standardcontroller="SCMC__Sales_Order__c">
    <apex:variable var="theReplacementTransfer" value="{!SCMC__Sales_Order__c.Id}"></apex:variable>
    <div style="float: left; width: 600px; padding: 10px;">
            <flow:interview name="ReplacementTransferRequest" finishLocation="{!URLFOR('/'+theReplacementTransfer)}" >
                    <apex:param name="varSalesOrderID" value="{!SCMC__Sales_Order__c.Id}"/>
                    <apex:param name="varOwnershipID" value="{!SCMC__Sales_Order__c.SCMC__Ownership__c}"/>
                    <apex:param name="varComments" value="{!SCMC__Sales_Order__c.SCMC__Sales_Representative_Instructions__c}"/>
            </flow:interview>
        </div>
</apex:page>