You need to sign in to do that
Don't have an account?
Pamy
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
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
All Answers
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)
Although you can achieve this functionality through Process Builder too, if you want to accomplish this using trigger then use below mentioned code:
Please use the below code:
Here I considered:
(1) Alignment.
(2) Changed the insert statement.
(3) Naming Convention.
Please do let me know if it helps you.
Regards,
Mahesh