function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tmbarrytmbarry 

Trigger Doing funny things.

I created a simple trigger to increment a numeric value on an object if a certain task is created.  

 

trigger UpdateSentMail_01 on Task (after insert){ 

// Tasks that meet criteria, and new tasks to create
Task[] qualifiedTasks = new Task[0], newTasks = new Task[0];
    
// Map of SD Members
   Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();

// Find qualifying tasks
for(Task record:Trigger.new)
        if(Record.subject!=null && record.subject.contains('Print Sf Order Submitted') && record.status == 'Completed')
           qualifiedtasks.add(record);
    
    // Obtain member ID values
    for(Task record:qualifiedtasks)
        members.put(record.whatid,null);
   
    // If there are any membes to query, do so.
    if(!members.isempty())
        members.putall([select id, letter_counter__c from SD_Member__c where id in :members.keyset()]);

double i = 0;

     for(Task record:qualifiedtasks) {
// Get the current Letter Counter value
   i = members.get(record.whatid).letter_counter__c;
// Increase the Letter Counter by 1
    i=i+1;

        members.get(record.whatid).Send_Monthly_Letter__c=False;
    }
    
     update members.values();

    for(Task record:qualifiedtasks) {
        members.get(record.whatid).Send_Monthly_Letter__c=True;
        members.get(record.whatid).Make_1st_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_2nd_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_3rd_Follow_Up_Call__c=False;
        members.get(record.whatid).Letter_Counter__c=i;
    }
    
    update members.values();
}

 Before I entered the test tasks using excel connector here is what my test data looked like:

Record IDFirst NameLast NameLetter Counter
a02W000000A8SgfIAFTestRecord_011517
a02W000000A8SggIAFTestRecord_01551
a02W000000A8SghIAFTestRecord_01358
a02W000000A8SgiIAFTestRecord_017521
a02W000000A8SgjIAFTestRecord_019514
a02W000000A8SglIAFTestRecord_011616
a02W000000A8SgmIAFTestRecord_01560
a02W000000A8SgnIAFTestRecord_01369
a02W000000A8SgoIAFTestRecord_017622
a02W000000A8SgpIAFTestRecord_019615
a02W000000A8SgqIAFTestRecord_011717
a02W000000A8SgrIAFTestRecord_01570
a02W000000A8SgsIAFTestRecord_013710
a02W000000A8SgtIAFTestRecord_017723
a02W000000A8SguIAFTestRecord_019716
a02W000000A8Sh0IAFTestRecord_011818
a02W000000A8Sh1IAFTestRecord_01580
a02W000000A8Sh2IAFTestRecord_013811
a02W000000A8Sh3IAFTestRecord_017824
a02W000000A8Sh4IAFTestRecord_019817
a02W000000A8ShAIAVTestRecord_011919
a02W000000A8ShBIAVTestRecord_01590
a02W000000A8ShEIAVTestRecord_019918
a02W000000A8ShCIAVTestRecord_013912
a02W000000A8ShDIAVTestRecord_017925

 

Now, after entering in my test tasks, each record should have increment by 1.  Instead I got this:

 

Record IDFirst NameLast NameLetter Counter
a02W000000A8SgfIAFTestRecord_011513
a02W000000A8SggIAFTestRecord_015513
a02W000000A8SghIAFTestRecord_013513
a02W000000A8SgiIAFTestRecord_017513
a02W000000A8SgjIAFTestRecord_019513
a02W000000A8SglIAFTestRecord_011613
a02W000000A8SgmIAFTestRecord_015613
a02W000000A8SgnIAFTestRecord_013613
a02W000000A8SgoIAFTestRecord_017613
a02W000000A8SgpIAFTestRecord_019613
a02W000000A8SgqIAFTestRecord_011713
a02W000000A8SgrIAFTestRecord_015713
a02W000000A8SgsIAFTestRecord_013713
a02W000000A8SgtIAFTestRecord_017713
a02W000000A8SguIAFTestRecord_019713
a02W000000A8Sh0IAFTestRecord_011813
a02W000000A8Sh1IAFTestRecord_015813
a02W000000A8Sh2IAFTestRecord_013813
a02W000000A8Sh3IAFTestRecord_017813
a02W000000A8Sh4IAFTestRecord_019813
a02W000000A8ShAIAVTestRecord_011913
a02W000000A8ShBIAVTestRecord_015913
a02W000000A8ShEIAVTestRecord_019913
a02W000000A8ShCIAVTestRecord_013913
a02W000000A8ShDIAVTestRecord_017913

 

Since I didn't want to include all 700 test records, another block of records had their Letter_counter__c field set to all 3s and another group all 11.

 

Any ideas what is happening?

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Try this

 

trigger UpdateSentMail_01 on Task (after insert){ 

// Tasks that meet criteria, and new tasks to create
Task[] qualifiedTasks = new Task[0], newTasks = new Task[0];
    
// Map of SD Members
   Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();

// Find qualifying tasks
for(Task record:Trigger.new)
        if(Record.subject!=null && record.subject.contains('Print Sf Order Submitted') && record.status == 'Completed')
           qualifiedtasks.add(record);
    
    // Obtain member ID values
    for(Task record:qualifiedtasks)
        members.put(record.whatid,null);
   
    // If there are any membes to query, do so.
    if(!members.isempty())
        members.putall([select id, letter_counter__c from SD_Member__c where id in :members.keyset()]);

double i = 0;

     for(Task record:qualifiedtasks) {
// Get the current Letter Counter value
   i = members.get(record.whatid).letter_counter__c;
// Increase the Letter Counter by 1
    i=i+1;

        members.get(record.whatid).Send_Monthly_Letter__c=False;
        members.get(record.whatid).Letter_Counter__c=i;
    }
    
     update members.values();

    for(Task record:qualifiedtasks) {
        members.get(record.whatid).Send_Monthly_Letter__c=True;
        members.get(record.whatid).Make_1st_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_2nd_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_3rd_Follow_Up_Call__c=False;
        //members.get(record.whatid).Letter_Counter__c=i;
//when you are updating "i" from here it is actually taking the last value from the last for loop and is static
} update members.values(); }

 

All Answers

Avidev9Avidev9

Try this

 

trigger UpdateSentMail_01 on Task (after insert){ 

// Tasks that meet criteria, and new tasks to create
Task[] qualifiedTasks = new Task[0], newTasks = new Task[0];
    
// Map of SD Members
   Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();

// Find qualifying tasks
for(Task record:Trigger.new)
        if(Record.subject!=null && record.subject.contains('Print Sf Order Submitted') && record.status == 'Completed')
           qualifiedtasks.add(record);
    
    // Obtain member ID values
    for(Task record:qualifiedtasks)
        members.put(record.whatid,null);
   
    // If there are any membes to query, do so.
    if(!members.isempty())
        members.putall([select id, letter_counter__c from SD_Member__c where id in :members.keyset()]);

double i = 0;

     for(Task record:qualifiedtasks) {
// Get the current Letter Counter value
   i = members.get(record.whatid).letter_counter__c;
// Increase the Letter Counter by 1
    i=i+1;

        members.get(record.whatid).Send_Monthly_Letter__c=False;
        members.get(record.whatid).Letter_Counter__c=i;
    }
    
     update members.values();

    for(Task record:qualifiedtasks) {
        members.get(record.whatid).Send_Monthly_Letter__c=True;
        members.get(record.whatid).Make_1st_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_2nd_Follow_Up_Call__c=False;
        members.get(record.whatid).Make_3rd_Follow_Up_Call__c=False;
        //members.get(record.whatid).Letter_Counter__c=i;
//when you are updating "i" from here it is actually taking the last value from the last for loop and is static
} update members.values(); }

 

This was selected as the best answer
tmbarrytmbarry

Thanks Avi,

 

That fixed it!

tmbarrytmbarry

Hey Avi, 

 

I ran into and issue.  

 

The trigger works great.  I wrote the test class and got 100% coverage.  But when I upload it into Production and validate it, it fails and I get the following messages on those unrelated test classes:

 

MyTestClass001.myUnitTest()Class361Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...
MyTestClass002.myUnitTest()Class361Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...
MyTestClass003.myUnitTest()Class151Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...
MyTestClass004.myUnitTest()Class151Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...
MyTestClass005.myUnitTest()Class151Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...
MyTestClass007.myUnitTest()Class151Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateSentMail_01: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateSentMail_01: line 37, column 1: []", Failure Stack Trace: "Cl...

 

Any thoughts as to what to do next?

Avidev9Avidev9
can you highlight line number 37 for me ?