You need to sign in to do that
Don't have an account?
Kim Amaral
Trying to write a Trigger where if the Product Name matches an existing record then populate information in certain fields
Currently my Marketing Manager exports a list of newly listed products and writes up the SEO on each. There are instances where we are receiving the same exact product to sell. Instead of her looking in our system and manually copying and pasting existing SEO I am looking for either a Validation Rule or Trigger to be set.
I would need that if there is an exact match on Product Name, then populate Marketing (Rich Test Area), Blurb (Text), and Keyword (Text) fields to the new product record. If there is a False given, leave those fields blank.
I tried doing a validation rule but it doesn't seem to be the right way.
I am not familiar with Apex Triggers but from what I've been reading it could work.
Thanks
I would need that if there is an exact match on Product Name, then populate Marketing (Rich Test Area), Blurb (Text), and Keyword (Text) fields to the new product record. If there is a False given, leave those fields blank.
I tried doing a validation rule but it doesn't seem to be the right way.
I am not familiar with Apex Triggers but from what I've been reading it could work.
Thanks
Hi Kim,
As per your requirement, Whenever any product is created, then it must check existing product name and if it match then populate certain fields on newly created product otherwise leave those field blank.
Below is code , please use this, Hopefully it will help you :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
So my next question is can this be a one time trigger. Product is created, the trigger is ran and never updated again?
Using as an example an Orange Pole as our product. If during spot check the marketing team sees that the one product named "Orange Pole" is actually a 6" not a 4" like the rest and she manually changes the Marketing blurb to indicate that it's a 6", will the trigger run again and overwrite it?
I don't foresee that happening but again don't want it to happen.
Error: Compile Error: Entity is not api accessible entity name: Product at line 1 column 27
Hi Kim,
Please use Product2 instead Product and let me know the result
Thanks
Hi Kim,
This trigger will only be fire on insert time, not update time. Yes this is a one time trigger. Product is created, the trigger is ran and never updated again.
Please let me know. in case of any help :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
trigger ProductTrigger on Product2 (before insert)
{
Map<String,Product2> mp_ProductMatch = new Map<String,Product2>();
Set<String> set_Name = new Set<String>();
for(Product prd : Trigger.new)
{
set_Name.add(prd.Name);
}
for(Product prd : [Select Id,Name from Product2 where Name IN:set_Name])
{
mp_ProductMatch.put(prd.Name,prd);
}
for(Product prd : Trigger.new)
{
if(mp_ProductMatch!=null && mp_ProductMatch.containsKey(prd.Name))
{
prd.Marketing = 'Rich Text Area Value';
prd.Blurb = 'Text Value';
prd.Keyword = 'Text Value';
}
}
}
Please replace Product by Product2 in code, please find below snippet : -
please let me know the result
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Trigger works but the result is not what I'm looking for.
For my sandbox example I put
Product Name: Sorvall ST16 Centrifuge
Marketing: This centrifuge is great.
Keywords: centrifuges, used centrifuges
Blurb: This centrifuge is great.
When I created a product with a duplicate Product name the results were
Marketing: Rich Text Area Value
Keywords: Text Value
Blurb: Text Value.
I was entering the FieldType of each Field when I specified Text value. I need the new product to be what's displayed on the existing record.
Hi Kim,
Please use below code and let me know :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Thank you so much. I'll move this into production!
I changed the first line to "trigger ProductTrigger on Product2 (before insert, after update)" in case a product is entered on the site and named "Centrifuge" but then it's realized later that its a Sorvall ST16 Centrifuge. I want it to recognize the match and fill in those fields.
In Sandbox, I created a product with just "centrifuge" as the title and I got an error message. So my other issue is that I still need the record to be created even if the criteria is not met. Those 3 fields are to stay blank if the result is FALSE.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger SYR_ItemProductSynchTrigger caused an unexpected exception, contact your administrator: SYR_ItemProductSynchTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 01tQ00000043qcDIAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ProductTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.ProductTrigger: line 20, column 1: []: Class.SYR_ItemProductSynchronizer.synchronizeProducts: line 71, column 1
Kim,
1.Use before context like : before insert and before update, your ReadOnly Exception error will resolve.
2. When criterion will not met, then Records will be create with blank values.
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Hi Kim,
Thank you, please let me know in case of any help :)
Please Mark as best answer :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com