0 votes
1 view
by

I'* getting the following error because of this line of code (line 235) in my Before Update Trigger on Site Survey (when I hardcode the sum here, the test passes):

Integer sum = Integer.valueOf(aggSum.get('riskScoreTotal'));

System.DmlException: Update failed. First exception on row 0 with id a0o1k000001u0J2AAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SiteSurvey_Trigger: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.SiteSurvey_TriggerHandler.updateRiskScoreTotalOnSiteSurvey: line 235, column 1
Class.SiteSurvey_TriggerHandler.beforeUpdate: line 33, column 1
Class.TriggerHandler.handleTrigger: line 30, column 1
Trigger.SiteSurvey_Trigger: line 4, column 1: []

This is my trigger code Before Update

private static void updateRiskScoreTotalOnSiteSurvey(List<Site_Survey__c> newSurveys) {

Map<Id, AggregateResult> surveyIdToSum = new Map<Id, AggregateResult>(
  [SELECT Site_Survey__c Id, SUM(Score__c) riskScoreTotal
  FROM Site_Survey_Response__c
  WHERE Site_Survey__c IN :newSurveys
  GROUP BY Site_Survey__c]);

for (Site_Survey__c survey : newSurveys){
  AggregateResult aggSum = surveyIdToSum.get(survey.Id);

  if(survey.Status__c == 'In_Review' ){
    Integer sum = Integer.valueOf(aggSum.get('riskScoreTotal'));
    if(sum != null){
      survey.Risk_Score_Total__c = sum;
    }
  }
}

This is the Test Case that's failing

@IsTest
  static void testRecordTypeUpdateReview() {
    Map<String, Schema.RecordTypeInfo> nameToRT =
      RecordType_Utility.getRecordTypesByDevName(
        Site_Survey__c.getSObjectType());

Schema.RecordTypeInfo inProgrgess =
  nameToRT.get(SiteSurvey_TriggerHandler.RecordType.In_Progress.name());
Schema.RecordTypeInfo review =
  nameToRT.get(SiteSurvey_TriggerHandler.RecordType.Review.name());
Site_Survey__c survey =
  [SELECT Id, RecordTypeId FROM Site_Survey__c LIMIT 1];
System.assertEquals(inProgrgess.getRecordTypeId(), survey.RecordTypeId);
List<Survey_Section__c> sections =
  [SELECT Id FROM Survey_Section__c WHERE Survey__c = :survey.Id];

for (Survey_Section__c section : sections) {
  section.Status__c = 'Complete';
}

update sections;
survey.Status__c = SiteSurvey_TriggerHandler.Status.In_Review.name();
Test.startTest();
update survey;
Test.stopTest();
survey = [SELECT Id, RecordTypeId FROM Site_Survey__c LIMIT 1];
System.assertEquals(review.getRecordTypeId(), survey.RecordTypeId);

}

1 Answer

0 votes
by
 
Best answer

If zero rows would be returned, there's no entry in the map for the record, so you need to check for that first:

private static void updateRiskScoreTotalOnSiteSurvey(List<Site_Survey__c> newSurveys) {
    Map<Id, AggregateResult> surveyIdToSum = new Map<Id, AggregateResult>([
        SELECT Site_Survey__c Id, SUM(Score__c) riskScoreTotal
        FROM Site_Survey_Response__c
        WHERE Site_Survey__c IN :newSurveys
        GROUP BY Site_Survey__c
    ]);

    for (Site_Survey__c survey : newSurveys){
        if(survey.Status__c == 'In_Review' ){
            AggregateResult aggSum = surveyIdToSum.get(survey.Id);
            Integer sum = 0;
            if(aggSum != null) {
                sum = (Integer)aggSum.get('riskScoreTotal');
            }
            survey.Risk_Score_Total__c = sum;
        }
    }
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...