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
LloydSLloydS 

Creating a custom object record based upon opportunity trigger

Okay I'm graduating from using triggers for simple field updates to creating a new record of a custom object. I need help getting the test class to work and the trigger as well.

 

When an opportunity is won (designated as the status being Closed Won), I want to create a new Checklist (custom object) with the required fields of the name being static text, a template ID being designated, the account referenced via the Entity lookup field, and a deadline set 10 days from today.

 

Here's the trigger so far.

 

trigger OpportunityChecklist on Opportunity (after update) { if (Trigger.new.size() == 1) { if (Trigger.old[0].StageName != 'Closed Won' && Trigger.new[0].StageName == 'Closed Won') { L5CL__Checklist__c obj = new L5CL__Checklist__c(); obj.Name = 'New Agency Orientation'; obj.L5CL__Checklist_Template__c = 'a18A0000000GvAG'; obj.L5CL__Entity__c = Trigger.new[0].AccountId; date now = date.today(); obj.L5CL__Deadline__c = now.addDays(10); insert obj; } } }

 

As for the test class, I'm not exactly sure how the system.assertequals function should be written to reference the newly created custom object record.

 

 

@isTest private class OpportunityWonChecklistTest { static testMethod void myUnitTest() { Test.startTest(); Opportunity o = new Opportunity(); o.name='NA'; o.RecordTypeId='012A0000000L9p0'; o.Type = 'Deferred Annuity'; o.StageName = 'Engage'; o.CloseDate = date.today(); insert o; o.StageName = 'Closed Won'; update o; L5CL__Checklist__c l_new = ???????; system.assertequals(l_new.Name != null, l_new.Name); Test.stopTest(); } }

 

 

Thanks for the help. This is starting to make sense.

Rajesh ShahRajesh Shah
Well before you update Opportunity, there would be no Checklist record for that opportunity. After the update statement, it should have 1 checklist record. So you can query before and after and in assert check the size of the result retured.
LloydSLloydS
The Checklist object has no connection with the Opportunity object. The relationship is with the account (Entity). But I want the new checklist object to be triggered via a won Opportunity.
LloydSLloydS
In addition to still trying to figure out how to write the test class, there's one other issue that just popped up. The installation of the application of which the custom object is part of requires that the "New" button for the custom object be overridden with an S-Control. So I can't just add a new object in my trigger, I need to do it and call the S-Control or in some other way make sure that the S-Control is used to create the object.