You need to sign in to do that
Don't have an account?
MaggieSumit
Getting NullPointerException oppTr: execution of AfterInsert caused by: System.NullPointerException
- trigger oppTr on Opportunity (after update,after insert){
- Map<Id,Opportunity> OppStage = new Map<Id,Opportunity>();
- List<Account> AccList = new List<Account>();
- for(Opportunity opp : trigger.new){
- if(opp.StageName == 'Closed Lost' && trigger.oldMap.get(opp.Id).StageName == 'Closed Won'){
- OppStage.put(opp.AccountId, opp);
- }
- }
- System.debug('OppStage'+OppStage);
- try {
- if(OppStage.size()>0){
- System.debug('OppStage'+OppStage.size());
- for(Opportunity opp2 : OppStage.values()){
- System.debug('opp2'+opp2);
- Account acc = new Account();
- acc.Id = opp2.AccountId;
- acc.Status__c = 'Lost';
- AccList.add(acc);
- System.debug('AccList'+acc.Status__c);
- }
- update AccList;
- System.debug('AccList'+AccList);
- }
- } catch (System.NullPointerException e){
- String s;
- s.toLowerCase();
- }
- }
The null pointer exception caused because Trigger.oldmap wont work with Insert trigger. So please update your code as below
Please mark this answer as best answer if it resolve your problem
All Answers
The null pointer exception caused because Trigger.oldmap wont work with Insert trigger. So please update your code as below
Please mark this answer as best answer if it resolve your problem
It is working good for update also. I tested it. Please make sure you are changing the "Closed Won" opportunity to "Closed Lost". Do not change the oppor to "Closed Lost" from other stage.
Thanks,
Kiruba. V
but thanks a lost it helps a lot. I mark as best answer.
This will solve
So, as per my requirement, your first code is correct, but the only thing needed to change like If second opp stage is any value and its change to "Closed Lost". then it will update the account status.
Thanks a lots. I will manage that.