0

My goal is to enable my team to update a particular field on the Opportunity Object (Stage__c) to 'Closed Won', and have that auto-generate a new custom child object (Onboarding) that is linked to the same account that the Opportunity is linked to.

At this point, I have produced the following trigger but am continuously prompt with

"Error: Compile Error: Invalid field Stage__c for SObject Opportunity at line 6 column 17"

Below is my entire trigger. Any thoughts/advice are greatly appreciated!

trigger CreateOB on Account (after insert, after update) {
    Onboarding__c[] inserts = new Onboarding__c[] {};
    for (Opportunity a : Trigger.new) {
        if (a.Stage__c == 'Closed Won') {
            Account old = Trigger.oldMap.get(a.Id);
            if (a.Stage__c != old.Stage__c) {
                inserts.add(new Onboarding__c(Account_Name__c = a.Id));
            }
        }
    }
    insert inserts;
}
1

Your Trigger is on account but you are Assigning trigger.new to the Opportunity sObject in your for loop

The error means there is no field named Stage__c on the Opportunity Object

Are you sure you have created a custom field named Stage? Or are you trying to update the standard field Stage. If the latter, the API name of the standard Field is StageName

It may also be fixed by changing Opportunity to Account in the for loop as that may be what you meant to have. (Only if Stage__c exists on Account

  • Hi Eric, Thanks for all of your help on this! I updated my trigger based on your comments above and below but now I am seeing a new error. You were correct that I was using the wrong field value, mental hiccup I suppose. Now the problem seems to be that I am attempting to create an Onboarding page (custom child obj) from a field change on the Opportunity page (child obj) and have the new Onboarding page linked to the same Account that the Opportunity was linked to. In my next comment will be the code I currently have and the error I am receiving. – Dave Dec 4 '14 at 20:57
  • trigger CreateOB on Account (after insert, after update) { Onboarding__c[] inserts = new Onboarding__c[] {}; for (Opportunity a : Trigger.new) { if (a.StageName == 'Closed Won') { Opportunity old = Trigger.oldMap.get(a.Id); if (a.StageName != old.StageName) { inserts.add(new Onboarding__c(Account_Name__c = a.Id)); } } } insert inserts; } – Dave Dec 4 '14 at 20:58
-1

Since your trigger is on the Account object, your trigger.new list will contain a list of accounts that the trigger is running on. So you're basically trying to check the Stage__c field on the Account object, which doesn't exist.

Not sure what your process is, but 2 options are available here:

1) Change your trigger to be on Opportunity

2) Replace trigger.new with a SOQL query in order to obtain the opportunity's Stage__c field:

for (Account a : [SELECT Id, Opportunity.Stage__c FROM Account WHERE Id IN :trigger.newMap.keySet()])

and then

if (a.Opportunity.Stage__c == 'Closed Won') // your code here

  • Even so, the compiler did not get to the part about the invalid assignment to opportunity. It is stuck on the fact that Stage__c is not a valid field for Opportunity so your a.Opportunity.Stage__c should still produce the same error. Not to mention you cannot access Opportunity like you are doing in the query – Eric Dec 4 '14 at 1:46
  • Can you please check the field level security on that field and make sure it's visible for the profile? – Boris Bachovski Dec 4 '14 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.