You need to sign in to do that
Don't have an account?
Nitin sharma 425
After Update trigger issue,Having an issuse with the if statement.
Hi All,
After update trigger on the opportunity Object.
If the Title__c field on the Opportunity object have CEO as the title then I should be able to update countofCEO field on the Account object
So when a user update the title__ field from CEO to Blank then counter on the Account object should decrease by 1 and vice versa
When I Update from CEO to Blank ,code run's fine but when I update same record and change Title field from blank to CEO then I get null pointer Exception and am not sure how to remove it.
Exception as per Salesforce.
CountOfOppwithCEOTitle: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.CountOfOppwithCEOTitle: line 77, column 1
Below given line is the culprit.
As per the debug logs ,Below given line is an issue
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null)
if(trigger.isUpdate&& trigger.IsAfter)
{
Set<id>Accids=new set<id>();
Map<id,opportunity>mapofOppIdtoOppRecords=new map<id,opportunity>();
for(opportunity opp:trigger.new)
{
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null)
//if(trigger.newmap.get(opp.id).title__c!=trigger.oldmap.get(opp.Id).title__c && (Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null))
{
system.debug('I am in the initial for loop');
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
else
if(opp.title__c!=trigger.oldmap.get(opp.Id).title__c && (String.isBlank(Trigger.oldmap.get(opp.id).title__c)&& opp.title__c.contains('CEO')))
{
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
}
Map<id,Account>MapofAccountIdToAccount=new map<id,account>([select id,countofCEO__c from account where id in:Accids]);
List<opportunity>Opp=[Select id,title__c,Accountid from Opportunity where id in:mapofOppIdtoOppRecords.keyset()];
list<account>acclist=new list<account>();
for(Opportunity Opprecords:opp)
{
system.debug('I am in for loop');
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c==null)
{
system.debug('The debug value is');
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c-1;
acclist.add(acc);
}
else
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c.contains('CEO'))
{
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c+1;
acclist.add(acc);
}
}
try
{
if(!acclist.isEmpty()&& acclist.size()>0)
{
Map<id,Account>MapofAccountIdToOpp=new map<id,account>();
MapofAccountIdToOpp.putall(acclist);
update MapofAccountIdToOpp.values();
}
}
catch(DmlException de)
{
}
}
Thanks
After update trigger on the opportunity Object.
If the Title__c field on the Opportunity object have CEO as the title then I should be able to update countofCEO field on the Account object
So when a user update the title__ field from CEO to Blank then counter on the Account object should decrease by 1 and vice versa
When I Update from CEO to Blank ,code run's fine but when I update same record and change Title field from blank to CEO then I get null pointer Exception and am not sure how to remove it.
Exception as per Salesforce.
CountOfOppwithCEOTitle: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.CountOfOppwithCEOTitle: line 77, column 1
Below given line is the culprit.
As per the debug logs ,Below given line is an issue
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null)
if(trigger.isUpdate&& trigger.IsAfter)
{
Set<id>Accids=new set<id>();
Map<id,opportunity>mapofOppIdtoOppRecords=new map<id,opportunity>();
for(opportunity opp:trigger.new)
{
if(Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null)
//if(trigger.newmap.get(opp.id).title__c!=trigger.oldmap.get(opp.Id).title__c && (Trigger.oldmap.get(opp.id).title__c.contains('CEO')&& opp.title__c==null))
{
system.debug('I am in the initial for loop');
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
else
if(opp.title__c!=trigger.oldmap.get(opp.Id).title__c && (String.isBlank(Trigger.oldmap.get(opp.id).title__c)&& opp.title__c.contains('CEO')))
{
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
}
Map<id,Account>MapofAccountIdToAccount=new map<id,account>([select id,countofCEO__c from account where id in:Accids]);
List<opportunity>Opp=[Select id,title__c,Accountid from Opportunity where id in:mapofOppIdtoOppRecords.keyset()];
list<account>acclist=new list<account>();
for(Opportunity Opprecords:opp)
{
system.debug('I am in for loop');
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c==null)
{
system.debug('The debug value is');
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c-1;
acclist.add(acc);
}
else
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c.contains('CEO'))
{
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c+1;
acclist.add(acc);
}
}
try
{
if(!acclist.isEmpty()&& acclist.size()>0)
{
Map<id,Account>MapofAccountIdToOpp=new map<id,account>();
MapofAccountIdToOpp.putall(acclist);
update MapofAccountIdToOpp.values();
}
}
catch(DmlException de)
{
}
}
Thanks
You need to put a null check before checking contains.