2

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.

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.

trigger CreateOB on Opportunity (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;
}

I am able to save the code but am receiving the following error on the Opportunity page when I attempt to submit it with the StageName = Closed Won:

Error:Apex trigger CreateOB caused an unexpected exception, contact your administrator: CreateOB: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Account Name: id value of incorrect type: 00611000007THcnAAG: [Account_Name__c]: Trigger.CreateOB: line 11, column 1

3
  • 1
    If you haven't checked out the process builder yet, you might want to consider using that to accomplish this task vs a trigger.
    – Jenny B
    Commented Dec 5, 2014 at 1:17
  • Hi Jenny, I actually have not had a chance to check this out yet, though everyone else seems to have informed that this apex trigger is my only option. Could I bother you for just a bit more insight into how I could utilize this feature for my need?
    – Dave
    Commented Dec 5, 2014 at 23:28
  • I added an answer with screenshots of how you could do it with the process builder.
    – Jenny B
    Commented Dec 6, 2014 at 3:27

2 Answers 2

5

Instead of:

inserts.add(new Onboarding__c(Account_Name__c = a.Id));

you probably need:

inserts.add(new Onboarding__c(Account_Name__c = a.AccountId));

(assuming Onboarding__c.Account_Name__c is a lookup to objects of type Account).

Probably worth changing the variable from a to o to make the code a bit clearer.

2

An alternative to this trigger is to use the new process builder, you should be able to open a case with SalesForce and ask for it to be turned on in your org, it's still beta. Once it is turned on you will find "Processes" on the setup page under the "Workflow and Approvals" menu.Workflow Menu
I stepped through creating a related task similar to what you would do for the onboarding record.
Click the "New Process" button and give it a name/description.New Process
Click on the "Add Object" component and select the Opportunity object.Add Object
Enter your criteria similar to a workflow ruleCritera
Then click on the "Immediate Action" component and select create new record and prepopulate the fields. Clicking on the pencil next to the field allows you to use another field's values.enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .