+ Start a Discussion
rajesh k 10rajesh k 10 

when checkbox will be check how to update a field using trigger in salesforce?

Create a custom field cancel__c(checkbox) on sales_Order__c  custom object.
Whenever this checkbox gets checked, a trigger should fire and update the Discount percentage field to 50%.
Best Answer chosen by rajesh k 10
rohitsfdcrohitsfdc
Rajesh, 
the code looks fine to me.

Do you see anything in the debug log? Lets remove the onchange event.

try the code below

trigger salesordertrigger on Sales_Order__c(before update){
for(Sales_order__c so : trigger.new){
        if(so.Cancel__c)
        {
           
            so.discount__c=70;
            
        }
      
        }
}




All Answers

rohitsfdcrohitsfdc
Hello Rajesh,
You can check the value change in trigger by using code below

trigger salesordertrigger on (before update){
for(Sales_order__c so : trigger.new){
if(so.sales_Order__c.cancel__c && !trigger.oldmap.get(so.id).cancel__c )

//your logic here

}
}


rajesh k 10rajesh k 10
Hi,
     I got Foreignkey relationship error.

please help me..
rajesh k 10rajesh k 10
Hi,
       I wrote like this but not working

trigger Trg_Discountupdateonorder1 on sales_Order__c(before update){

List<sales_Order__c> lst=new List<sales_Order__c>();

        for(Sales_order__c so : trigger.new)
        {
            if(so.Cancel__c == true)
            {
            so.Discount__C =70;
            lst.add(so);
            }
                  
           


        }
        if(lst.size() > 0)
        {
        update lst;
        }
       
}

please help me..
rohitsfdcrohitsfdc
Sorry about my code, it had one mistake. FIxed it. try it now please

trigger salesordertrigger on (before update){
for(Sales_order__c so : trigger.new){
if(so.cancel__c && !trigger.oldmap.get(so.id).cancel__c )

so.discount__c=70;

}
}


rajesh k 10rajesh k 10
Hi,
      Here where your checking cancel__c check is true or false.

         Hi i tried but Discount field not updated and There is no debug log also

please help me..........
rohitsfdcrohitsfdc
try changing the value of cancel checkbox to true from false in any record
rajesh k 10rajesh k 10
Hi,
         I try to like this but not working

trigger salesordertrigger on Sales_Order__c(before update){
for(Sales_order__c so : trigger.new){
        if(so.Cancel__c && !trigger.oldmap.get(so.id).cancel__c )
        {
            if(so.Cancel__c == true)
            {
            so.discount__c=70;
            }
        }
      
        }
}
rohitsfdcrohitsfdc
Rajesh, 
the code looks fine to me.

Do you see anything in the debug log? Lets remove the onchange event.

try the code below

trigger salesordertrigger on Sales_Order__c(before update){
for(Sales_order__c so : trigger.new){
        if(so.Cancel__c)
        {
           
            so.discount__c=70;
            
        }
      
        }
}




This was selected as the best answer
Ankush DurejaAnkush Dureja
What error you are getting?