+ Start a Discussion
Awesome User11Awesome User11 

Variable does not exist: tc.Account _Name__c at line 13 column 58

I am putting together code for an update to a custom object (opportunity hierarchy) based on set fields (IDs) being provided in the opportunity object.
I am getting an error that my field Account_Name__c is not a variable the exists.  Any ideas?
 
trigger OpptyHierarchy on Opportunity_Hierarchy__c (before insert, before update) {
    Set<string> OpportunityID = new set<string>();
    for (Opportunity_Hierarchy__c tc : trigger.new) {
        if (tc.Account_Name__c != NULL) {OpportunityID.add(tc.Account_Name__c);
        }
    }
    Map<string, Opportunity> Opportunity = new Map <string, Opportunity>();
    for (Opportunity obj : [SELECT ID, AccountID, Main_Account__c, Enterprise_Account__c
                            from Opportunity
                            Where OpportunityID in: Opportunity]) {Opportunity.put(obj.Account_Name__c,obj);
    }
    for (Opportunity_Hierarchy__c t: trigger.new) {
        if (Opportunity.containsKey(tc.Account_Name__c)) tc.Account_Name__c = Opportunity.get(tc.Account_Name__c).ID;
        tc.Main_Account__c = Opportunity.get(tc.Account_Name__c).Main_Account__c;
        tc.Enterprise_Account__c = Opportunity.get(tc.Account_Name__c).Enterprise_Account__c;
    }
}

 
Virendra ChouhanVirendra Chouhan
HI,

In your for loop your are using tc as an trigger.new instance but in your loop your wrote only  instead of tc -
change it: like below
for (Opportunity_Hierarchy__c tc: trigger.new) {
        if (Opportunity.containsKey(tc.Account_Name__c)) <b><u>tc.Account_Name__c </u></b>= Opportunity.get(tc.Account_Name__c).ID;
        tc.Main_Account__c = Opportunity.get(tc.Account_Name__c).Main_Account__c;
        tc.Enterprise_Account__c = Opportunity.get(tc.Account_Name__c).Enterprise_Account__c;
    }

 
Awesome User11Awesome User11
Thanks!
Now I received another error that states:
Error: Compile Error: Invalid field Account_Name__c for SObject Opportunity at line 10 column 84
Virendra ChouhanVirendra Chouhan
Is your Opportunity object has Account_Name__c field? Please check.

 
Awesome User11Awesome User11
Hello I have changed it to AccountID instead, but received this error:
No such column 'OpportunityID' on entity 'Opportunity'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 28
Virendra ChouhanVirendra Chouhan
Because there is no such field name "OpportunityId" in opportunity object its only ID not OpportunityId.
[SELECT ID, AccountID, Main_Account__c, Enterprise_Account__c
                            from Opportunity
                            Where Id in: OpportunityID ]
If this is helpful for you please mark it as a Best answer. For other queries please post other question.

I suggest you please complete trailhead first. go here for trailhead;
https://trailhead.salesforce.com/

also if you want to lear Apex visit this site:
http://www.sfdc99.com/