You need to sign in to do that
Don't have an account?
GMASJ
Update value using map
Hi,
I am trying to update the billing country in account below is the trigger updating in AFTER update event.
But I am getting below error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()
Trigger.
Helpe Class
Please suggest me what is the issue with the code.
Thanks
Sudhir
I am trying to update the billing country in account below is the trigger updating in AFTER update event.
But I am getting below error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()
Trigger.
trigger AccountTrigger on Account (Before Insert, Before Update, After Insert, After Update ) { if(Trigger.isAfter){ OpportunityAccountUtils.ProcessAcctCountryCode(Trigger.newMap, Trigger.oldMap); } }
Helpe Class
public class OpportunityAccountUtils { public static void ProcessAcctCountryCode(Map<id, Account> newMap, Map<id, Account> oldMap) { List<Account> ActLst = new List<Account>(); Map<String, Country_Code__c> MapCountryCode = new Map<String, Country_Code__c>([SELECT Country_Code__c, Country_Name__c FROM Country_Code__c]); for(Account Act : newMap.values()){ Act.billingcountry = MapCountryCode.get(Act.billingcountry).Country_Code__c; ActLst.add(Act); } if(!ActLst.isEmpty()){ Update ActLst; } } }
Please suggest me what is the issue with the code.
Thanks
Sudhir
Thanks
Sudhir
All Answers
I think you need to make changes in your helper class. Please let me know if helped.
Regards,
Ramakant
Error: Compile Error: Method does not exist or incorrect signature: void put(String, String) from the type Map<String,Country_Code__c> at line 7 column 28
Later I changed it to as below
There was no error in code but I am getting error while updating the account.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()
I will be updating the billingcountry though which the code code should be updated in the same field Please let me know how to fix this issue.
Thanks
Sudhir
Please change your trigger as below:
Hope this will work.
Please mark as best answer if helped.
Regards,
Ramakant
Why are we removing After Insert, After update even from trigger.
If I removed event and run it not updating no changes.
If I included after insert, after update event I am still getting the same error.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()
Thanks
Sudhir
Right now i wrote a code with static values but you can replace it with your custom setting . Whole code is working in after event , here is the code
public class AccountTriggerHelper {
public static void updateAcc(Map<Id ,Account> newAccMap) {
if(newAccMap.size() > 0) {
List<Account> acc = new List<Account>();
for(Account ac : [Select Id , BillingCountry FROM Account WHERE Id IN: newAccMap.keySet()] ) {
if(ac.billingCOuntry == 'India') {
ac.billingCountry = 'USA';
acc.add(ac);
}
}
if(Acc.size() > 0)
update acc;
}
}
}
Thanks for you reply your code doesn't seems to be working I am setill having a readonly issue when trigger is fired.
When I run the below anonimious block it is workign fine when i convert it toa trigger it is not working
Thanks
Sudhir
I did not use custom setting have created a custom object and using it for country code
Trigger Helper Class
Tired using @Furture method its not working please suggest a best approach to achive
Thanks
Sudhir
Below mentioned code is working fine in my org paste it as it is but make the changes in your country code accordingly. I think from now you will not suffer any problem.
Trigger:--
trigger Trigger_Account on Account (Before Insert, Before Update,After Insert, After Update) {
// AccountTriggerHelper.updateAcc(Trigger.newMap);
/* set<id> actid = new set<id>();
for(Account Act: Trigger.new){
actid.add(act.id);
} */
if(Trigger.isAfter && Trigger.isInsert){
AccountTriggerHelper.ProcessAcctCountryCode(Trigger.newMap , Trigger.oldMap);
}
}
Helper class :--
public static void ProcessAcctCountryCode(Map<id, Account> newMap, Map<id, Account> oldMap) {
Map<String, String> MapCountryCode = new Map<String, String>();
List<Account> ActLst = new List<Account>();
MapCountryCode.put('India','91');
for(Account Act : [Select Id , BillingCountry FROM Account WHERE Id IN: newMap.keySet()]){
MapCountryCode.get(Act.BillingCountry);
Act.Billingcountry = MapCountryCode.get(Act.BillingCountry);
ActLst.add(Act);
}
Update ActLst;
}
Mark it best answer if it helps you.
Thanks
Sharad
Thanks for you reply there is no error in your code but it is still not updating the billingcountry since I am looking the same field to populate the code might be because of that it is not updating can you please suggest.
Trigger
Helper Class
Thanks
Sudhir
Thanks
Sudhir