Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I created this trigger to update a lookup on the record. It works fine if I edit the record manually, but not when doing a mass insert or update. I am not sure why... any help would be appreciated. trigger SDRCallstoHours on SDR_Calls__c (before insert, before update) {

    Map<Id, List<SDR_Calls__c>> calls = new Map<Id, List<SDR_Calls__c>>();
    for (SDR_Calls__c c : Trigger.new ) { 
        List<SDR_Calls__c> l = calls.get(c.Date_of_Call__c);
        if (l == null) {
            calls.put(c.Date_of_Call__c, l);
        }
        **l.add(c);**
    }

    for (SDR_Hours__c Hour : [
            SELECT Name, Id, Date_Worked__c, Extension__c
            FROM SDR_Hours__c
            WHERE Date_Worked__c IN :calls.keySet()
            ]) { 
        for (SDR_Calls__c s : calls.get( Hour.Date_Worked__c)) {
            if(s.SDR_Extension__c == Hour.Extension__c) {
                s.SDR_Hours_Record_Name__c = Hour.Id;
            }
        }
    }
}

Here is the test class: It is a test that covers a couple of triggers

@isTest
private class TEST_SDRHotTrackingOpp {

    static testMethod void myHotTest() {



       User u = new User(Alias = 'mjar', Email='mjar@testorg.com', 
       EmailEncodingKey='UTF-8', LastName='Jar', LanguageLocaleKey='en_US', 
       LocaleSidKey='en_US',  FirstName= 'M',
       TimeZoneSidKey='America/Los_Angeles', UserName='mjar@testorg.com');

        SDR_Date__c dt= new SDR_Date__c (
        Name = '04/01/2014'
        );
        insert dt;  

        Account acc = new Account (Name = 'ApexTest2014', 
        Type = 'Customer');
        insert acc;

        SDR_Extension__c ext = new SDR_Extension__c (
        Name = '9999',
        Qualifier_Lead_Gen_Name__c = 'M Jar',
        SDR_Name__c = u.Id
        );
        insert ext;  

        SDR_Time_Conversion__c tme = new SDR_Time_Conversion__c (
        Name = '00:15:00',
        Decimal__c = 0.25
        );
        insert tme;    

        SDR_Hours__c hr = new SDR_Hours__c (
        Extension__c = ext.Id,
        Date_Worked__c = dt.Id,
        Hours_Worked__c = 8

        );
        insert hr;

        SDR_Calls__c cl = new SDR_Calls__c (
        SDR_Extension__c = ext.Id,
        Date_of_Call__c = dt.Id,
        Time_on_Call__c = tme.Id

        );
        **insert cl;**


        Opportunity opp = new Opportunity(
            Name = 'Apex Test Opp1',
            AccountId = acc.Id,
            CloseDate = date.today(), 
            StageName = 'Hot',
            Likelihood_of_hosting__c = 'Possible',
            State__c = 'UT',
            Type = 'New business - Non-Software',
            Verticle__c = 'ISO AP',
            Leadsource = 'cold call',
            Tepid_to_Warm_Date__c = date.today(),
            SDR_Hours__c = hr.Id,
            SDR_Date__c = dt.Id
        );
        insert opp;

            Test.startTest();  

             opp.Qualifier__c = 'M Jar';
             opp.Date_Qualified__c = date.today();
             update opp;



            Test.stopTest();

    }
}
share|improve this question

2 Answers 2

up vote 6 down vote accepted

One problem your trigger probably has is that multiple SDR_Calls__c objects may be referring to the same date (assuming Date_of_Call__c is the ID of some date object). Your map will only keep hold of the last SDR_Calls__c object added.

This code uses lists to hold multiple SDR_Calls__c objects and updates all the items in the lists:

trigger SDRCallstoHours on SDR_Calls__c (before insert, before update) {

    Map<Id, List<SDR_Calls__c>> calls = new Map<Id, List<SDR_Calls__c>>();
    for (SDR_Calls__c c : Trigger.new ) { 
        List<SDR_Calls__c> l = calls.get(c.Date_of_Call__c);
        if (l == null) {
            l = new List<SDR_Calls__c>();
            calls.put(c.Date_of_Call__c, l);
        }
        l.add(c);
    }

    for (SDR_Hours__c Hour : [
            SELECT Name, Id, Date_Worked__c, Extension__c
            FROM SDR_Hours__c
            WHERE Date_Worked__c IN :calls.keySet()
            ]) { 
        for (SDR_Calls__c s : calls.get( Hour.Date_Worked__c)) {
            if(s.SDR_Extension__c == Hour.Extension__c) {
                s.SDR_Hours_Record_Name__c = Hour.Id;
            }
        }
    }
}
share|improve this answer
    
That did it. Stupid thing to miss. –  Merry May 2 at 18:20
    
I am now using the trigger the way you wrote it - Now my test class fails (added to my question highlighted the fail points) - I get this error.. System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SDRCallstoHours: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.SDRCallstoHours: line 9, column 1: [] Class.TEST_SDRHotTrackingOpp.myHotTest: line 49, column 1 –  Merry May 2 at 19:29
1  
@Merry Goof on my part; forgot to create the list; I've added that to the code in the answer... –  Keith C May 2 at 19:45
    
Perfect. Worked! –  Merry May 2 at 19:56

I don't think you need this line: update callsToUpdate;. Modification are implicitly committed to the DB when the record is inserted on Before Insert & Before Update triggers.

What are you using to mass update. Here is a list of operations that do not invoke triggers.

share|improve this answer
    
I am using the dataloader.io. When I inserted new records it fired on a handful of them but not all. And nothing happens when I update the record... –  Merry May 2 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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