+ Start a Discussion
PamyPamy 

When Product gets created/updated in Salesforce, a new record will be inserted in a custom object(Custom__c) through Apex Trigger

Hi All,

Can anyone help me in trigger. My requirement is below:
Whenever Product gets inserted or updated in salesforce on Product2 object, a new record same as on Product2 will be inserted in a custom object(custom__c). Below is my code but it is giving error.
trigger MHHE_Pricing_Tier_Temp_ProductUpdate on Product2 (after insert,after update) {
    List <MHHE_Pricing_Tier_Temp__c> productToInsertUpdate = new List <MHHE_Pricing_Tier_Temp__c>();
    if(trigger.isInsert || trigger.isUpdate){
    for (Product2 o : Trigger.new) {
    MHHE_Pricing_Tier_Temp__c temprec = new MHHE_Pricing_Tier_Temp__c();
    temprec.Product_Name__c=o.name;
    productToInsertUpdate.add(temprec); 
    }
    }
     try{
        insert productToInsertUpdate;
     } 
     catch (system.Dmlexception e) {
      system.debug (e);
    }

Any help regarding would be much appriciated
Best Answer chosen by Pamy
JethaJetha
Then please use the below modified code : 
trigger MHHE_Pricing_Tier_Temp_ProductUpdate on Product2 (after insert,after update) 
{
    List <MHHE_Pricing_Tier_Temp__c> productToInsertUpdate = new List <MHHE_Pricing_Tier_Temp__c>();
    if(trigger.isInsert || trigger.isUpdate)
	{
		for (Product2 o : Trigger.new) 
		{
			MHHE_Pricing_Tier_Temp__c temprec = new MHHE_Pricing_Tier_Temp__c();
			temprec.Product_Name__c=o.Id;
			productToInsertUpdate.add(temprec); 
		}
	}
     
	try
	{
        insert productToInsertUpdate;
	} 
     
	catch (system.Dmlexception e) 
	{
      system.debug (e);
    }

All Answers

JethaJetha
What is the data type for your field Product_Name__c and can you please let us know the error message???
PamyPamy
Error message is Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Temp_ProductUpdate caused an unexpected exception, contact your administrator: Temp_ProductUpdate: execution of AfterInsert caused by: System.StringException: Invalid id: test1: Trigger.Tier_Temp_ProductUpdate: line 7, column 1

data type of Product_Name__c is Lookup(Product)
JethaJetha
Then please use the below modified code : 
trigger MHHE_Pricing_Tier_Temp_ProductUpdate on Product2 (after insert,after update) 
{
    List <MHHE_Pricing_Tier_Temp__c> productToInsertUpdate = new List <MHHE_Pricing_Tier_Temp__c>();
    if(trigger.isInsert || trigger.isUpdate)
	{
		for (Product2 o : Trigger.new) 
		{
			MHHE_Pricing_Tier_Temp__c temprec = new MHHE_Pricing_Tier_Temp__c();
			temprec.Product_Name__c=o.Id;
			productToInsertUpdate.add(temprec); 
		}
	}
     
	try
	{
        insert productToInsertUpdate;
	} 
     
	catch (system.Dmlexception e) 
	{
      system.debug (e);
    }
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
Hi Pamy,

Although you can achieve this functionality through Process Builder too, if you want to accomplish this using trigger then use below mentioned code:
 
trigger MHHE_Pricing_Tier_Temp_ProductUpdate on Product2 (after insert,after update) 
{
    List <MHHE_Pricing_Tier_Temp__c> productToInsertUpdate = new List <MHHE_Pricing_Tier_Temp__c>();
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
	{
		for (Product2 o : Trigger.new) 
		{
			MHHE_Pricing_Tier_Temp__c temprec = new MHHE_Pricing_Tier_Temp__c();
			temprec.Product_Name__c=o.Id;
			productToInsertUpdate.add(temprec); 
		}
	}
     
	
        insert productToInsertUpdate;
	}

 
Mahesh DMahesh D
Hi Pamy,

Please use the below code:

Here I considered:

(1) Alignment.
(2) Changed the insert statement.
(3) Naming Convention.

 
trigger ProductAfterTrigger on Product2 (after insert,after update) {

    List<MHHE_Pricing_Tier_Temp__c> pttList = new List<MHHE_Pricing_Tier_Temp__c>();
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
		for (Product2 o : Trigger.new) {
			MHHE_Pricing_Tier_Temp__c tempRec = new MHHE_Pricing_Tier_Temp__c();
			tempRec.Product_Name__c = o.Id;
			pttList.add(tempRec); 
		}
		
		if(!pttList.isEmpty())
			insert pttList;
	}
}

Please do let me know if it helps you.

Regards,
Mahesh
PamyPamy
Thank you so much all your Help :)