+ Start a Discussion
Huy NguyenHuy Nguyen 

SOQL exception

Hi Expert. I have a code below. Can anyone help to fix to avoid SOQL exception :

List<RecordType> recordTypes = [SELECT Id, Name FROM RecordType Where Name='Non-Compliance' Limit 1];
        String recordTypeId=recordTypes[0].Id;
        
        for(Task item : taskUpsertList){
            List<Non_Compliance__c> newNonComplianceList = mapNonCompliance.get(item.Subject.substring(0,4));
            for(Non_Compliance__c nc : newNonComplianceList){
                item.Subject = nc.Name;
                item.Legislative_Reference__c = nc.Legislative_Reference__c;
                item.Compliance_Issue__c = nc.Compliance_Issue__c;
                item.Offence_Reference__c = nc.Offence_Reference__c;
                item.Offender__c = nc.Offender__c;
                item.Offence_Penalty__c = nc.Offence_Penalty__c;
                item.Penalty_Notice_Penalty__c = nc.Penalty_Notice_Penalty__c;
                item.Outcome__c = nc.Outcome__c;
                item.Type = nc.RecordType.Name;
                item.Description = nc.Comments__c;
                item.Priority = nc.Priority__c;
                if(nc.RecordType.Name == 'Line Item Non-Compliance'){
                    item.Line_Item_Category_Id__c  = nc.Line_Item_Category__r.Id;
                }
                item.Non_Compliance_Id__c = nc.Id;
                item.Non_Compliance_Number__c = nc.Non_Compliance_Number__c;
                item.RecordTypeId = recordTypeId;
            }
        }
U JayU Jay
Could you please specify what soql exception you got? there was a chance to get null point exception (Not sure was that your exception) . And resolved using the below code :-



List<RecordType> recordTypes = new List<RecordType>();
String recordTypeId;
 for(RecordType recordTypeItem : [SELECT Id, Name FROM RecordType Where Name='Non-Compliance']){
        recordTypeId =recordTypeItem.Id;
         break;
 }      
        for(Task item : taskUpsertList){
            List<Non_Compliance__c> newNonComplianceList = mapNonCompliance.get(item.Subject.substring(0,4));
            for(Non_Compliance__c nc : newNonComplianceList){
                item.Subject = nc.Name;
                item.Legislative_Reference__c = nc.Legislative_Reference__c;
                item.Compliance_Issue__c = nc.Compliance_Issue__c;
                item.Offence_Reference__c = nc.Offence_Reference__c;
                item.Offender__c = nc.Offender__c;
                item.Offence_Penalty__c = nc.Offence_Penalty__c;
                item.Penalty_Notice_Penalty__c = nc.Penalty_Notice_Penalty__c;
                item.Outcome__c = nc.Outcome__c;
                item.Type = nc.RecordType.Name;
                item.Description = nc.Comments__c;
                item.Priority = nc.Priority__c;
                if(nc.RecordType.Name == 'Line Item Non-Compliance'){
                    item.Line_Item_Category_Id__c  = nc.Line_Item_Category__r.Id;
                }
                item.Non_Compliance_Id__c = nc.Id;
                item.Non_Compliance_Number__c = nc.Non_Compliance_Number__c;
                if(recordTypeId != Null){
                    item.RecordTypeId = recordTypeId;
                }
                 else{
                               // will take the default recordtype
                 }
            }
        }
Pankaj_GanwaniPankaj_Ganwani
Can you please provide us the exact exception which you are facing?

Always use for SOQL or check emptiness of the list before assigning this to variable to avoid null pointer exception.