0

I have a trigger with helper class and test. But I don't know how to test update there and what kind of notification I need. Thanks for any help.

Trigger:

trigger ArchivedTrigger on Item__c (after update) {
JTriggerHelper triggerHelper = new TriggerHelper();

if(Trigger.isUpdate){ 
    for(Item__c item : Trigger.new){
        if(item.Status__c == 'Old'){
            triggerHelper.remove(item);
        }
    }
}
}

Helper:

public void remove(Item__c item){
    List<Tool__c> toolsList  = [SELECT Id, CustomIDs__c FROM Tool__c];

    for(Tool__c tool : toolsList){
        if(tool.CustomIDs__c != null && tool.CustomIDs__c.contains(item.CustomIDs__c)){
            List<String> positionIdsList = String.valueOf(tool.CustomIDs__c).split(' ');

            for(Integer i=0; i<positionIdsList.size(); i++){
                if(positionIdsList[i] == item.CustomIDs__c){
                    positionIdsList.remove(i);   
                } 
            }
            tool.CustomIDs__c = String.join(positionIdsList,' ');

            try{
                update tool;
            //How to test an exception here?
            } catch(Exception e){
                //What is better to do here? Email? Logs for developer?
            }
        }
    }
}

Test:

@isTest
static void testUpdateTrigger() {
    TriggerHelper triggerHelper = new TriggerHelper();

    Tool__c tool = new Tool__c(Email__c = 'test@test.com', CustomIDs__c = 'text 999999');
    insert tool;
    Item__c testTool = new Item__c(CustomIDs__c = '999999');
    insert testTool;

    testTool.Status__c = 'Old';
    update testTool;

    Test.startTest();
        triggerHelper.removePositionId(testTool);
    Test.stopTest();

    Tool__c toolTest = [SELECT CustomIDs__c FROM Tool__c WHERE Email__c = 'test@test.com'];
    System.assertEquals('text', toolTest.CustomIDs__c);
}
  • 1
    I think that you have several things in your code that are not very going to work well. For loops inside for loops is not a good practice. DML operations inside loops, issues as well. Problems as reaching governor limits quite fast. Can you please elaborate a bit more on the requirements so we can help you with it? I think you should get your trigger working properly before jumping to the test class. You could also start writing your test class first , but the is an other approach.Tell us what it is that you want to accomplish and the data model involved in this business case. – Carlos Naranjo Dec 7 '18 at 9:19
1

First thing you don't need to do this in after update, as you are just populating tool.CustomIDs__c field. You can do the same in before update trigger and no need to update records explicitly.

Second, you are trying to update single record at a time in a for loop which is not recommended, every DML must be bulkified, I would suggest you to learn in detail about apex triggers here

after modification your code be like

Trigger

trigger ArchivedTrigger on Item__c (before update) {
    JTriggerHelper triggerHelper = new TriggerHelper();

    if(Trigger.isUpdate){ 
        for(Item__c item : Trigger.new){
            if(item.Status__c == 'Old'){
                triggerHelper.remove(item);
            }
        }
    }
}

Helper:

public void remove(Item__c item){
    List<Tool__c> toolsList  = [SELECT Id, CustomIDs__c FROM Tool__c];

    for(Tool__c tool : toolsList){
        if(tool.CustomIDs__c != null && tool.CustomIDs__c.contains(item.CustomIDs__c)){
            List<String> positionIdsList = String.valueOf(tool.CustomIDs__c).split(' ');

            for(Integer i=0; i<positionIdsList.size(); i++){
                if(positionIdsList[i] == item.CustomIDs__c){
                    positionIdsList.remove(i);   
                } 
            }
            tool.CustomIDs__c = String.join(positionIdsList,' ');

            /* no need to update as in before trigger any changes made in trigger context records will be saved in database
            */
        }
    }
}
| improve this answer | |

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.