You need to sign in to do that
Don't have an account?
Jeff Bomar
Problem Creating a Apex trigger
What is needed:
custom object: Custom_Programming__c
fields to validate: Custom_Programming__c.Custom_Type__c and Custom_Programming__c.Total_Hours__c
Any time the record is inserted or changed:
Custom_Type__c field changes and Total_Hours__c is greater than 0 or the Total_Hours__c is changed and meets the criteria it will get the price from the price book and update custom.Testing_Balance__c
I get is this message when testing:
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateBalanceTapeDrop caused an unexpected exception, contact your administrator: UpdateBalanceTapeDrop: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.: Trigger.UpdateBalanceTapeDrop: line 29, column 1".
Code:
trigger UpdateBalanceTapeDrop on Custom_Programming__c (before insert, before update)
{
for (Custom_Programming__c custom : Trigger.new)
{
//SQL statement to lookup price
List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
//Conditions FOR Tapedrop
if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for Tapedrop
{
custom.Testing_Balance__c = sqlResult[1].UnitPrice + (sqlResult[0].UnitPrice * custom.Total_Hours__c);
}
//Conditions for No Billing or Bug Fix
if (custom.Custom_Type__c =='No Billing' || custom.Custom_Type__c =='Bug Fix' && custom.Total_Hours__c >0)
//Calculate Balance for No Billing or Bug Fix
{
custom.Testing_Balance__c = sqlResult[0].UnitPrice * 0;
}
//Conditions for everything else
if (custom.Custom_Type__c != 'No Billing' || custom.Custom_Type__c !='Bug Fix' || custom.Custom_Type__c != 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for everything else
{
custom.Testing_Balance__c =sqlResult[0].UnitPrice * custom.Total_Hours__c;
}
}
}
Sorry if confusing I am new to Apex triggers and any help would be much appreciated.
custom object: Custom_Programming__c
fields to validate: Custom_Programming__c.Custom_Type__c and Custom_Programming__c.Total_Hours__c
Any time the record is inserted or changed:
Custom_Type__c field changes and Total_Hours__c is greater than 0 or the Total_Hours__c is changed and meets the criteria it will get the price from the price book and update custom.Testing_Balance__c
I get is this message when testing:
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateBalanceTapeDrop caused an unexpected exception, contact your administrator: UpdateBalanceTapeDrop: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.: Trigger.UpdateBalanceTapeDrop: line 29, column 1".
Code:
trigger UpdateBalanceTapeDrop on Custom_Programming__c (before insert, before update)
{
for (Custom_Programming__c custom : Trigger.new)
{
//SQL statement to lookup price
List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
//Conditions FOR Tapedrop
if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for Tapedrop
{
custom.Testing_Balance__c = sqlResult[1].UnitPrice + (sqlResult[0].UnitPrice * custom.Total_Hours__c);
}
//Conditions for No Billing or Bug Fix
if (custom.Custom_Type__c =='No Billing' || custom.Custom_Type__c =='Bug Fix' && custom.Total_Hours__c >0)
//Calculate Balance for No Billing or Bug Fix
{
custom.Testing_Balance__c = sqlResult[0].UnitPrice * 0;
}
//Conditions for everything else
if (custom.Custom_Type__c != 'No Billing' || custom.Custom_Type__c !='Bug Fix' || custom.Custom_Type__c != 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for everything else
{
custom.Testing_Balance__c =sqlResult[0].UnitPrice * custom.Total_Hours__c;
}
}
}
Sorry if confusing I am new to Apex triggers and any help would be much appreciated.
sqlResult[0] does not have any value which is why I believe you are seeing this error
You can do a size check here
I strongly recommend adding a try/catch as well in the code as described in the documentation here (https://help.salesforce.com/HTViewSolution?id=000063739)
Let me know if this helps
All Answers
I recommend doing a size check of sqlResults before using it in the code
As per this help article, the execution of a trigger will sometimes generate an error message, "execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]"
This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.
Resolve NullPointerException
The solution is to make sure the Object and/or the Attribute to be used is not null. In this example, the code needs to be modified as follows:
Exception handling routine
Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!
Review all error messages below to correct your data.
Apex trigger UpdateBalanceTapeDrop caused an unexpected exception, contact your administrator: UpdateBalanceTapeDrop: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.UpdateBalanceTapeDrop: line 29, column 1
Any help would be greatly appreciated
Apex script unhandled trigger exception by user/organization: 00550000006JDH2/00D290000001OCv
Source organization: 00D50000000JWQF (null)
UpdateBalanceTapeDrop: execution of AfterUpdate
caused by: System.NullPointerException: Argument cannot be null.
Trigger.UpdateBalanceTapeDrop: line 30, column 1
Code:
trigger UpdateBalanceTapeDrop on Custom_Programming__c (after insert, after update)
{
for (Custom_Programming__c custom : [Select Id, Custom_Type__c, Total_Hours__c, Testing_Balance__c From Custom_Programming__c where Id in :Trigger.new])
{
//SQL statement to lookup price
List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
//Conditions FOR Tapedrop
if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for Tapedrop
{
custom.Testing_Balance__c = sqlResult[1].UnitPrice + (sqlResult[0].UnitPrice * custom.Total_Hours__c);
}
//Conditions for No Billing or Bug Fix
if (custom.Custom_Type__c =='No Billing' || custom.Custom_Type__c =='Bug Fix' && custom.Total_Hours__c >0)
//Calculate Balance for No Billing or Bug Fix
{
custom.Testing_Balance__c = sqlResult[0].UnitPrice * 0;
}
//Conditions for everything else
if (custom.Custom_Type__c != 'No Billing' || custom.Custom_Type__c !='Bug Fix' || custom.Custom_Type__c != 'Tape Drop' && custom.Total_Hours__c >0)
//Calculate Balance for everything else
{
custom.Testing_Balance__c =sqlResult[0].UnitPrice * custom.Total_Hours__c;
}
}
}
sqlResult[0] does not have any value which is why I believe you are seeing this error
You can do a size check here
I strongly recommend adding a try/catch as well in the code as described in the documentation here (https://help.salesforce.com/HTViewSolution?id=000063739)
Let me know if this helps