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
Kev BrierKev Brier 

Auto update Look-up field in Opportunity from Custom Object

Our custom object is the Bidding element of the Opportunity (which we call Bid__c) and is a related list against the standard opportunity object. On the Opportunity we have created a Lookup Relationship to the bid object, which needs to be auto popluated with the related bid number. We have in past used Workflow/Field Update to acheive this type of scenairo but obviously this doesnt work for Lookup Relationships. 

We have attempted to write our first trigger, we've got the stage that no error's are found but the trigger itself doesn't work. Code below:

trigger UpdateLookup on Opportunity (after insert, before update) 
{
    //instantiate set to hold unique Bid record ids
    Set<Id> BidIds = new Set<Id>();
    for(Opportunity s : Trigger.new)
    {
        BidIds.add(s.Bid_Name__c);
    }

    //instantiate map to hold deployment record Opportunity ids to their corresponding Bid Name
    Map<Id, Bid__c> BidIdsMap = new Map<Id, Bid__c>([SELECT Id, Name FROM Bid__c WHERE Id IN: BidIds]);

    for (Opportunity s : Trigger.new) 
    {
        if (s.Name == null && BidIdsMap.containsKey(s.Bid_Name__c)) 
        {
            s.Name = BidIdsMap.get(s.Bid_Name__c).Name;
        }
    }
}

We've kind of hit a brick wall and would really appreciate any support or guidance?

Opportunity Lookup Field - Bid_Name__c
Custom Object - Bid__c
Related field that we required to populate the lookup - Name (standard field)
Best Answer chosen by Kev Brier
BHinnersBHinners
Okay, I suggest that you trigger a change to the opportunity object based on the creation of a new bid related to that opportunity. So, Bid is the object for your trigger. Use it to access the related opportunity and update the bid field on that opportunity using the ID of the current bid. Then just save the updated opportunity. Something like: trigger UpdateLookup on Bid__c (after insert, before update) { Bid__c[] bids = Trigger.new; List updatedOpportunitiesList = new List(); For (Bids__c b : bids){ //get the opportunity for this bid b -- probably b.opportunity__c or similar. //write b.id to the bid__c field of the related opportunity something like b.opportunity__r.bid__c=b.id or similar //add the updated opportunity to a list like updatedOpportunitiesList above } Insert updatedOpportunitiesList; }

All Answers

BHinnersBHinners
You need to be able to get the related data for Opportunities, but you seem to be missing the code to do that.  Here is a blog post that describes using changing Opportunity related data based on changes to the Opportunity object: http://knthornt.wordpress.com/2011/07/29/updating-products-on-opportunity-close-date-change/

But that may not be exactly what you are trying to do, I amy not be understanding exactly what you need.  How does the business process work?  If I create or edit an Opportunity, what determines which single Bid record should be linked to that Opportunity?
Kev BrierKev Brier
Thanks for the response, probably my confusing explaination of the process. Business process is that the individual creates the Opportunity and then if a Bid is required, they create this on the custom bid object, which is a master detail relationship to the Opportunity. We've found that in order to pull infromation from both the Opportunity & Bid the master detail relationship will not suffice and we need a standard lookup relationship field - We would like the field to auto populate on the Opportunity screen with the related bid without the user having to manually update. 

Guess a trigger is the best way but our knowledge of Apex is very weak - Hopefully you may be able to provide some advice?
BHinnersBHinners
Between Bid and Opportunity, which is master and which is detail? Is there just one Bid record per Opportunity? Is there just one Opportunity record per Bid?
Kev BrierKev Brier
Opportunity is the master and Bid is the detail - Yes one bid record per Opportunity
BHinnersBHinners
The Master-Detail relationship you describe implies that you will have multiple bids per opportunity. If there is only one Bid per Opportunity, is there also only one Opportunity per Bid? If it is a one-to-one relationship, I think you want to use the "create new Bid" button on the Opportunity related list and then when the Bid is created have that new Bid trigger a field update on the Opportunity parent. For each Bid in your trigger, fetch the parent Opportunity and update the Bid field using the current Bid ID, then insert the updated Opportunity. Hope that helps!
Kev BrierKev Brier
You're correct its a one-to-one relationship with Opportunity & Bid. We use 'create new bid' when creating the Bid alongside the current Opportunity which prepopulates the Opportunity field within Bid creation.   

We have then created a standard lookup field on the Opportunity to create the final link with the Opportunity & Bid and this is where the trigger needs to update automatically - We have additional functionaility that works on the back of this link being put in place but dont want the user to manually update, potentially adding the wrong bid to Opportunity, ideally the trigger auto populates and then we hide the field from the user. 

In fairness we're probably out of our depth but need to figure this out for multiple reasons. Sorry to be pain 
BHinnersBHinners
Okay, I suggest that you trigger a change to the opportunity object based on the creation of a new bid related to that opportunity. So, Bid is the object for your trigger. Use it to access the related opportunity and update the bid field on that opportunity using the ID of the current bid. Then just save the updated opportunity. Something like: trigger UpdateLookup on Bid__c (after insert, before update) { Bid__c[] bids = Trigger.new; List updatedOpportunitiesList = new List(); For (Bids__c b : bids){ //get the opportunity for this bid b -- probably b.opportunity__c or similar. //write b.id to the bid__c field of the related opportunity something like b.opportunity__r.bid__c=b.id or similar //add the updated opportunity to a list like updatedOpportunitiesList above } Insert updatedOpportunitiesList; }
This was selected as the best answer