• Rob Nelson 8
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
trigger AssignByLeadSource on Lead (before insert) {
    // Use row locks to prevent race conditions
    {
        LeadSourceCounters__c[] temp = [SELECT Id FROM LeadSourceCounters__c FOR UPDATE];
        //LeadSourceCounters is a custom setting
    }
    
    Map<String, LeadSourceCounters__c> counters = LeadSourceCounters__c.getAll();
    //counters is a new map that pulls in all the counts from the custom setting.
    
    System.debug('LeadSourceCounters__c.getAll() = ' + LeadSourceCounters__c.getAll());
    System.debug(counters);
    
    for(Lead record: Trigger.new) {
        if(record.LeadSource==null) {
            //if LeadSource is null, we can't round robin it specially, so continue ends for loop.
            continue;
        }
        
        if(!counters.containsKey(record.LeadSource)) {
        //if LeadSource doesn't exist, adds it and sets value to zero.    
        
            LeadSourceCounters__c happydog = new LeadSourceCounters__c(
                Name=record.LeadSource, 
                Value__c=0
            );
                   
            counters.put(record.LeadSource, happydog);
        }
        
        counters.get(record.LeadSource).Value__c+=1;
        //add 1 to Value__c counter for that lead source
        record.AssignmentId__c = counters.get(record.LeadSource).Value__c;
    }
    upsert counters.values();
    //saves the updated counter for that lead source
}

Everything works great except the .put command. 
counters.put(record.LeadSource, happydog);

On that line it throws the error:

AssignByLeadSource: execution of BeforeInsert caused by: System.FinalException: Collection is read-only: External entry point

This might be a rookie question but how do I add an item to the map? All the examples I have seen use .put
 
trigger AssignByLeadSource on Lead (before insert) {
    // Use row locks to prevent race conditions
    {
        LeadSourceCounters__c[] temp = [SELECT Id FROM LeadSourceCounters__c FOR UPDATE];
        //LeadSourceCounters is a custom setting
    }
    
    Map<String, LeadSourceCounters__c> counters = LeadSourceCounters__c.getAll();
    //counters is a new map that pulls in all the counts from the custom setting.
    
    System.debug('LeadSourceCounters__c.getAll() = ' + LeadSourceCounters__c.getAll());
    System.debug(counters);
    
    for(Lead record: Trigger.new) {
        if(record.LeadSource==null) {
            //if LeadSource is null, we can't round robin it specially, so continue ends for loop.
            continue;
        }
        
        if(!counters.containsKey(record.LeadSource)) {
        //if LeadSource doesn't exist, adds it and sets value to zero.    
        
            LeadSourceCounters__c happydog = new LeadSourceCounters__c(
                Name=record.LeadSource, 
                Value__c=0
            );
                   
            counters.put(record.LeadSource, happydog);
        }
        
        counters.get(record.LeadSource).Value__c+=1;
        //add 1 to Value__c counter for that lead source
        record.AssignmentId__c = counters.get(record.LeadSource).Value__c;
    }
    upsert counters.values();
    //saves the updated counter for that lead source
}

Everything works great except the .put command. 
counters.put(record.LeadSource, happydog);

On that line it throws the error:

AssignByLeadSource: execution of BeforeInsert caused by: System.FinalException: Collection is read-only: External entry point

This might be a rookie question but how do I add an item to the map? All the examples I have seen use .put