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
Cody KindschuhCody Kindschuh 

Apex Trigger to create custom object, just once!

I am fairly new with apex triggers, and I have written a basic trigger to create a Vendor Details page when the account type = Vendor.  The problem is I only want it to do this once, and if someone were to change the type to something else, and back to Vendor, it creates the page again.

here is my trigger so far:

trigger AutoCreateVendorDet on Account (after update) {
for (Account a : Trigger.new) {
    if (a.Type__c == 'Vendor') {
        // Create a new Vendor Details Page
        Vendor_Details__c v = new Vendor_Details__c();
            v.Name = 'Vendor Information';
            v.Account__c = a.Id;
            insert v;
            }
    }
}
Best Answer chosen by Cody Kindschuh
kk
Thanks Cody,

One more thing I forgot to say, don't write DML(Insert/Upsert) statement inside a For loop which hit salesforce  limit. Please create a list for Vendor_Details__c and upsert it outside of the loop

trigger AutoCreateVendorDet on Account (after update) {
list<Vendor_Details__c> vendorList = new list<Vendor_Details__c>();
for (Account a : Trigger.new) {
    if (a.Type__c == 'Vendor') {
        // Create a new Vendor Details Page
        Vendor_Details__c v = new Vendor_Details__c();
            v.Name = 'Vendor Information';
            v.Account__c = a.Id;
v.External_ID__c = a.Id
vendorList.add(v);
            }
    }
if (vendorList.size()>0)
{Upsert v External_ID__c;}
}

All Answers

kk
Hi Cody,

I would suggest to create External ID field in Vendor_Details__c object with value as "Account ID".
insted of using insert statement in trigger use Upsert statement. Hence it won't create duplicate Vendor Details for an Account.

trigger AutoCreateVendorDet on Account (after update) {
for (Account a : Trigger.new) {
    if (a.Type__c == 'Vendor') {
        // Create a new Vendor Details Page
        Vendor_Details__c v = new Vendor_Details__c();
            v.Name = 'Vendor Information';
            v.Account__c = a.Id;
v.External_ID__c = a.Id
            Upsert v External_ID__c;

            }
    }
}
Cody KindschuhCody Kindschuh
Thanks, this worked perfectly!
kk
Thanks Cody,

One more thing I forgot to say, don't write DML(Insert/Upsert) statement inside a For loop which hit salesforce  limit. Please create a list for Vendor_Details__c and upsert it outside of the loop

trigger AutoCreateVendorDet on Account (after update) {
list<Vendor_Details__c> vendorList = new list<Vendor_Details__c>();
for (Account a : Trigger.new) {
    if (a.Type__c == 'Vendor') {
        // Create a new Vendor Details Page
        Vendor_Details__c v = new Vendor_Details__c();
            v.Name = 'Vendor Information';
            v.Account__c = a.Id;
v.External_ID__c = a.Id
vendorList.add(v);
            }
    }
if (vendorList.size()>0)
{Upsert v External_ID__c;}
}
This was selected as the best answer