+ Start a Discussion
Eduardo AndradeEduardo 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.
Best Answer chosen by Eduardo Andrade
HARSHIL U PARIKHHARSHIL U PARIKH
I would say you if write Insert Io; inside the FOR loop then this will fail right after the first 150 records insert since there are only 150 DML statements allowed per transaction. But however, you can define a list of CustomObject__c then add all the records and then put DML on the list.
See the trigger below,
trigger CreateCustomObject on Attachment (After insert) {

    List<CustomObject__c > custObjListToInsert = New List<CustomObject__c >();

    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);
            custObjListToInsert.add(io);
        }
    }
    
    try{
        If(!custObjListToInsert.IsEmpty() ){
            insert custObjListToInsert;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception for CreateCustomObject Trigger Is:: ' + e.getMessage());
    }
}
Test Class:
In test class you need to insert the sample opportunty first.
@isTest
public class TestCreateCustomObject {
    @isTest static void TestOne(){
    
        //Opportunity op = [select id from opportunity limit 1];
        Opportunity op = New Opportunity();
        op.Name = 'Sample Opp';
        op.ClosedDate = System.Today();
        op.Stage = 'Closed Won';
        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);
        
    }
}
Hope this helps & if ot solves the query then please mark it as solved / Best Answered!
Thanks!


 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
I would say you if write Insert Io; inside the FOR loop then this will fail right after the first 150 records insert since there are only 150 DML statements allowed per transaction. But however, you can define a list of CustomObject__c then add all the records and then put DML on the list.
See the trigger below,
trigger CreateCustomObject on Attachment (After insert) {

    List<CustomObject__c > custObjListToInsert = New List<CustomObject__c >();

    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);
            custObjListToInsert.add(io);
        }
    }
    
    try{
        If(!custObjListToInsert.IsEmpty() ){
            insert custObjListToInsert;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception for CreateCustomObject Trigger Is:: ' + e.getMessage());
    }
}
Test Class:
In test class you need to insert the sample opportunty first.
@isTest
public class TestCreateCustomObject {
    @isTest static void TestOne(){
    
        //Opportunity op = [select id from opportunity limit 1];
        Opportunity op = New Opportunity();
        op.Name = 'Sample Opp';
        op.ClosedDate = System.Today();
        op.Stage = 'Closed Won';
        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);
        
    }
}
Hope this helps & if ot solves the query then please mark it as solved / Best Answered!
Thanks!


 
This was selected as the best answer
Eduardo AndradeEduardo Andrade

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

HARSHIL U PARIKHHARSHIL U PARIKH
Yes! That is true, anything that is under @IsTest is test data and you create a data inside the test class.
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!