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
Kim AmaralKim 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 
rajat Maheshwari 6rajat Maheshwari 6

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 :)

 

trigger ProductTrigger on Product (before insert)
  {
    Map<String,Product> mp_ProductMatch = new Map<String,Product>();
  Set<String> set_Name = new Set<String>();
  
   for(Product prd : Trigger.new)
       {
          set_Name.add(prd.Name);
      }

for(Product prd : [Select Id,Name from Product 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';
         }
   }

}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Kim AmaralKim Amaral
Thank you Rajat. 

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. 
Kim AmaralKim Amaral
I just tried this trigger (in Sandbox) and got this error

Error: Compile Error: Entity is not api accessible entity name: Product at line 1 column 27
rajat Maheshwari 6rajat Maheshwari 6

Hi Kim, 

Please use Product2 instead Product and let me know the result

Thanks

rajat Maheshwari 6rajat Maheshwari 6

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

Kim AmaralKim Amaral
I am still getting an error. Can you take a look?

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

}
 
rajat Maheshwari 6rajat Maheshwari 6
Kim,

Please replace Product by Product2 in code, please find below snippet : - 
trigger ProductTrigger on Product2 (before insert)
  {
    Map<String,Product2> mp_ProductMatch = new Map<String,Product2>();
  Set<String> set_Name = new Set<String>();
   
   for(Product2 prd : Trigger.new)
       {
          set_Name.add(prd.Name);
      }
 
for(Product2 prd : [Select Id,Name from Product2 where Name IN:set_Name])
   {
       mp_ProductMatch.put(prd.Name,prd);
   }
 
for(Product2 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 let me know the result 

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Kim AmaralKim Amaral
Thanks. I just had to change the prd.Marketing to prd.Marketing__c and same for the other two. 

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. 
rajat Maheshwari 6rajat Maheshwari 6

Hi Kim,

Please use below code and let me know :) 

 

trigger ProductTrigger on Product2 (before insert)
  {
    Map<String,Product2> mp_ProductMatch = new Map<String,Product2>();
  Set<String> set_Name = new Set<String>();
   
   for(Product2 prd : Trigger.new)
       {
          set_Name.add(prd.Name);
      }
 
for(Product2 prd : [Select Id,Name, Marketing,Blurb,Keyword from Product2 where Name IN:set_Name])
   {
       mp_ProductMatch.put(prd.Name,prd);
   }
 
for(Product2 prd : Trigger.new)
   {
      if(mp_ProductMatch!=null && mp_ProductMatch.containsKey(prd.Name))
         {
             prd.Marketing =mp_ProductMatch.get(prd.Name).Marketing;
             prd.Blurb = mp_ProductMatch.get(prd.Name).Blurb;
             prd.Keyword = mp_ProductMatch.get(prd.Name).Keyword ;
         }
   }

}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Kim AmaralKim Amaral
This worked. And I notice that it looks back to the last created record that has the same name. My last created record had Rich Area text, text value, text value. So I deleted it and created a new product and it display from the "This centrifuge is great". A nice feature so that if there are any changes made the new records will show the updated SEO. 

Thank you so much. I'll move this into production!
Kim AmaralKim Amaral
Rajat, sorry I think I found a problem. 

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
rajat Maheshwari 6rajat Maheshwari 6

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

 

 

Kim AmaralKim Amaral
Awesome, that worked. Thanks again. Thank you, Kim Amaral SURPLUS SOLUTIONS LLC 1250 AIRPORT ROAD FALL RIVER, MA 02720 OFFICE: 508-646-2744 FAX: 508-409-3522
rajat Maheshwari 6rajat Maheshwari 6

Hi Kim,

Thank you, please let me know in case of any help :)

Please Mark as best answer :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com