You need to sign in to do that
Don't have an account?
Eduardo Andrade
trigger test on creation of custom object
Hi, a have a simple trigger that creates a custom object depending on attachment name:
trigger CreateCustomObject on Attachment (before insert) { for(Attachment a : Trigger.New) { if(a.name.substring(0,6) == 'CO_SD-'){ CustomObject__c io = new CustomObject__c(opportunity__c = a.ParentId, attachName__c = a.Name); insert io; } } }
How can I test this trigger? I've tried this, but with no success:
@isTest public class TestCreateCustomObject { @isTest static void TestOne(){ Opportunity op = [select id from opportunity limit 1]; insert op; Attachment at = new Attachment(name = 'CO_SD-test.txt', parentId = op.Id); insert at; List<CustomObject__c> co_list = [select id from CustomObject__c order by createddate desc limit 1]; System.assertEquals(at.name, co_list[0].attachName__c); } }Thanks any idea.
See the trigger below, Test Class:
In test class you need to insert the sample opportunty first. Hope this helps & if ot solves the query then please mark it as solved / Best Answered!
Thanks!
All Answers
See the trigger below, Test Class:
In test class you need to insert the sample opportunty first. Hope this helps & if ot solves the query then please mark it as solved / Best Answered!
Thanks!
Thanks Govind Guru, the major problem was that select to instantiate op, (I learned that test class sees only test data), with few adjusts it worked
Blob bodyAtt = Blob.valueOf('body of attach.'); to create attachment, ... new Attachment(body = bodyAtt, ...
and
... [select id, attachName__c from CustomObject__c ... to use attachName__c on System.assertEquals
In addition, they won't count towards to your charactors limitations plus the records created inside test classes would not commit to the database.
Hope it helps!