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
pvandepvande 

Error: Compile Error: Variable does not exist

I am new to APEX coding and am struggling with the following error.  Any help would be apprecaited.
Error: Compile Error: Variable does not exist: Designated_Forecast_Account__c at line 5 column 1

My trigger:

trigger ForecastAccount on Opportunity (before insert, before update) {
 
if ( Project__r.Sponsor_Account__r.Forecast_Account__c  = True) {
 
Designated_Forecast_Account__c =  Sponsor_Account__r.Id ;
 
} else if
 
(  Project__r.Sponsor_Account__r.Forecast_Account__c  = False){
 
Designated_Forecast_Account__c = Sponsor_Account__r.Reporting_Parent__c ;
 }
}
 
Best Answer chosen by pvande
HARSHIL U PARIKHHARSHIL U PARIKH
I would suggest to take following path,

1) First make a formula field on Project__c object named "Forcast Account Checkbox" and it will simpley be checked when Forcast_Account__c is checked.
2) Now we are going to make a use of that field in trigger:
Trigger ForecastAccount on Opportunity (before insert, before update) 
{
    For(Opportunity Opp : Trigger.New)
    {
        If(Opp.Project__c != null)
        {
            
            List<Opportunity> comingOppInfo = [Select Id, Designated_Forecast_Account__c, Project__r.Id, 
                                                                Project__r.Forcast_Account_Checkbox, Project__r.Sponser_Account__c, Project__r.Reporting_Parent__c
                                                                    FROM  Opportunity WHERE Id =:Opp.Id LIMIT 1];
                                                                    
            If(comingOppInfo[0]. Project__r.Forcast_Account_Checkbox == TRUE)
            {
                comingOppInfo[0].Designated_Forecast_Account__c = comingOppInfo[0].Project__r.Sponser_Account__c;
            }
            
            Else If(comingOppInfo[0]. Project__r.Forcast_Account_Checkbox == FALSE)
            {
                comingOppInfo[0].Designated_Forecast_Account__c = comingOppInfo[0].Project__r.Reporting_Parent__c;
            }                                                        
                                    
        }
    }
 
}
Hope this helps!

 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Can you give your requirement in depth? I mean what the data model here? How many objects are there and how do they relate to each other as well as what is that you are looking forward to have automated?

the very first thing I can point out is that you need to use both of those IF statements under the Trigger.New keyword since that way trigger would run for all the opportunity records.

This is should be a starting point in my opinion:
Trigger ForecastAccount on Opportunity (before insert, before update) 
{
    For(Opportunity Opp : Trigger.New)
    {
        // All the logic goes here..
    }
 
}
Hope this helps!

 
pvandepvande
Govind, thank you for looking at our question. .  I have  incorporated your suggestion about Trigger.New, but am still receiving errors.  Here are the answers to your questions.

Account > Project __c > Opportunity 
The trigger is on the opportunity.  We are trying to access account information on the custom object of Project.  Project is related to account via a custom field called Sponsor_Account__c.  We are trying to retrieve the boolean field Forecast_Account__c on the Account; if true then we will  insert the account ID into the custom field Designated_Forecast_Account__c that resides on the opportunity.
om gupta(sfdc)om gupta(sfdc)
hi i think that Designated_Forecast_Account__c  will be like opportunityObj.Designated_Forecast_Account__c  and if that not work plz check the permission on that field for that field . you may get the result
HARSHIL U PARIKHHARSHIL U PARIKH
I would suggest to take following path,

1) First make a formula field on Project__c object named "Forcast Account Checkbox" and it will simpley be checked when Forcast_Account__c is checked.
2) Now we are going to make a use of that field in trigger:
Trigger ForecastAccount on Opportunity (before insert, before update) 
{
    For(Opportunity Opp : Trigger.New)
    {
        If(Opp.Project__c != null)
        {
            
            List<Opportunity> comingOppInfo = [Select Id, Designated_Forecast_Account__c, Project__r.Id, 
                                                                Project__r.Forcast_Account_Checkbox, Project__r.Sponser_Account__c, Project__r.Reporting_Parent__c
                                                                    FROM  Opportunity WHERE Id =:Opp.Id LIMIT 1];
                                                                    
            If(comingOppInfo[0]. Project__r.Forcast_Account_Checkbox == TRUE)
            {
                comingOppInfo[0].Designated_Forecast_Account__c = comingOppInfo[0].Project__r.Sponser_Account__c;
            }
            
            Else If(comingOppInfo[0]. Project__r.Forcast_Account_Checkbox == FALSE)
            {
                comingOppInfo[0].Designated_Forecast_Account__c = comingOppInfo[0].Project__r.Reporting_Parent__c;
            }                                                        
                                    
        }
    }
 
}
Hope this helps!

 
This was selected as the best answer
pvandepvande
Govind, thank you very much for your help with this problem.  I really appreciate your assistance with this code!