You need to sign in to do that
Don't have an account?
Joy___
Update a custom field on Account object trigger
Hi,
I have pretty complicated situation here!
I'm trying to write a trigger, I have to update a custom field : Annual_Sales__C ( currency type ) on Account object when an order is updating
This is my code below :
trigger UpdateAnnualSalesTrigger on Order (after update) {
set <String> accID = New Set <String> ();
for (Order o: Trigger.new) {
if (o.AccountId != Null ) {
accID.add (o.AccountId);
}
}
for( Order o: Trigger.new){
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for (Account ac: [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) {
}
if (upAccList.size ()> 0)
update upAccList;
}
}
}
When I update an order, an error message is display :
caused by: System.NullPointerException: Attempt to de-reference a null object : ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;
Thanks :)
I have pretty complicated situation here!
I'm trying to write a trigger, I have to update a custom field : Annual_Sales__C ( currency type ) on Account object when an order is updating
This is my code below :
trigger UpdateAnnualSalesTrigger on Order (after update) {
set <String> accID = New Set <String> ();
for (Order o: Trigger.new) {
if (o.AccountId != Null ) {
accID.add (o.AccountId);
}
}
for( Order o: Trigger.new){
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for (Account ac: [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) {
ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;
UpAccList.add (ac); }
if (upAccList.size ()> 0)
update upAccList;
}
}
}
When I update an order, an error message is display :
caused by: System.NullPointerException: Attempt to de-reference a null object : ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;
Thanks :)
trigger UpdateAnnualSalesTrigger on Order (after insert ,after update ,after delete,after undelete) {
if(trigger.isafter&& trigger.isinsert &&trigger.isupdate){
set <String> accID = New Set <String> ();
for (Order order1: Trigger.new) {
if (order1.AccountId != Null ) {
accID.add (order1.AccountId);
}
}
for( Order ord: Trigger.new){
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for (Account ac: [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) {
ac.Annual_Sales__c = ac.Annual_Sales__c + ord.Total_Amount__c ;
UpAccList.add (ac);
}
try {
if (upAccList.size ()> 0)
update upAccList;
}Catch(DmlException e){
System.debug('The following error has occured '+ e.getMessage());
}
}
}
}
}
All Answers
Null pointer exceptions are usually thrown 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.
Salesforce recommends using the Safe Navigation Operator to Avoid Null Pointer Exceptions
As you might already know the error null pointer exception occurs when query doesn't return any rows. While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned. What's not obvious is that it also assumes that exactly one row is returned!
https://help.salesforce.com/HTViewSolution?id=000063739
Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
trigger UpdateAnnualSalesTrigger on Order (after insert ,after update ,after delete,after undelete) {
if(trigger.isafter&& trigger.isinsert &&trigger.isupdate){
set <String> accID = New Set <String> ();
for (Order order1: Trigger.new) {
if (order1.AccountId != Null ) {
accID.add (order1.AccountId);
}
}
for( Order ord: Trigger.new){
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for (Account ac: [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) {
ac.Annual_Sales__c = ac.Annual_Sales__c + ord.Total_Amount__c ;
UpAccList.add (ac);
}
try {
if (upAccList.size ()> 0)
update upAccList;
}Catch(DmlException e){
System.debug('The following error has occured '+ e.getMessage());
}
}
}
}
}
Thanks !! Great, it works now :)