You need to sign in to do that
Don't have an account?
Cross-Object Trigger
I'm having trouble with the following code. I keep getting the error that there is no viable alternative on the "for(integer i...) line.
trigger recipTechSurvey on Tech_Survey__c(after update, after insert){
List<Show__c> linkedShows = [SELECT id, Tech_Survey__c FROM Show__c WHERE Show__c.id IN :Trigger.newMap.keySet()];
for (Integer i = 0; i < Trigger.new.size(); i++) {
List<Show__c> showsToUpdate = new List<Show__c>{};
for(List <Show__c> ls: linkedShows, ){
if (Trigger.new[i].ShowNum__c == ls.id) {
ls.Tech_Survey__c= :Trigger.new[i].id;
showsToUpdate.put(ls.id, ls.Tech_Survey__c);
}
}
Database.update(showsToUpdate);
}
There are still some problems with your code.
Please note that your code is still not bug free but it will work for single record manipulations. Also, there are some improvements that can be made to get it more aligned to the best practises.
All Answers
Apex error messages can point you in the wrong direction at times. From experience I always look at the line before and the line after if I cannot see a problem with the line mentioned in the error message.
The line
List<Show__c> showsToUpdate = new List<Show__c>{};
needs to be changed to
List<Show__c> showsToUpdate = new List<Show__c>();
Also, the line
for(List <Show__c> ls: linkedShows, ){
needs to be changed to
for(Show__c ls: linkedShows){
Nope. Still not working. I'm trying to do something that seems like it should be incredibly simple. I have two custom objects that are linked by lookup, but cannot have a master-child relationship.
What I am trying to do is simply trigger a reciprocal update, so that if one object is selected on a lookup, the other object has the corresponding lookup field populated with the other's id.
Am I overengineering? Here's what I've got now, restructured.
1 trigger recipTechSurvey on Tech_Survey__c (after insert, after update) { 2 List<Show__c> linkedShows = [SELECT id, Tech_Survey__c FROM Show__c WHERE Show__c.id IN :Trigger.newMap.keySet()]; 3 Map<id,Show__c> showsToUpdate = new Map<id,Show__c>{}; 4 for (Integer i = 0; i < Trigger.new.size(); i++){ 5 for (Integer f = 0; f < linkedShows.size(); f++){ 6 if(Trigger.new[i].ShowNum__c == linkedShows[f].id) { 7 Show__c s = new Show__c(Tech_Survey__c=Trigger.new[i].id); 8 showsToUpdate.put(s.id, s); 9 } 10 } 11 } 12 update showsToUpdate.values(); 13
}
}
AND here's the test method:
static testMethod void testrecipTechSurvey(){
Show__c show = new Show__c(name='testShow', Show_Num__c='14-2000');
insert show;
Show__c setShow = [SELECT Id, Show_Num__c, Tech_Survey__c FROM Show__c WHERE Show_Num__c='14-2000'];
Tech_Survey__c survey = new Tech_Survey__c(ShowNum__c=setShow.id);
insert survey;
Tech_Survey__c ts = [SELECT id, ShowNum__c FROM Tech_Survey__c WHERE ShowNum__c= :setShow.id];
Show__c s = [SELECT Id, Show_Num__c, Tech_Survey__c FROM Show__c WHERE Show_Num__c='14-2000'];
System.assertEquals (s.Tech_Survey__c, ts.id);
}
There are still some problems with your code.
Please note that your code is still not bug free but it will work for single record manipulations. Also, there are some improvements that can be made to get it more aligned to the best practises.
Thanks so much man. Made some minor tweaks and overhauled the test methods and it works like a charm. Much appreciation!