You need to sign in to do that
Don't have an account?
parth sachi 7
Every time when i am updating any other field and save,before trigger is called.How i can eliminate this issue
Even i changes name field ,update trigger is getting called and adding un-neccessary discount to price.Expected is that it should update only if brand is getting change.How can i remove this ?
trigger MobileShopeeDiscount on Mobile__c (before insert,before Update) {
if(trigger.isInsert == true ){
MobileShopeeDiscount.MobileShopeeDiscountFuncation(Trigger.new);
}
if(trigger.isUpdate == true){
MobileShopeeDiscount.OldCustomerDiscountFuncation(Trigger.new);
}
}
public class MobileShopeeDiscount {
public static void MobileShopeeDiscountFuncation(list<mobile__c>VarList){
for(mobile__c tempvar :VarList){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.1);
tempvar.DiscountStatus__c = 'Congrates !! You got 10% Discount';
}else if(tempvar.Brand__c == 'Apple'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'Congrates !! You got 20% Discount';
}
}
}
public static void OldCustomerDiscountFuncation(list<mobile__c>VarList1){
for(Mobile__c tempvar :VarList1 ){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.05);
tempvar.DiscountStatus__c = 'Congrates !! You got 5% Discount';
}else if(tempvar.Brand__c == 'Apple'){
// tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'No Discount Applicable on this purchase';
}
}
}
}
trigger MobileShopeeDiscount on Mobile__c (before insert,before Update) {
if(trigger.isInsert == true ){
MobileShopeeDiscount.MobileShopeeDiscountFuncation(Trigger.new);
}
if(trigger.isUpdate == true){
MobileShopeeDiscount.OldCustomerDiscountFuncation(Trigger.new);
}
}
public class MobileShopeeDiscount {
public static void MobileShopeeDiscountFuncation(list<mobile__c>VarList){
for(mobile__c tempvar :VarList){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.1);
tempvar.DiscountStatus__c = 'Congrates !! You got 10% Discount';
}else if(tempvar.Brand__c == 'Apple'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'Congrates !! You got 20% Discount';
}
}
}
public static void OldCustomerDiscountFuncation(list<mobile__c>VarList1){
for(Mobile__c tempvar :VarList1 ){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.05);
tempvar.DiscountStatus__c = 'Congrates !! You got 5% Discount';
}else if(tempvar.Brand__c == 'Apple'){
// tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'No Discount Applicable on this purchase';
}
}
}
}
Use the Trigger.OldMap to Compare the old value and new value.
Please find the bewlo update code:
trigger MobileShopeeDiscount on Mobile__c (before insert,before Update) {
if(trigger.isInsert == true ){
MobileShopeeDiscount.MobileShopeeDiscountFuncation(Trigger.new);
}
if(trigger.isUpdate == true){
MobileShopeeDiscount.OldCustomerDiscountFuncation(Trigger.new,Trigger.OldMap);
}
}
public class MobileShopeeDiscount {
public static void MobileShopeeDiscountFuncation(list<mobile__c>VarList){
for(mobile__c tempvar :VarList){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.1);
tempvar.DiscountStatus__c = 'Congrates !! You got 10% Discount';
}else if(tempvar.Brand__c == 'Apple'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'Congrates !! You got 20% Discount';
}
}
}
public static void OldCustomerDiscountFuncation(list<mobile__c>VarList1, Map<Id,mobile__c> oldRecMap){
for(Mobile__c tempvar :VarList1 ){
Mobile__c oldRec = oldRecMap.get(tempvar.Id);
if(tempvar.Brand__c == 'samsung' && oldRec.Brand__c != tempvar.Brand__c){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.05);
tempvar.DiscountStatus__c = 'Congrates !! You got 5% Discount';
}else if(tempvar.Brand__c == 'Apple'&& oldRec.Brand__c != tempvar.Brand__c){
// tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'No Discount Applicable on this purchase';
}
}
}
}
Thanks,
Maharajan.C
All Answers
You can check the old and new value of the brand field.
Hope this helps.
Thanks and Regards,
Neha
www.initaura.com - Everything Salesforce (https://www.initaura.com)
Use the Trigger.OldMap to Compare the old value and new value.
Please find the bewlo update code:
trigger MobileShopeeDiscount on Mobile__c (before insert,before Update) {
if(trigger.isInsert == true ){
MobileShopeeDiscount.MobileShopeeDiscountFuncation(Trigger.new);
}
if(trigger.isUpdate == true){
MobileShopeeDiscount.OldCustomerDiscountFuncation(Trigger.new,Trigger.OldMap);
}
}
public class MobileShopeeDiscount {
public static void MobileShopeeDiscountFuncation(list<mobile__c>VarList){
for(mobile__c tempvar :VarList){
if(tempvar.Brand__c == 'samsung'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.1);
tempvar.DiscountStatus__c = 'Congrates !! You got 10% Discount';
}else if(tempvar.Brand__c == 'Apple'){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'Congrates !! You got 20% Discount';
}
}
}
public static void OldCustomerDiscountFuncation(list<mobile__c>VarList1, Map<Id,mobile__c> oldRecMap){
for(Mobile__c tempvar :VarList1 ){
Mobile__c oldRec = oldRecMap.get(tempvar.Id);
if(tempvar.Brand__c == 'samsung' && oldRec.Brand__c != tempvar.Brand__c){
tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.05);
tempvar.DiscountStatus__c = 'Congrates !! You got 5% Discount';
}else if(tempvar.Brand__c == 'Apple'&& oldRec.Brand__c != tempvar.Brand__c){
// tempvar.Price__c = tempvar.Price__c-(tempvar.Price__c *0.2);
tempvar.DiscountStatus__c = 'No Discount Applicable on this purchase';
}
}
}
}
Thanks,
Maharajan.C