+ Start a Discussion
S N 27S N 27 

System.ListException: Row with null Id at index: 0

Hi All,
I have written a small piece of code that basically checks for the asset related to Case. If the case status is updated to "Application-submitted" and if there are no asset related to a case then throw an error. I have made use of Aggregate result to count the asset on the case.I don't know where my code is going wrong.pls help

trigger assetCountonCase on Case (before update) {

    Set<Id> caseId = new Set<Id>();
    Set<Id> rtIds = new Set<Id>();
    
    Map<ID,RecordType> casertMap = New Map<ID,RecordType>([Select ID,Name,DeveloperName From RecordType Where sObjectType ='Case']);
    system.debug('casertMap=========================='+casertMap);
    for(Case cse: Trigger.new){
        system.debug('cse==========================');
        if((cse.status=='Application-Submitted')&&((casertMap.get(cse.recordTypeID).Developername.containsIgnoreCase('Simple_Application'))||(casertMap.get(cse.recordTypeID).Developername.containsIgnoreCase('Complex_Application')))){
            caseId.add(cse.Id);
            system.debug('caseId========================'+caseId);
        }
    }
    
    Map<Id,AggregateResult> assetConMap = new Map<Id,AggregateResult>([Select JMB1__Case__c id,count(id) a from Asset where JMB1__Case__c!=null and JMB1__Case__c in:caseId GROUP BY JMB1__Case__c]);
    system.debug('assetConMap========================='+assetConMap);
    
    for(Case cse: Trigger.new){
        AggregateResult assetsize = assetConMap.get(cse.Id);
        system.debug('assetsize==========================='+assetsize);
        if(assetsize==null){
            cse.addError('One asset is required');
        }
    
    }

}
karthikeyan perumalkarthikeyan perumal
Hello 

use below updated code
 
trigger assetCountonCase on Case (before update) {

    Set<Id> caseId = new Set<Id>();
    Set<Id> rtIds = new Set<Id>();
    
    Map<ID,RecordType> casertMap = New Map<ID,RecordType>([Select ID,Name,DeveloperName From RecordType Where sObjectType ='Case']);
    system.debug('casertMap=========================='+casertMap);
    for(Case cse: Trigger.new){
        system.debug('cse==========================');
        if((cse.status=='Application-Submitted')&&((casertMap.get(cse.recordTypeID).Developername.containsIgnoreCase('Simple_Application'))||(casertMap.get(cse.recordTypeID).Developername.containsIgnoreCase('Complex_Application')))){
            caseId.add(cse.Id);
            system.debug('caseId========================'+caseId);
        }
    }
    
    Map<Id,AggregateResult> assetConMap = new Map<Id,AggregateResult>([Select JMB1__Case__c id,count(id) a from Asset where JMB1__Case__c!=null and JMB1__Case__c in:caseId GROUP BY JMB1__Case__c]);
    system.debug('assetConMap========================='+assetConMap);
    
   if(assetConMap.size()>0)
   {  
    for(Case cse: Trigger.new){
        AggregateResult assetsize = assetConMap.get(cse.Id);
        system.debug('assetsize==========================='+assetsize);
        if(assetsize==null){
            cse.addError('One asset is required');
        }
    
    }
   }

}

Hope this will help you, 

Thanks
karthik