You need to sign in to do that
Don't have an account?
Kev 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
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
Then you need to write trigger on child object BID__c
Try this code hope it works ,sorry there may be some typo errors .
Thanks
Anil.B
All Answers
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
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
Then you need to write trigger on child object BID__c
Try this code hope it works ,sorry there may be some typo errors .
Thanks
Anil.B
Change the trigger event to after insert,after update
Thanks
Anil.B
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
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
Cheers and thanks for the typo correction and marking it as best answer.
Thanks
Anil.B
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;
}
}