1

I have a trigger on custom object object called File_Uploaded__c which has an attribute : IsMain__c of type checkbox, and the trigger maintains only one file as main :

trigger setIsMain on File_Uploaded__c (before update) {

// this trigger will only be executed if we change IsMain__c field from false to true
list<File_Uploaded__c> filesToChange = new list<File_Uploaded__c>();
for(File_Uploaded__c f: Trigger.new) {
    if(f.IsMain__c != Trigger.oldMap.get(f.Id).IsMain__c && f.IsMain__c == true) {    
       filesToChange.add(f);
    }
}

if(filesToChange.size() > 0) {
    File_Uploaded__c fileToUpdate = filesToChange.get(0);
    List<File_Uploaded__c> oldMainFile = [select Id, IsMain__c from File_Uploaded__c where IsMain__c = true and Case__c = :fileToUpdate.Case__c and Id != :fileToUpdate.Id];
    if (oldMainfile.size() > 0) {
        oldMainfile.get(0).IsMain__c = false;
        update oldMainfile.get(0);
    }        

    // get the corresponding case and appliance of the new main file 
    Case caseFile = [select  EligibleSupport__c, Product__c, Software_version__c, CustomerProject__c from Case where Id = :fileToUpdate.Case__c];  
    caseFile.Product__c = fileToUpdate.Product__c;
    caseFile.Software_version__c = fileToUpdate.Version__c;

    if (fileToUpdate.Asset2__c != null ) {
         Asset2__c asset = [select Support_Contract_Level__c, Support_Contract_End_Date__c, Opportunity__r.Project__c from Asset2__c where Id = :fileToUpdate.Asset2__c]; 
         caseFile.EligibleSupport__c = asset.Support_Contract_End_Date__c < system.now() ? 'Expired' : asset.Support_Contract_Level__c;
         caseFile.CustomerProject__c = asset.Opportunity__r.Project__c;   
    }else {
        caseFile.EligibleSupport__c = '';
         caseFile.CustomerProject__c = null; 
    }       

    // update the corresponding case of the new main file                        
    update caseFile;
}  

and when i write a test class:

@isTest
public class TestFileIsMain {
static testmethod void testIsMain() {

    File_Uploaded__c f = new File_Uploaded__c();
    f.FileName__c = 'testTrigger';
    f.Case__c = '500f0000005BBQPAA4';
    f.IsMain__c = false;
    insert f;

    f.IsMain__c = true;
    update f;

    List<File_Uploaded__c> allMainFiles = [select Id from File_Uploaded__c where Case__c =:f.Case__c and IsMain__c = true];

    System.assertEquals(1, allMainFiles.size());
}

}

i got several errors :

System.DmlException: Update failed. First exception on row 0 with id a2tf0000000Dx8OAAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, setIsMain: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.setIsMain: line 20, column 1: []

0

what your trigger does is set one main as File per case so here's an example for the test class:

@isTest
   public class TestFileIsMain {
       static testMethod void testIsMain() {
           Case c = new Case(Name = 'Test Name', your_fields_here = '...');
           insert c;

           File_Uploaded__c f1 = new File_Uploaded__c(Name = 'test1', IsMain = true, Case__c = c.Id);
           insert f1;

           File_Uploaded__c f2 = new File_Uploaded__c(Name = 'test2', IsMain = false, Case__c = c.Id);
           insert f2;  
           File_Uploaded__c f3 = new File_Uploaded__c(Name = 'test3', IsMain = false,  Case__c = c.Id);        
           insert f3; 

           f2.IsMain__c = true;                                      

           test.startTest();       
           update f2;
           test.stopTest();

           List<File_Uploaded__c> allMainFiles = [select Id from File_Uploaded__c where IsMain__c = true and Case__c = :c.Id];

           System.assertEquals(1, allMainFiles.size());
       }
   }

you should first create a Case (the Id is generated automatically) and insert it and then assign it to Case__c field, and the test part should be between test.startTest() and test.stopTest() because the trigger is fired before update, so then you can then query what you want from your object.

| improve this answer | |
  • you are welcome, and good luck. – rainman Jun 4 '15 at 13:47
0

Your TestClass has no access to the records on your data. You need to create a Case in the testclass and use the ID of that. Something like:

Case c = new Case(Name = 'Test Name', your_required_fields = 'test'); // Fill in whatever fields are required
insert c;
// init your filetoupload
f.Case__c = c.Id;

You don't give Case c an id, as the system does this on insert.

| improve this answer | |
  • you mean like this: Case c = new Case(Name = 'Test Name', Id = 'myId''); and then i asign it to Case__c ?? But the probem is that Case__c it's just the id of the Case and then i need to check if there's effectively 1 main file or 2 because that's what the trigger does – ihssan Jun 4 '15 at 11:53
  • No, not like this. I will make an edit, maybe you will see it then. – Frederik Witte Jun 4 '15 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.