You need to sign in to do that
Don't have an account?
Trigger to update Lookup
In our evironment, users can relate opportunities to another opportunity (add on sales) or to a 'vision' (custom object). There are two lookup fields on the opportunity to track this.
Source_Vision__c and Source_Opportunity__c
It is possible that an opportunity is created from another opportunity that was created from a vision. What I would like to do is create a trigger that will populate this relationship so that the newly created opportunity has both the relationship to the original vision, and the source opportunity.
trigger OpportunityTrigger on Opportunity (after insert) { for(Opportunity o:Trigger.new){
//Check if Source_Vision__c is null if(o.Source_Opportunity__r.Source_Vision__c.name != null ){
//If not, set Source_Vision__c on new record to Source_Vision of relaated record. o.Source_Vision__c = o.Source_Opportunity__r.Source_Vision__c;} } }
I am getting an invalid key relationship error (Opportunity.Source_Vision__c)
Am I missing something?
Thanks@
I think you need to get the list and need to update the opp
That should work!
Thanks,
I was able to modify the trigger to get it to validate, but am still having trouble getting the data.
The trigger is firing, but I can't seem to get to any data from the vision.
I did some testing and I am unable to use the customobject__r.customobject2__r.field structure. Is there another way to accomplish this?
Take that in a list and update the list.
Trigger will fire but you need to get the id and get the oopportunity in a List and update the List,intstead of directly assigning the value.
http://boards.developerforce.com/t5/Apex-Code-Development/Apex-trigger-to-update-parent-based-on-lookup-when-child-is/td-p/536937
TRY THIS IT WILL GET JOB DONE
Thanks, however I am not trying to update the parent in a parent-child relationship.
I simply want to take the ID from a lookup on Opportunity 1 to Opportunity 2 (which is also related to Opportunity 1).
Example:
I have a Vision, VisionONE which leads to an opportunity. Opportunity 1 has Source_Vision__c = VIsionONE. Opportunity 1 leads to Opportunity 2. Opportunity 2 has Source_Opportunity __c = Opportunity 1, but Source_Vision__c = blank.
I would like to set Opportunity2.Vision_Source__c to Opportunity2.Source_Opportunity__r.Source_Vision__c.
Would I have to query Opportunity 1 in the list? [Select Id, Source_Vision__c From Oppotunity where Id = o.Source_Opportunity__r.id ?
Thanks, and apologies if I am not understanding this correctly.
yes,
GET THE LIST and assign the values.
trigger test(before insert,before update){
Set<Id> oppId = new SEt<Id>();
for(Opportunity opp : trigger.new){
if(opp.Source_Opportunity__c != null){
oppId.add(opp.Source_Opportunity__c);
}
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Name,Source_Vision__c from Opportunity where Id in: oppId]);
for(opportunity opp1 : trigger.new){
if(oppMap.get(opp1.Id) != null){
opp1.Source_Vision__c =oppMap.get(opp1.Id).Source_Vision__c;
}
}
update oppMap.Values();
}
this will work
i guess
Thanks for your help, but this still does not appear to be working.
It looks like it is not adding the opportunity to the map as it is failing the if(oppMatp.get(opp1.Id) != null) comparison
Thanks, I tried rewriting the trigger with your suggestion, but I still cannot get it to work.
I think the steps would be something like this:
Check to see if new opportunity (oppNew) OppNew.SourceOpportunity__c is blank.
If not blank, add the SourceOpportunity__c to a map.
Query to find the source opportunity oppSrc and pull in oppSrc.Source_Vision__c
Set oppNew.Source_Vision__c to oppSrc.Source_Vision
Update
but I just cant seem to get it to work. I have read the suggested posts but still have no luck. Any help would be very much appreciated.
Got it to work.
However, my test class is failing.. I have it deployed in sandbox and it is working fine, but fails in the test class.