function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LoganUTLoganUT 

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! 
 
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;
    }
}

 
Best Answer chosen by LoganUT
LoganUTLoganUT
Akhil Reddy and SalesforceSC Thanks for the help! 

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

Akhil ReddyAkhil Reddy
trigger locStatusOpp on Opportunity (after update) {
	List<Location_Status__c> loctoInsert  = new List<Location_Status__c>();
        for (Opportunity opp : Trigger.new) {
        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); 
			loctoInsert.add(locNew);
        }
        insert loctoInsert;
    }
}

Try this.
SalesforceSCSalesforceSC
At Line 11 try checking if loc != Null
If loc != Null  then Update loc else Insert.
LoganUTLoganUT
Akhil Reddy,

Thanks for the suggestion. I tried yours and I am still not creating a new Location_Status__c object.
 
LoganUTLoganUT
Akhil Reddy and SalesforceSC Thanks for the help! 

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.... 
This was selected as the best answer