function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Varsha JethwaniVarsha Jethwani 

How to access values of Account Object and Custom Object fields from Opportunity

I am trying to check the condition in my apex code, but it fails due to highlighted code. I am trying to access field NPCT_Recur_Donation_Opps_Total_Number__c from Opportunity's Account
and similarly below condition

Anyone let me know where I am wrong

for( Oppotunity opp: OppList)
{
if((opp.NPCT_Last_Reason_Code__c == NPCT_Constants.NPCT_Last_Reason_Code_AC01  ||                         (opp.Accounts.NPCT_Recur_Donation_Opps_Total_Number__c <= 1 ||                            opp.npe03__Recurring_Donation__r.NPCT_Consecutive_Reversals__c == 2))
.
.
.
}

Thanks in advance
Regards,
Varsha
AnkaiahAnkaiah (Salesforce Developers) 
Hi Varsha,

Directly you cant able to refer the account fields in opportunity trigger.new

Please try with below code.
Set<id> accIds = new set<id>();
Map<id,integer> accmap = new Map<id,integer>();
for(Opportunity opp: trigger.new){
if(opp.AccountId !=null){

accIds.add(opp.AccountId);
}

List<Account>  acclist = [select id,NPCT_Recur_Donation_Opps_Total_Number__c from Account where id=:accIds];
for(Account acc:acclist){

accmap.put(acc.id,acc.NPCT_Recur_Donation_Opps_Total_Number__c);

}

for(Opportunity opp: trigger.new){

If((accmap.contains(opp.Accountid)&& accmap.get(id)>=1) ||opp.npe03__Recurring_Donation__r.NPCT_Consecutive_Reversals__c == 2){
//process logic
}

}

}

If this helps, please mark it as best answer.

Regards,
Ankaiah Bandi