1

I am new to triggers and I'm working on a trigger that will

  1. On new Project create,
  2. No other criteria Actions:
  3. Follow the Account lookup field on the Project to update the Account.“Most Recent Project” lookup field with the new project
  4. Follow the Wind Energy Site lookup to update the Account. “Most Recent Project” lookup field with the new project

I havnen't had any luck. In what ways can this be accomplished?

Thank you in advance! Wayne

Below is what I had before... I also began with workflows, but couldn't seem to update a lookup field via a workflow. I'm going through Apex workbook currently.

trigger ProjContactUpdateAccount on Account (after insert) {

    for (Oppotunity p: Trigger.new)      
        if (p.Most_Recent_Project__c != null) m.put(p.Account__c, p.Id);

}

Thanks a lot for this, I've edited your suggestion into

// After an insert the Project objects will have a ID values
trigger ProjContactUpdateAccount on Opportunity (after insert) {

    Map<Id, Id> m = new Map<Id, Id>();
    // Triggers need to be written to work on many objects (bulk case)
    for (Opportunity p : Trigger.new) {
        // Record the Account IDs and the corresponding Project IDs in a map
        if (p.Account__c != null) m.put(p.Account__c, p.Id);
        if (p.Wind_Energy_Site_Name__c != null) m.put(p.Wind_Energy_Site_Name__c, p.Id);
    }

    // This is a case where a query of Accounts isn't needed, just an update
    Account[] accounts = new Account[] {};
    for (Id id : m.keySet()) {
        // Update the right Account setting the reverse lookup to point to the Project.
        // All done with ID values from the map.
        accounts.add(new Account(
                Id = id,
                Most_Recent_Project_del__c = m.get(id)
                ));
    }
    update accounts;
}

And am getting an error on a non-related trigger and class on deployment... Thoughts?

1
  • 3
    Please show us the code you've got now. Stack Exchange users won't generally write code for you from scratch, but are often willing to help others debug.
    – Jagular
    Dec 8 '14 at 20:11
1

As your title mentions Opportunity but the body of the text talks about "Project create" exactly which object the trigger should work on isn't too clear.

But (in violation of the general principle that Jagular mentions) here is some commented code that should help you:

// After an insert the Project objects will have a ID values
public MyTrigger on Project__c (after insert) {

    Map<Id, Id> m = new Map<Id, Id>();
    // Triggers need to be written to work on many objects (bulk case)
    for (Project__c p : Trigger.new) {
        // Record the Account IDs and the corresponding Project IDs in a map
        if (p.Account__c != null) m.put(p.Account__c, p.Id);
        if (p.WindEnergySite__c != null) m.put(p.WindEnergySite__c, p.Id);
    }

    // This is a case where a query of Accounts isn't needed, just an update
    Account[] accounts = new Account[] {};
    for (Id id : m.keySet()) {
        // Update the right Account setting the reverse lookup to point to the Project.
        // All done with ID values from the map.
        accounts.add(new Account(
                Id = id,
                MostRecentProject__c = m.get(id)
                ));
    }
    update accounts;
}

So I suggest you try something of this pattern.

2
  • I was not aware of that rule. I will respect that from now on, thanks! The body refers to project which is just a pseudonym for opportunity in our org.
    – wayne-m
    Dec 8 '14 at 20:30
  • @wayne-m So substitute object names as required, and also probably field names as they may be different too. I took you at your word that you "havnen't had any luck"; the sort of questions that get a bad response are ones that are blatantly a copy of a requirement where the asker has done nothing to figure it out themselves. This isn't the easiest of triggers to start with.
    – Keith C
    Dec 8 '14 at 20:34

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.