• Michael Haase
  • NEWBIE
  • 19 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hello all,

I get a nullpointer exeption when writing an apex class for my trigger. The apex class and the trigger use Custom Settings.I the rror: "System.NullPointerException: Attempt to de-reference a null object". I guess the reason is that the Custom Setting object cannot be correctly allocated due to the namespace prefix (M2809) I have registered.
Does anybody have an idea how to put that correctly in the code or how to solve the problem?

Here is the apex class. I get the error in line 7 column 0.
1 @isTest
2  public class TaskTriggerTest {
3    static testMethod void testLastResearchActivity_v2(){
4     
5       //Get custom settings
6        Research_Mailer__c settings = Research_Mailer__c.getInstance('RMS');
7        String RSM_Task_Record_Type_ID = settings.m2809__Task_Record_Type_ID__c;
8        String RSM_ChuykoId = settings.Chuyko_Owner_ID__c;
9       String RSM_GoncharovId = settings.Goncharov_Owner_ID__c;
10     String RSM_ProfileId = settings.Profile_ID__c;
        
        //Create 3 users and insert them
        List<User> userToInsert = new List<User>();
        //User 1
        User u1 = new user();
        u1.FirstName = 'David';
        u1.LastName  = 'Owner 1';
     ...
Dear colleagues,

could you please help? I am new to Apex and just struggeling with the "System.LimitException: Too many Email Invocations: 11" error.

I am trying to send an email to the Contact owner upon creation of a task for the contact. That works so far fine, except that I am breaking the governor limt for the number of emails to be sent.

This is my trigger:

trigger Individual_SM_Notification_v2 on Task (after insert) {

//Query for Contact    
//Step 1: Create a Set of all Contacts to query
Set<String> allWhoIds = new Set<String>();
for (Task newTask : Trigger.new){
if (newTask.WhoId != null){
allWhoIds.add(newTask.WhoId);
    system.debug('Step 1: ' + newTask.WhoId);
}
}

//Step 2:Query the Contact Ids from the WhoIds (from Step 1)
List <Contact> potentialContacts = [Select Id, Name, Owner.Email, Owner.LastName From Contact Where Id In :allWhoIds];
system.debug('List<Contact> Details: ' + potentialContacts);
   
    
    
//Step 3: Create Map to search Contacts by WhoId
Map<String, String>WhoIdToContactMap = new map<String, String>();
for (Contact c : potentialContacts){
WhoIdToContactMap.put(c.Id, c.Owner.Email);
}

// Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
//Get the matching Contact Owner by the Contact Id
for (task newTask : Trigger.new){
String UserToUpdate = WhoIdToContactMap.get(newTask.WhoId);
System.debug('User to update: ' + UserToUpdate);
      

    
    
    
    
// Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
    
// Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(UserToUpdate);
      mail.setToAddresses(sendTo); 
    System.debug('Step 2:' + sendTo);
    
    
// Step 3: Set who the email is sent from
      mail.setReplyTo('SalesforceAdmin@bcsprime.com');
      mail.setSenderDisplayName('BCS GMIB Research Team'); 
    system.debug('Step 3:' + mail);

// (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('mhaase@bcsprime.com');
      mail.setCcAddresses(ccTo);    
    
 
// Step 4. Set email contents - you can use variables!
mail.setSubject('Research Call Update');
String body = 'Date: ' + newTask.ActivityDate + '<br/>';
body += 'Caller: ' + newTask.Assigned_To_Dupe__c + '<br/>';
body += 'Company Name: ' + newTask.Account_Name_dupe__c + '<br/>';
body += 'Contact Name: ' + newTask.Client_Name__c  + '<br/>';
body += 'Description: ' + newTask.Description + '<br/>';
mail.setHtmlBody(body);

// Step 5. Add your email to the master list
mails.add(mail);    


// Step 6: Send all emails in the master list
Messaging.sendEmail(mails);
          
}
}




This is my test class:

@isTest
public class TestLastResearchActivity_v2 {
    static testMethod void testLastResearchActivity_v2(){
        
        
        Account acc = new Account();
        acc.Name = 'ABC Investments';
        acc.RecordTypeId = '012b0000000UQ0L';
        insert acc;
        
        Account acctId = [Select Id From Account Where Name = 'ABC Investments'];
        
        
        Contact c = new Contact();
        c.LastName = 'Meier';
        c.RecordTypeId = '012b0000000USN6';
        insert c;
        
        //Lookup Contact Id
        Contact taskContact = [Select ID From Contact Where LastName = 'Meier'];
        
        
        //Create a list for the tasks to be updated
        List<Task> taskList = new List<Task>();
        
        //Tasks Chuyko
        Task t = new Task();
        t.WhatId = acctId.Id;
        t.RecordTypeId = '012b0000000URMt';
        t.OwnerId = '005b0000001R6YS';
        t.Communication_Type__c = 'Conversation';
        t.Whoid = taskContact.Id;
        insert t;
        
        
        //Tasks Petropavlovski
        Task t2 = new Task();
        t2.WhatId = acctId.Id;
        t2.RecordTypeId = '012b0000000URMt';
        t2.OwnerId = '005b0000001TATI';
        t2.Communication_Type__c = 'Conversation';
        t2.Whoid = taskContact.Id;
        insert t2;
        
        //Tasks Tikhomirov
        Task t3 = new Task();
        t3.WhatId = acctId.Id;
        t3.RecordTypeId = '012b0000000URMt';
        t3.OwnerId = '005b0000001TAgR';
        t3.Communication_Type__c = 'Conversation';
        t3.Whoid = taskContact.Id;
        insert t3;
        
        //Tasks Ibragimov
        Task t4 = new Task();
        t4.WhatId = acctId.Id;
        t4.RecordTypeId = '012b0000000URMt';
        t4.OwnerId = '005b0000001RKOq';
        t4.Communication_Type__c = 'Conversation';
        t4.Whoid = taskContact.Id;
        insert t4;
        
        //Tasks Naydennova
        Task t5 = new Task();
        t5.WhatId = acctId.Id;
        t5.RecordTypeId = '012b0000000URMt';
        t5.OwnerId = '005b0000001RKOl';
        t5.Communication_Type__c = 'Conversation';
        t5.Whoid = taskContact.Id;
        insert t5;
        
        //Tasks Goncharov
        Task t6 = new Task();
        t6.WhatId = acctId.Id;
        t6.RecordTypeId = '012b0000000URMt';
        t6.OwnerId = '005b0000001RKOg';
        t6.Communication_Type__c = 'Conversation';
        t6.Whoid = taskContact.Id;
        insert t6;
        
        //Tasks Tachennikov
        Task t7 = new Task();
        t7.WhatId = acctId.Id;
        t7.RecordTypeId = '012b0000000URMt';
        t7.OwnerId = '005b0000001TAUk';
        t7.Communication_Type__c = 'Conversation';
        t7.Whoid = taskContact.Id;
        insert t7;
        
        //Tasks Mitchell
        Task t8 = new Task();
        t8.WhatId = acctId.Id;
        t8.RecordTypeId = '012b0000000URMt';
        t8.OwnerId = '005b0000001RKOv';
        t8.Communication_Type__c = 'Conversation';
        t8.Whoid = taskContact.Id;
        insert t8;
        
        //Tasks Kotok
        Task t9 = new Task();
        t9.WhatId = acctId.Id;
        t9.RecordTypeId = '012b0000000URMt';
        t9.OwnerId = '005b0000001TAb7';
        t9.Communication_Type__c = 'Conversation';
        t9.Whoid = taskContact.Id;
        insert t9;
        }
}

Thanks in advance for your help!!!
Hi everyone. I am new to APEX and need some help.

I need to update the date field "Last_Reseach_Call__c) on the contact record always when a new task with record type "Log Call" was created. This field update cannot be done via workflow, therefore I am trying to solve the problem with an Apex trigger.

Here is the code I have so far:

trigger UpdateContactLastActivity on Task (after insert, after update)
 {

// The set used to hold the Ids of the Contacts that need to be updated
Set <Id> ContactIds = new Set <Id> ();

// The actual Contacts to save
List <Contact> ContactsToUpdate = new List <Contact> ();

// Now we go through and if we find Tasks that are marked as Research Call, then update the Contact with "Last Research Contact" date
for (Task t : Trigger.new)
{
if (t.RecordType)
{
ContactIds.add (t.Contact);
}
}

// Now have the list of Contacts, fetch the associated Contact records and updatethem
if (!ContactIds.isEmpty () )
{
for (Contact c : [select Id, LastName from Contact where Id in :ContactIds])
{
contact.Last_Research_Contact__c = Today();
ContactsToUpdate.add (c);
}

update ContactsToUpdate;
}
}


I currently cannot save the code as I get an error ЭCompile Error: Invalid field Contact for SObject Task at line 15 column 17".

Could please someone advise where the problem is and how the correct code should look like?

Thanks an advance!
Hello all,

I get a nullpointer exeption when writing an apex class for my trigger. The apex class and the trigger use Custom Settings.I the rror: "System.NullPointerException: Attempt to de-reference a null object". I guess the reason is that the Custom Setting object cannot be correctly allocated due to the namespace prefix (M2809) I have registered.
Does anybody have an idea how to put that correctly in the code or how to solve the problem?

Here is the apex class. I get the error in line 7 column 0.
1 @isTest
2  public class TaskTriggerTest {
3    static testMethod void testLastResearchActivity_v2(){
4     
5       //Get custom settings
6        Research_Mailer__c settings = Research_Mailer__c.getInstance('RMS');
7        String RSM_Task_Record_Type_ID = settings.m2809__Task_Record_Type_ID__c;
8        String RSM_ChuykoId = settings.Chuyko_Owner_ID__c;
9       String RSM_GoncharovId = settings.Goncharov_Owner_ID__c;
10     String RSM_ProfileId = settings.Profile_ID__c;
        
        //Create 3 users and insert them
        List<User> userToInsert = new List<User>();
        //User 1
        User u1 = new user();
        u1.FirstName = 'David';
        u1.LastName  = 'Owner 1';
     ...
Dear colleagues,

could you please help? I am new to Apex and just struggeling with the "System.LimitException: Too many Email Invocations: 11" error.

I am trying to send an email to the Contact owner upon creation of a task for the contact. That works so far fine, except that I am breaking the governor limt for the number of emails to be sent.

This is my trigger:

trigger Individual_SM_Notification_v2 on Task (after insert) {

//Query for Contact    
//Step 1: Create a Set of all Contacts to query
Set<String> allWhoIds = new Set<String>();
for (Task newTask : Trigger.new){
if (newTask.WhoId != null){
allWhoIds.add(newTask.WhoId);
    system.debug('Step 1: ' + newTask.WhoId);
}
}

//Step 2:Query the Contact Ids from the WhoIds (from Step 1)
List <Contact> potentialContacts = [Select Id, Name, Owner.Email, Owner.LastName From Contact Where Id In :allWhoIds];
system.debug('List<Contact> Details: ' + potentialContacts);
   
    
    
//Step 3: Create Map to search Contacts by WhoId
Map<String, String>WhoIdToContactMap = new map<String, String>();
for (Contact c : potentialContacts){
WhoIdToContactMap.put(c.Id, c.Owner.Email);
}

// Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
//Get the matching Contact Owner by the Contact Id
for (task newTask : Trigger.new){
String UserToUpdate = WhoIdToContactMap.get(newTask.WhoId);
System.debug('User to update: ' + UserToUpdate);
      

    
    
    
    
// Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
    
// Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(UserToUpdate);
      mail.setToAddresses(sendTo); 
    System.debug('Step 2:' + sendTo);
    
    
// Step 3: Set who the email is sent from
      mail.setReplyTo('SalesforceAdmin@bcsprime.com');
      mail.setSenderDisplayName('BCS GMIB Research Team'); 
    system.debug('Step 3:' + mail);

// (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('mhaase@bcsprime.com');
      mail.setCcAddresses(ccTo);    
    
 
// Step 4. Set email contents - you can use variables!
mail.setSubject('Research Call Update');
String body = 'Date: ' + newTask.ActivityDate + '<br/>';
body += 'Caller: ' + newTask.Assigned_To_Dupe__c + '<br/>';
body += 'Company Name: ' + newTask.Account_Name_dupe__c + '<br/>';
body += 'Contact Name: ' + newTask.Client_Name__c  + '<br/>';
body += 'Description: ' + newTask.Description + '<br/>';
mail.setHtmlBody(body);

// Step 5. Add your email to the master list
mails.add(mail);    


// Step 6: Send all emails in the master list
Messaging.sendEmail(mails);
          
}
}




This is my test class:

@isTest
public class TestLastResearchActivity_v2 {
    static testMethod void testLastResearchActivity_v2(){
        
        
        Account acc = new Account();
        acc.Name = 'ABC Investments';
        acc.RecordTypeId = '012b0000000UQ0L';
        insert acc;
        
        Account acctId = [Select Id From Account Where Name = 'ABC Investments'];
        
        
        Contact c = new Contact();
        c.LastName = 'Meier';
        c.RecordTypeId = '012b0000000USN6';
        insert c;
        
        //Lookup Contact Id
        Contact taskContact = [Select ID From Contact Where LastName = 'Meier'];
        
        
        //Create a list for the tasks to be updated
        List<Task> taskList = new List<Task>();
        
        //Tasks Chuyko
        Task t = new Task();
        t.WhatId = acctId.Id;
        t.RecordTypeId = '012b0000000URMt';
        t.OwnerId = '005b0000001R6YS';
        t.Communication_Type__c = 'Conversation';
        t.Whoid = taskContact.Id;
        insert t;
        
        
        //Tasks Petropavlovski
        Task t2 = new Task();
        t2.WhatId = acctId.Id;
        t2.RecordTypeId = '012b0000000URMt';
        t2.OwnerId = '005b0000001TATI';
        t2.Communication_Type__c = 'Conversation';
        t2.Whoid = taskContact.Id;
        insert t2;
        
        //Tasks Tikhomirov
        Task t3 = new Task();
        t3.WhatId = acctId.Id;
        t3.RecordTypeId = '012b0000000URMt';
        t3.OwnerId = '005b0000001TAgR';
        t3.Communication_Type__c = 'Conversation';
        t3.Whoid = taskContact.Id;
        insert t3;
        
        //Tasks Ibragimov
        Task t4 = new Task();
        t4.WhatId = acctId.Id;
        t4.RecordTypeId = '012b0000000URMt';
        t4.OwnerId = '005b0000001RKOq';
        t4.Communication_Type__c = 'Conversation';
        t4.Whoid = taskContact.Id;
        insert t4;
        
        //Tasks Naydennova
        Task t5 = new Task();
        t5.WhatId = acctId.Id;
        t5.RecordTypeId = '012b0000000URMt';
        t5.OwnerId = '005b0000001RKOl';
        t5.Communication_Type__c = 'Conversation';
        t5.Whoid = taskContact.Id;
        insert t5;
        
        //Tasks Goncharov
        Task t6 = new Task();
        t6.WhatId = acctId.Id;
        t6.RecordTypeId = '012b0000000URMt';
        t6.OwnerId = '005b0000001RKOg';
        t6.Communication_Type__c = 'Conversation';
        t6.Whoid = taskContact.Id;
        insert t6;
        
        //Tasks Tachennikov
        Task t7 = new Task();
        t7.WhatId = acctId.Id;
        t7.RecordTypeId = '012b0000000URMt';
        t7.OwnerId = '005b0000001TAUk';
        t7.Communication_Type__c = 'Conversation';
        t7.Whoid = taskContact.Id;
        insert t7;
        
        //Tasks Mitchell
        Task t8 = new Task();
        t8.WhatId = acctId.Id;
        t8.RecordTypeId = '012b0000000URMt';
        t8.OwnerId = '005b0000001RKOv';
        t8.Communication_Type__c = 'Conversation';
        t8.Whoid = taskContact.Id;
        insert t8;
        
        //Tasks Kotok
        Task t9 = new Task();
        t9.WhatId = acctId.Id;
        t9.RecordTypeId = '012b0000000URMt';
        t9.OwnerId = '005b0000001TAb7';
        t9.Communication_Type__c = 'Conversation';
        t9.Whoid = taskContact.Id;
        insert t9;
        }
}

Thanks in advance for your help!!!
Hi,
Is there any possibility to remove/hide  "New Event" button from Home Page?

User-added image

Thanks.
Hi,  I receive the following error in a test class but have not been able to repeat them in non test running scenarios.

"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. 
A flow trigger failed to execute the flow with version ID 301O00000008hM6. 
Contact your administrator for help.: []"

Does anyone know if there could be an issue running flows in test class contexts but not otherwise?

Thanks.
Hi everyone. I am new to APEX and need some help.

I need to update the date field "Last_Reseach_Call__c) on the contact record always when a new task with record type "Log Call" was created. This field update cannot be done via workflow, therefore I am trying to solve the problem with an Apex trigger.

Here is the code I have so far:

trigger UpdateContactLastActivity on Task (after insert, after update)
 {

// The set used to hold the Ids of the Contacts that need to be updated
Set <Id> ContactIds = new Set <Id> ();

// The actual Contacts to save
List <Contact> ContactsToUpdate = new List <Contact> ();

// Now we go through and if we find Tasks that are marked as Research Call, then update the Contact with "Last Research Contact" date
for (Task t : Trigger.new)
{
if (t.RecordType)
{
ContactIds.add (t.Contact);
}
}

// Now have the list of Contacts, fetch the associated Contact records and updatethem
if (!ContactIds.isEmpty () )
{
for (Contact c : [select Id, LastName from Contact where Id in :ContactIds])
{
contact.Last_Research_Contact__c = Today();
ContactsToUpdate.add (c);
}

update ContactsToUpdate;
}
}


I currently cannot save the code as I get an error ЭCompile Error: Invalid field Contact for SObject Task at line 15 column 17".

Could please someone advise where the problem is and how the correct code should look like?

Thanks an advance!