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 

Apex trigger on Opportunity to auto populate lookup field for custom object

Hi,

I'm new to developing in general and I am trying to create a trigger that updates the custom lookup field Bid_Name__c on the standard Opportunity object. I have a created a custom object called Bid__c which has a related one-to-one relationship with the Opportunity. I'm getting an error when attempting to save the Opportunity record, although it mentions the correct reference below but doesnt populate the field. My code is pasted below the error message.

System.StringException: Invalid id: Bid-1312: Trigger.UpdateBid: line 9, column 1

trigger UpdateBid on Opportunity (before insert, before update) {
    if (trigger.isBefore) { 
        if (trigger.isInsert || trigger.isUpdate) {
            //list Bids
            list<Bid__c> Bid = [SELECT ID, Name FROM Bid__c];
            for (Opportunity o : trigger.new) {
                o.Bid_Name__c = null;
                for (Bid__c b : Bid){
                    if (b.Name == o.Bid_Name__c) {
                        o.Bid_Name__c = b.Name;
                        break;
                    }
                }
            }
        }
    }
}

Any help that could be offered would be appreciated, struggling to work out what the solution here is.

Thank you
Best Answer chosen by Kev Brier
@anilbathula@@anilbathula@
Hi Kev Brier,

Then you need to write trigger on child object BID__c
Try this code hope it works ,sorry there may be some typo errors .
trigger UpdateBid on Bid__c (before insert, before update) {

Set<id>ids=new set<id>();
list<opportunity>oplst=new list<opportunity>();
	For(Bid__c b:trigger.new){
		if(b.opportunity__c!=Null){
			ids.add(b.opportunity__c); //lookup field to opportunity on bid__c
		}
	}
	if(!ids.Isempty()){
		list<opportunity >opp=[select id,name from opportunity where Bid_Name__C=Null AND id in:ids];
		for(opportunity op:opp){
		 
			for(bid__c bd: trigger.new){
				op.Bid_Name__c=b.id;
				oplst.add(op);
			}
		}
		update oplst;
	}   
}

Thanks
Anil.B

All Answers

@anilbathula@@anilbathula@
Hi Kev Brier,

Which is parent and which is child in your scenario?
 Firstly you will create opportunity then you created Bid__c when bid is created you want this bid name to be update in the lookup field on opportunity? this what you where looking for?

Thanks
Anil.B
 
Kev BrierKev Brier
Hi Anil, 

Thanks for the prompt response and your understanding of my requirements.

You're absolutely correct for the process - Parent = Opportunity  Child = Bid__c

Thanks,

Kev
@anilbathula@@anilbathula@
Hi Kev Brier,

Then you need to write trigger on child object BID__c
Try this code hope it works ,sorry there may be some typo errors .
trigger UpdateBid on Bid__c (before insert, before update) {

Set<id>ids=new set<id>();
list<opportunity>oplst=new list<opportunity>();
	For(Bid__c b:trigger.new){
		if(b.opportunity__c!=Null){
			ids.add(b.opportunity__c); //lookup field to opportunity on bid__c
		}
	}
	if(!ids.Isempty()){
		list<opportunity >opp=[select id,name from opportunity where Bid_Name__C=Null AND id in:ids];
		for(opportunity op:opp){
		 
			for(bid__c bd: trigger.new){
				op.Bid_Name__c=b.id;
				oplst.add(op);
			}
		}
		update oplst;
	}   
}

Thanks
Anil.B
This was selected as the best answer
@anilbathula@@anilbathula@
Hi Kev Brier,

Change the trigger event to after insert,after update

Thanks
Anil.B
Kev BrierKev Brier
Hi Anil,

Thanks for that - Might help if I put the trigger in the correct place :)

I'm now getting the error below

Error: Compile Error: Variable does not exist: b.id at line 15 column 32

trigger UpdateBid on Bid__c (after insert, after update) {

Set<id>ids=new set<id>();
list<opportunity>oplst=new list<opportunity>();
    For(Bid__c b:trigger.new){
        if(b.opportunity__c!=Null){
            ids.add(b.opportunity__c); //lookup field to opportunity on bid__c
        }
    }
    if(!ids.Isempty()){
        list<opportunity >opp=[select id,name from opportunity where Bid_Name__C=Null AND id in:ids];
        for(opportunity op:opp){
         
            for(bid__c bd: trigger.new){
                op.Bid_Name__c=b.id;
                oplst.add(op);
            }
        }
        update oplst;
    }   
}

I've bolded the section of the code it doesn't accept. Although the variable has been set at the top of the code.

Thanks,

Kev
 
Kev BrierKev Brier
Hi Anil,

Managed to get it working on the back of your code, little typo against the variable bd.Id.

All works perfectly and I've marked your response as a best answer.

Thanks for your time today

Kev
@anilbathula@@anilbathula@
Hi Kev Brier,

Cheers and thanks for the typo correction and marking it as best answer.

Thanks
Anil.B
 
Pankaj Chauhan 11Pankaj Chauhan 11
I have the similar requirement with Contract as an parent object with building section in it as a custom field which has a lookup relationship with custom object named Management.I want to autopopulate the field of Building section in contract object through the field of building section in management object.

i have write the code with the help of yours but it is not working.Kindly Help

trigger Management on Management__c (before insert, before update) {

    Set<id>ids=new set<id>();
list<contract>oplst=new list<contract>();
    For(Management__c b:trigger.new){
        if(b.Building_Section__c!=Null){
            ids.add(b.Building_Section__c); //lookup field to opportunity on bid__c
        }
    }
    if(!ids.Isempty()){
        list<contract >opp=[select id,name from contract where Building_Section__c=Null AND id in:ids];
        for(contract op:opp){
         
            for(Management__c bd: trigger.new){
                op.Building_Section__c=bd.id;
                oplst.add(op);
            }
        }
        update oplst;
    }   
}
 
sagar077sagar077
I need help to write a trigger for update the Source_Opportunity__c lookup field on (Opportunity Object) with the original Opportunity Name. The requirement is if the amendment contract is created then we have to update the Source_Opportunity__c lookup field on (Opportunity Object).