1

I Have the following apex code which is throwing a nullpointer exception on the line:

points = points + value;

List<JMG_ExternalSearch__c> searchList = [SELECT Id,LastModifiedDate,JMG_External_Search_Points__c,
                                             (SELECT Id,JMG_ES_Points__c FROM New_applications__r) 
                                             FROM JMG_ExternalSearch__c WHERE ID IN: esIds];
  
  for(JMG_ExternalSearch__c searchRec : searchList){
      integer points = 0;
      if(searchRec.New_applications__r.size() >0 ){
          for(cxsrec__cxsNew_application__c Ja: searchRec.New_applications__r){
              system.debug('Ja.JMG_ES_Points__c: ' + Ja.JMG_ES_Points__c);
              integer value = (integer)Ja.JMG_ES_Points__c;
            points = points + value; 
          }
      } system.debug('points: ' + points);
     searchRec.JMG_External_Search_Points__c = points;
  }
update searchList;

What am I missing? Thanks in advance.

1
  • HI @Thomas, given you've debugged the Ja.JMG_ES_Points__c field in your supplied code, I am assuming that you're seeing some of those values come through as null or blank on some records? Commented Feb 7, 2023 at 19:03

2 Answers 2

1

I'm not sure what data type your points field is, but essentially, just make sure you don't have a null value:

Integer value = (Integer)Ja.JMG_ES_Points__c;
value = value == null ? 0 : value;
1
  • 1
    That seem to be the culprit. Thanks!
    – Thomas
    Commented Feb 7, 2023 at 19:17
1

You could have, and should have, filtered out the records with null points. Also note that iterating over an empty list is acceptable. No need to check for an empty list first.

List<JMG_ExternalSearch__c> searchList = [
  SELECT Id,LastModifiedDate,JMG_External_Search_Points__c,
         (SELECT JMG_ES_Points__c FROM New_applications__r WHERE JMG_ES_Points__c != NULL) 
  FROM JMG_ExternalSearch__c WHERE ID IN: esIds];
  
  for(JMG_ExternalSearch__c searchRec : searchList){
      integer points = 0;
      for(cxsrec__cxsNew_application__c Ja: searchRec.New_applications__r){
          system.debug('Ja.JMG_ES_Points__c: ' + Ja.JMG_ES_Points__c);
          integer value = (integer)Ja.JMG_ES_Points__c;
        points = points + value; 
      }
     searchRec.JMG_External_Search_Points__c = points;
  }
update searchList;

However, I'd argue that you should let the database perform the tally for you. It's more efficient than Apex is.

Map<Id, JMG_ExternalSearch__c> recordsToUpdate = new Map<Id, JMG_ExternalSearch__c>();
for(Id esId: esIds) {
  recordsToUpdate.put(esId, new JMG_ExternalSearch__c(Id=esId, JMG_External_Search_Points__c=0));
}
for(AggregateResult result: [SELECT JMG_ExternalSearch__c Id, SUM(JMG_ES_Points__c) sum FROM New_Applications__c WHERE JMG_ExternalSearch__c = :esIds GROUP BY JMG_ExternalSearch__c]) {
  recordsToUpdate.get((Id)result.get('Id')).JMG_External_Search_Points__c = (Decimal)result.get('sum');
}
update recordsToUpdate.values();

Also, something like DLRS is purpose-built for your use case. Also, if the relationship is a master-detail relationship, a standard rollup summary field would have worked. While it can be fun to write your own code, one should take a moment to at least research options with less maintenance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .