
trigger CalucateNumberContact on Contact (After insert,After update,After delete) {
Set<Id> AccountIds=new Set<Id>();
Set<Id> oldAccountId=new Set<Id>();
List<Account> updateAccount=new List<Account>();
Integer count=0;
if(Trigger.isInsert||Trigger.isUpdate){
system.debug('tringger new-------------------------------------');
for(Contact accountdata:Trigger.new){
AccountIds.add(accountdata.AccountId);
}
List<Account> Listaccount=[select id,name,Number__c,secondryChildcount__c from Account where Id=:AccountIds];
system.debug('Listaccount==================================='+Listaccount);
for(Contact con1:Trigger.new){
if(con1.Role__c=='Secondary Operator'||con1.Role__c=='Secondary Echocardiologist'){
count++;
for(Account acc:Listaccount){
acc.secondryChildcount__c+=count;
}
count--;
}
if(con1.Role__c=='Primary Operator'||con1.Role__c=='Primary Echocardiologist'){
count++;
for(Account acc12:Listaccount){
acc12.Number__c+=count;
}
count--;
}
}
update Listaccount;
}
if(Trigger.isUpdate||Trigger.isDelete){
system.debug('old trigger------------------------------------------');
for(Contact accountdata:Trigger.old){
oldAccountId.add(accountdata.AccountId);
}
List<Account> Listaccount=[select id,Number__c,secondryChildcount__c from Account where Id=:oldAccountId];
for(Contact con1:Trigger.old){
if(con1.Role__c=='Secondary Operator'||con1.Role__c=='Secondary Echocardiologist'){
for(Account acc:Listaccount){
if(acc.secondryChildcount__c>0){
acc.secondryChildcount__c--;
}
}
}
if(con1.Role__c=='Primary Operator'||con1.Role__c=='Primary Echocardiologist'){
for(Account acc12:Listaccount){
if(acc12.Number__c>0){
acc12.Number__c--;
}
}
}
}
update Listaccount;
}
}
Please let me know. why i am unable to featch Number__c,secondryChildcount__c...this two fields
Thanks and Regards
salman
5 个回答
Hi Salman,
The main issue is that you are not giving a null check evrrytime you use any field.
for ex:
if(abc_c!=NULL)
apply such null check everywhere as a best practice to avoid null pointer exception,the error must be giving a line number as well,just add the debug there and debug it .
For any further Programmatic questions related to this, Please submit to the developer forums at
https://developer.salesforce.com
http://salesforce.stackexchange.com/
where the forums and participants are geared toward programming troubleshooting and support and the users there focus on code so you may get quicker answers as well as a wider variety of options.