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


}
Best Answer chosen by parth sachi 7
Maharajan CMaharajan C
Hi,

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

Neha AggrawalNeha Aggrawal
Hi Parth,

You can check the old and new value of the brand field.
 
Mobile__c [] newEntries = Trigger.new;
Mobile__c [] oldEntries = Trigger.old;
Integer i=0;
for(Mobile__c temp:newEntries)
if (oldEntries[i].Brand__c!= newEntries[i].Brand)
{
//Do something
i++;
}

Hope this helps.
Thanks and Regards, 
Neha
www.initaura.com - Everything Salesforce (https://www.initaura.com)
Maharajan CMaharajan C
Hi,

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
This was selected as the best answer
parth sachi 7parth sachi 7
ThankYou Neha Aggrawal and Maharajan.C :)