You need to sign in to do that
Don't have an account?
Jason Thurgood
Apex Trigger Padawan - Lead Before Trigger - Null Pointer Exception
CODE:
trigger LeadTrigger on Lead (before insert) {
Id recordtypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'SalesLead' AND SobjectType = 'Lead'].id;
List<Lead> LeadList = [SELECT Id, Round_Robin_Id__c, RecordTypeId, Eligible_for_Round_Robin__c FROM Lead WHERE RecordTypeId =: recordTypeId AND Eligible_for_Round_Robin__c = TRUE ORDER BY Round_Robin_Id__c DESC LIMIT 1];
for(Lead lead :trigger.new)
{
IF( Lead.RecordTypeId == recordtypeId && Lead.MobilePhone != null )
{
IF( LeadList.size() > 0 )
{
Decimal maxlead = leadList[0].Round_Robin_ID__c;
lead.Round_Robin_ID__c = maxlead +1;
}
else
{
lead.Round_Robin_ID__c = 1;
}
}
}
}
ERROR:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LeadTrigger: line 17, column 1 (line 17 is bold and italisized)
I know this is probably super obvious to some of you. But thanks for any hep.
trigger LeadTrigger on Lead (before insert) {
Id recordtypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'SalesLead' AND SobjectType = 'Lead'].id;
List<Lead> LeadList = [SELECT Id, Round_Robin_Id__c, RecordTypeId, Eligible_for_Round_Robin__c FROM Lead WHERE RecordTypeId =: recordTypeId AND Eligible_for_Round_Robin__c = TRUE ORDER BY Round_Robin_Id__c DESC LIMIT 1];
for(Lead lead :trigger.new)
{
IF( Lead.RecordTypeId == recordtypeId && Lead.MobilePhone != null )
{
IF( LeadList.size() > 0 )
{
Decimal maxlead = leadList[0].Round_Robin_ID__c;
lead.Round_Robin_ID__c = maxlead +1;
}
else
{
lead.Round_Robin_ID__c = 1;
}
}
}
}
ERROR:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LeadTrigger: line 17, column 1 (line 17 is bold and italisized)
I know this is probably super obvious to some of you. But thanks for any hep.
Figured it out:
List<Lead> LeadList = [SELECT Id, Round_Robin_Id__c, RecordTypeId, Eligible_for_Round_Robin__c FROM Lead WHERE RecordTypeId =: recordTypeId AND Eligible_for_Round_Robin__c = TRUE AND Round_Robin_ID__c > 0 ORDER BY Round_Robin_Id__c DESC LIMIT 1];
My list was empty, since no Leads had a Round_Robin_ID__c value yet.
Added AND Round_Robin_ID__c > 0 to the WHERE Clause.
Please modify your code like this....
IF( LeadList.size() > 0 )
{
if(Round_Robin_ID__c!=null)
{
Decimal maxlead = leadList[0].Round_Robin_ID__c;
lead.Round_Robin_ID__c = maxlead +1;
}
}
Please mark this as solution if this will help you