You need to sign in to do that
Don't have an account?

How to create a custom object from a related standard object using a Apex trigger?
When an opportunituy is changed to 'Closed Won' we would like to have a new custom object, Location_Status__c, created. I have included my trigger so far. When I save it I dont have any errors and am able to update/create Opportunities without any errors, but the custom object is not created. This code is specific for an Opportunity that is located in Albuquerque NM and hase the StageName of 'Closed Won'.
Thanks!
Thanks!
trigger locStatusOpp on Opportunity (after update) { for (Opportunity opp : Trigger.new) { List<Location_Status__c> loc = [SELECT Id, On_List__c, Location__c, Vendor_Type__c, OpportunityR__c FROM Location_Status__c]; for (Location_Status__c locLoop : loc) if (opp.StageName == 'Closed Won' || opp.Vendors__c == 'Bartender' || opp.Location__c == 'Albuquerque NM'){ Location_Status__c locNew = new Location_Status__c( Name = opp.Name, Location__c = 'Albuquerque NM', Vendor_Type__c = 'Bartender', OpportunityR__c = opp.Id); } insert loc; } }
I have it workign now, I used a little bit of what you both suggested, but I think the biggest thing was that I didnt have my trigger activated....
Rookie mistake of the year....
All Answers
Try this.
If loc != Null then Update loc else Insert.
Thanks for the suggestion. I tried yours and I am still not creating a new Location_Status__c object.
I have it workign now, I used a little bit of what you both suggested, but I think the biggest thing was that I didnt have my trigger activated....
Rookie mistake of the year....