function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matthew HoldgateMatthew Holdgate 

Apex code issue unexpected error

Hi

I have the Apex code and have no idea about Apex code or triggers so I can't solve any issues that appear. I get this error when creating a new record.

Apex trigger DOUM_SetTermsConditions caused an unexpected exception, contact your administrator: DOUM_SetTermsConditions: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.DOUM_SetTermsConditions: line 4, column 1

Any ideas as to how I can fix it?
trigger DOUM_SetTermsConditions on DOUM_Version__c (before insert, before update) {
Map<Id,Id> cases = new Map<Id,Id>();
    List<DOUM_Version__c> listGL = new List<DOUM_Version__c>();
    listGL = [Select DOUM_Terms_Conditions__r.Name, DOUM_Terms_Conditions__c from DOUM_Version__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c <> null];
    for(DOUM_Version__c objGL: listGL)
    {
        cases.put(objGL.id,objGL.DOUM_Terms_Conditions__r.Name);
    }
    for (DOUM_Version__c a : Trigger.new)
    {
        if(a.DOUM_Terms_Conditions__c != null)
        {
            a.DOUM_Terms_Conditions__c = cases.get(a.DOUM_Terms_Conditions__c);
        }
    }
}

 
Abhilash Mishra 13Abhilash Mishra 13
Hi Matthew,

Try this ,
trigger DOUM_SetTermsConditions on DOUM_Version__c (before insert, before update) {
Map<Id,Id> cases = new Map<Id,Id>();
    List<DOUM_Version__c> listGL = new List<DOUM_Version__c>();
    listGL = [Select DOUM_Terms_Conditions__r.Name, DOUM_Terms_Conditions__c from DOUM_Version__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c <> null];
if(listGL !=null && ! listGL.isempty()){ 
    for(DOUM_Version__c objGL: listGL)
    {
        cases.put(objGL.id,objGL.DOUM_Terms_Conditions__r.Name);
    }
}
    for (DOUM_Version__c a : Trigger.new)
    {
        if(a.DOUM_Terms_Conditions__c != nul && cases.containskey(a.DOUM_Terms_Conditions__c))
        {
            a.DOUM_Terms_Conditions__c = cases.get(a.DOUM_Terms_Conditions__c);
        }
    }
}

Let me know if this works or not.
Mark is solved by selecting a best answer. if it solves the problem.
Matthew HoldgateMatthew Holdgate
It doesnt work i still get this error unfortunately
 
Apex trigger DOUM_SetTermsConditions caused an unexpected exception, contact your administrator: DOUM_SetTermsConditions: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.DOUM_SetTermsConditions: line 4, column 1

What I want to happen is I have a filtered lookup field on the object DOUM_Version looking upon an object called DOUM_Terms_Conditions. The lookup field is called "DOUM_Terms_Conditions__c" 

The terms and conditions object has fields: (Label-Name[API]-Type)

"Terms & Conditions V" - Name - Auto Number (Unique Primary ID)
"Active Date" - DOUM_TCs_A_D__c - Date
"Approval Status" - DOUM_TCs_Approval_Status__c - Picklist
"Retired Date" - DOUM_TCs_RD__c - Date
"Terms & Conditions" - DOUM_TCsContent__c - Rich Text Area

The filter on the lookup basically looks for the only record that doesnt have a retirement date. 

I was wondering if it may be the filter that is breaking this trigger?
 
Abhilash Mishra 13Abhilash Mishra 13
Try this,
trigger DOUM_SetTermsConditions on DOUM_Version__c (before insert, before update) {
Map<Id,Id> cases = new Map<Id,Id>();
    List<DOUM_Version__c> listGL = new List<DOUM_Version__c>();
try{    
listGL = [Select DOUM_Terms_Conditions__r.Name, DOUM_Terms_Conditions__c from DOUM_Version__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c <> null];
}catch(exception e){
 system.debug(e.getmessage());
}
if(listGL !=null && ! listGL.isempty()){ 
    for(DOUM_Version__c objGL: listGL)
    {
        cases.put(objGL.id,objGL.DOUM_Terms_Conditions__r.Name);
    }
}
    for (DOUM_Version__c a : Trigger.new)
    {
        if(a.DOUM_Terms_Conditions__c != nul && cases.containskey(a.DOUM_Terms_Conditions__c))
        {
            a.DOUM_Terms_Conditions__c = cases.get(a.DOUM_Terms_Conditions__c);
        }
    }
}

I dont think It's the filter. Error is on Line 4 Column 1, Which is a SOQL statement.

Try above then we will see what else can be done .

Regards
Abhilash Mishra.
Matthew HoldgateMatthew Holdgate
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger DOUM_SetTermsConditions caused an unexpected exception, contact your administrator: DOUM_SetTermsConditions: execution of BeforeUpdate caused by: System.StringException: Invalid id: Terms & Conditions V01: External entry point

I now get this error. This strikes me as it's trying to pull a record ID but receiving the unique name which is "Terms & Conditions V01"

We made progress

Kindest regards

Matt
Abhilash Mishra 13Abhilash Mishra 13
Hi Matt, 
This error is occurring before update.  we will get to it. but first can you check whether trigger is working correct for insert or not.
 just try to insert a new record.
you can contact me on abhilash.udit@gmail.com for Quick conversation about this problem.
 i will be happy to help.
Regards
Abhilash
Matthew HoldgateMatthew Holdgate
Im still needing help

my code is 
trigger setTermsConditions on DOUM_DOU__c (before insert, before update) {

Map<Id,Id> cases = new Map<Id,Id>(); 
 
    List<DOUM_Terms_Conditions__c> listGL = new List<DOUM_Terms_Conditions__c>(); 

try{     

listGL = [Select DOUM_TermsConditions.Name, DOUM_Terms_Conds__c from DOUM_DOU__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c.DOUM_TCs_RD__c = NULL]; 

}catch(exception e){ 

 system.debug(e.getmessage()); 

} 

if(listGL !=null && ! listGL.isempty()){  

    for(DOUM_DOU__c objGL: listGL) 
 
    { 

        cases.put(objGL.id,objGL.DOUM_TermsConditions__r.Name); 
        
    } 

} 

    for (DOUM_DOU__c a : Trigger.new) 

    { 
    
        if(a.DOUM_Terms_Conds__c != nul && cases.containskey(a.DOUM_Terms_Conds__c)) 

        { 
            a.DOUM_Terms_Conds__c = cases.get(a.DOUM_Terms_Conds__c); 

        } 
    } 

} 


}
User-added image