You need to sign in to do that
Don't have an account?
Updating Opportunity from custom object in a trigger
I've gone round and round with trying to create a trigger on a custom object opportunitywwa__c that need to set the value of opportunity.has_wwa__c to true if it is currently false.
Opportunitywwa__c has a field named opportunity_name__c which is a lookup to opportunity which may or not be populated.
Since there can be one or more opportunitywwa__c records mapped to a single opportunity I only need to update the Opportunity.has_wwa__c field if the value is false.
Here is the code that I can't get to compile:
trigger updt_Opp_Has_OR3 on OpportunityWWA__c (after insert, after update) { Set<id> oppsToUpdate = new Set<id>(); //add the opportunityID from the lookup field into the set for (OpportunityWWA__c wwa : Trigger.new){ if (wwa.opportunity_name__c <> null){ oppsToUpdate.add(wwa.opportunity_name__c); } } //get list of Opportunities that need to be updated for(Opportunity opp : [SELECT Id, (select opportunity_name__c FROM OpportunityWWA__R where id IN : oppsToUpdate and opportunity_name__c <> null) from opportunity where has_ors__c = false]) { opp.has_www__c = true; } if(opp.size() > 0){ //I get error on this line that varialbel "opp" does not exists //What am I missing and will this work for bulk updates? update opp; } }
// When a ParnterOR is updated, this trigger sets the has_ors__c field to true on the associated Opp trigger updt_Opp_Has_OR on OpportunityWWA__c (after insert, after update) { //use set to force ids to be unique Set<id> checkForUpdate = new Set<id>(); //add the ID from the lookup field into the set for (OpportunityWWA__c wwa : Trigger.new){ if (wwa.opportunity_name__c <> null){ checkForUpdate.add(wwa.opportunity_name__c); } } //create object to hold records to be updated List<Opportunity> toUpdate = new List<Opportunity> {}; //get list of records that need to be updated for(Opportunity opp : [SELECT Id from opportunity WHERE id IN : checkForUpdate AND has_ors__c = false]) { //create records in List with Ids from query and set field value toUpdate.add(new Opportunity(Id = opp.Id, has_ors__c = true)); } if(toUpdate.size() > 0){ update toUpdate; } }
All Answers
Opp is a variable , not a list, so you can't check it's size...
Just to check - if the relationship to Opportunity is a Master-Detail, you can use workflow to do this...
But if not, your code still seems over complex - I don't think you need to query the related object in your second query - I think you can just do this:
trigger updt_Opp_Has_OR3 on OpportunityWWA__c (after insert, after update) {
Set<id> oppsToUpdate = new Set<id>();
//add the opportunityID from the lookup field into the set
for (OpportunityWWA__c wwa : Trigger.new){
if (wwa.opportunity_name__c <> null){
oppsToUpdate.add(wwa.opportunity_name__c);
}
}
List<Opportunity> oppupdate = new List<Opportunity> {};
//get list of Opportunities that need to be updated
for(Opportunity opp : [SELECT Id where id IN : oppsToUpdate AND has_ors__c = false]) {
oppupdate.add(new Opportunity(Id = opp.Id, has_www__c = true));
}
if(oppupdate.size() > 0){
update oppupdate;
}
}
// When a ParnterOR is updated, this trigger sets the has_ors__c field to true on the associated Opp trigger updt_Opp_Has_OR on OpportunityWWA__c (after insert, after update) { //use set to force ids to be unique Set<id> checkForUpdate = new Set<id>(); //add the ID from the lookup field into the set for (OpportunityWWA__c wwa : Trigger.new){ if (wwa.opportunity_name__c <> null){ checkForUpdate.add(wwa.opportunity_name__c); } } //create object to hold records to be updated List<Opportunity> toUpdate = new List<Opportunity> {}; //get list of records that need to be updated for(Opportunity opp : [SELECT Id from opportunity WHERE id IN : checkForUpdate AND has_ors__c = false]) { //create records in List with Ids from query and set field value toUpdate.add(new Opportunity(Id = opp.Id, has_ors__c = true)); } if(toUpdate.size() > 0){ update toUpdate; } }