• Justin Staugaitis
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Im new to APEX and i have a trigger that is suppose to calculate a total score using 6 fields.  If any of the data in those 6 fields gets updated I want the Trigger to recalcuate the score.  But my current code is not updating.  I have two issues

1) After I deployed the code the APEX trigger didnt do the calculation.  Why is this? 
2) After I made a change to one of the 6 data points the Trigger still didnt execute.  Why is this?

Could someone help look at the code I have and provide some recommeneded changes.  (just note I am new so some of the terms I may not understand if you would make any code changes can you show me the before and after change).  

trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
             if(cob.Days_With_No_Activity__c!=null){
            //   cob.Days_With_No_Activity__c= 100 ;
             
             
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New2__c * 0.15) + 
                           (cob.Contact_Left2__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
                           }
           }
       }
    }
}
I need help changing my APEX trigger Im getting the following error message  

MassLeadConverterControllerTest
massLeadTest
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Health_Score_Calculation_New: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Health_Score_Calculation_New: line 12, column 1: [] 
Stack Trace: Class.MassLeadConverterControllerTest.massLeadTest: line 11, column 1


Below is the Trigger 

trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New_Rollup__c * 0.15) + 
                           (cob.Contact_Left_Rollup__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
           }
       }
    }
}
I received the following error message and researched on the forms and the solution seems to be adding UPDATE FOR but I couldnt make that update in SOQL query as I received an error message "Cannot lock rows for an SObject type that can not be updated: AggregateResult"

So im not sure if I need to edit the code where I call List<Account> to an SOQL statement.  See my code below.

ISSUE MESSAGE:

Apex script unhandled trigger exception by user/organization: 0053400000AEv6J/00D80000000PTja
 
accountIssueScore: execution of AfterUpdate
 
caused by: System.DmlException: Update failed. First exception on row 0 with id 0018000001AKu9XAAT; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0018000001AKu9XAAT: []
 
Trigger.accountIssueScore: line 17, column 1
 
trigger accountIssueScore on Case (after update, after insert) {

        List<AggregateResult> contactAggrList = [SELECT AccountId, SUM(Severity_Value__c) 
                                                 FROM Case 
                                                 WHERE Id IN :Trigger.New
                                                 AND 
                                                 AccountId <> null
                                                 AND Severity__c IN ('Blocker','Critical','Major')
                                                 Group By AccountId FOR UPDATE
                                                 ];

        List<Account> accountsToUPdate3 = new List<Account>();

        for(AggregateResult contactAggr : contactAggrList) {
            accountsToUPdate3.add(new Account(Id = (Id)contactAggr.get('AccountId'),
                                             Issue_Score__c = (Decimal)contactAggr.get('expr0'))); 

        }

        update accountsToUPdate3;
}

​​​​​​​
 
Im new to APEX and i have a trigger that is suppose to calculate a total score using 6 fields.  If any of the data in those 6 fields gets updated I want the Trigger to recalcuate the score.  But my current code is not updating.  I have two issues

1) After I deployed the code the APEX trigger didnt do the calculation.  Why is this? 
2) After I made a change to one of the 6 data points the Trigger still didnt execute.  Why is this?

Could someone help look at the code I have and provide some recommeneded changes.  (just note I am new so some of the terms I may not understand if you would make any code changes can you show me the before and after change).  

trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
             if(cob.Days_With_No_Activity__c!=null){
            //   cob.Days_With_No_Activity__c= 100 ;
             
             
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New2__c * 0.15) + 
                           (cob.Contact_Left2__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
                           }
           }
       }
    }
}
I received the following error message and researched on the forms and the solution seems to be adding UPDATE FOR but I couldnt make that update in SOQL query as I received an error message "Cannot lock rows for an SObject type that can not be updated: AggregateResult"

So im not sure if I need to edit the code where I call List<Account> to an SOQL statement.  See my code below.

ISSUE MESSAGE:

Apex script unhandled trigger exception by user/organization: 0053400000AEv6J/00D80000000PTja
 
accountIssueScore: execution of AfterUpdate
 
caused by: System.DmlException: Update failed. First exception on row 0 with id 0018000001AKu9XAAT; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0018000001AKu9XAAT: []
 
Trigger.accountIssueScore: line 17, column 1
 
trigger accountIssueScore on Case (after update, after insert) {

        List<AggregateResult> contactAggrList = [SELECT AccountId, SUM(Severity_Value__c) 
                                                 FROM Case 
                                                 WHERE Id IN :Trigger.New
                                                 AND 
                                                 AccountId <> null
                                                 AND Severity__c IN ('Blocker','Critical','Major')
                                                 Group By AccountId FOR UPDATE
                                                 ];

        List<Account> accountsToUPdate3 = new List<Account>();

        for(AggregateResult contactAggr : contactAggrList) {
            accountsToUPdate3.add(new Account(Id = (Id)contactAggr.get('AccountId'),
                                             Issue_Score__c = (Decimal)contactAggr.get('expr0'))); 

        }

        update accountsToUPdate3;
}

​​​​​​​