Find your content:

Search form

You are here

Trigger does not always update record

 
Share

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();

    }
}

Attribution to: Merry Stambaugh

Possible Suggestion/Solution #1

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.


Attribution to: NSjonas

Possible Suggestion/Solution #2

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;
            }
        }
    }
}

Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34278

My Block Status

My Block Content