You need to sign in to do that
Don't have an account?
Zoom_V
If there a better method than Upsert for this code ?
I have an AFTER INSERT, AFTER UPDATE trigger which Upserts a new record for each value in a multi-value field. It is also putting a value from a field in the current record (Vendor__c) into one of the fields of the newly created records (VendorNewRecord__c). If a new value is ever added to the multi-value field then it will create a new record for that value using the Upsert. I'm using Upsert because I don't want to create duplicate records every time the record is saved.
However, I don't want it to update the records which have already been made. I only want it to use Upsert to make sure it has already created a record for the value in the multi-value field.
Is there a way to use Upsert to only check if there is already a record made, but not actually update it ?
Here is the actual code which I have for this task :
Thank you very much for any help you can give.
However, I don't want it to update the records which have already been made. I only want it to use Upsert to make sure it has already created a record for the value in the multi-value field.
Is there a way to use Upsert to only check if there is already a record made, but not actually update it ?
Here is the actual code which I have for this task :
trigger AutoCreateSubsServOnContrOv on Policy_Profile__c (After insert, after update) { List<Product_Affected_Entry__c> subs = new List<Product_Affected_Entry__c>(); List<String> subAccNames=new List<String>(); for (Policy_Profile__c newCont : Trigger.New) { if (newCont.Products_Affected3__c != '[]') { // split out the multi-select picklist using a comma delimiter System.debug('Products_Affected3__c ' + newCont.Products_Affected3__c); String temp = newCont.Products_Affected3__c; temp.normalizeSpace(); temp = temp.replace(']',''); temp = temp.replace('[',''); String[] all = temp.split(','); subAccNames.addAll(all); for (String acctName : all) { subAccNames.add(acctName.normalizeSpace()); } } } // get the ids for all vendor products and store in a map keyed by name Map<String, Id> subAccIdsByName=new Map<String, Id>(); System.debug('FIRSTsubAccIdsByName='+subAccIdsByName); for (Vendor_Product__c subacc : [select id, Name from Vendor_Product__c where Name in :subAccNames]) { subAccIdsByName.put(subacc.Name, subacc.id); } //For each position processed by the trigger, add a new //Product_Affected_Entry__c record for the specified Products_Affected3__c. //Note that Trigger.New is a list of all the new positions //that are being created. for (Policy_Profile__c newContract : Trigger.New) { if (newContract.Products_Affected3__c != '[]') { // split out the multi-select picklist using a comma delimiter String temp = newContract.Products_Affected3__c; //temp.normalizeSpace(); temp = temp.replace(']',''); temp = temp.replace('[',''); String[] all = temp.split(','); for(String productsonpolicy: all) { productsonpolicy = productsonpolicy.normalizeSpace(); System.debug('productsonpolicy in last FOR loop making the NEW records FOR*****'+productsonpolicy); //for(String productsonpolicy: newContract.Products_Affected3__c.split(',')){ Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c( Policy__c = newContract.Id, VendorNewRecord__c = newContract.Vendor__c, Vendor_Product__c = subAccIdsByName.get(productsonpolicy), // GET THE SUB ACCOUNT ID BASED ON NAME Policy_and_Product__c = newContract.Name + '~' + subAccIdsByName.get(productsonpolicy)); subs.add(ssoc); } } } upsert subs Policy_and_Product__c; }
Thank you very much for any help you can give.
The way your code flow looks to do
- Create one Product_Affected_Entry__c record for the Policy_and_Product__c
- If record already exits then update it
I am sure Policy_and_Product__c is an externalId field.If your requirement is to stop the update and only to do insert then you have to
- Query the Product_Affected_Entry__c from database
- Create a map of Policy_and_Product__c and respective Product_Affected_Entry__c
- Use the map to find if record exists or not
- If exists then do not do any thing
- If not in map then add to list of records to be inserted.
See similar example where external id is used for upsert if you need to understad:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm
Thanks
Shashikant
Shanshikant - yha Policy_and_Product__c is an externalID field.
After reading the examples you showed I am still unsure how to do it. I was hoping one of those examples would show how to properly do an IF exists then do not do anything type of line in there....
Yes, there is a way if you don't want to update the already created records and only want to insert the new ones.
You can use the External Id field to find out which records are already present or created and which ones are new i.e. needs to be created and then insert the new records instead of upserting.
I have modified the Trigger and explained what I have done. Please find below the Trigger:
Please let me know if this helps.
If yes, please mark the Question as Solved.
Thanks and Regards,
Anirudh Singh