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
kirubakaran viswanathankirubakaran viswanathan 

Why my custom checkbox is always checked when I try to save as uncheck in the account object with active trigger?

Why my custom checkbox is always checked when I try to save as uncheck in the account object?

This is happening only when I have a active trigger. The trigger which I wrote is simple one to check whether that checkbox is "checked" or not, then copy the postal code from Billing address to shipping address. 

When that particular trigger is "Inactive" and tried to save the record with that checkbox as "unchecked" it is working good.
Not sure why this trigger is always "checked" the checkbox when I save the record.

This is my trigger:
Trigger AccountAddressTrigger on Account (before insert,before update) {
       for (Account a : Trigger.new)
   {
           if(a.Match_Billing_address__C = True)
       {
         if(a.BillingPostalCode != NULL)
               {
       a.ShippingPostalCode= a.BillingPostalCode;
                  } 
           }
   }
   
}
 
Best Answer chosen by kirubakaran viswanathan
Jithin Krishnan 2Jithin Krishnan 2
Hi Kirubakaran,
This is because of a minor mistake in your if condition. Replace the below line:
if(a.Match_Billing_address__C = True)
to this:
if(a.Match_Billing_address__C == True)
The first one assigns the value 'true' to the field while the second one checks if the field value is true.
Please mark the answer and close the question if this helped you.
Thanks!
 

All Answers

Jithin Krishnan 2Jithin Krishnan 2
Hi Kirubakaran,
This is because of a minor mistake in your if condition. Replace the below line:
if(a.Match_Billing_address__C = True)
to this:
if(a.Match_Billing_address__C == True)
The first one assigns the value 'true' to the field while the second one checks if the field value is true.
Please mark the answer and close the question if this helped you.
Thanks!
 
This was selected as the best answer
Ketankumar PatelKetankumar Patel
Trigger AccountAddressTrigger on Account (before insert,before update) {
  for (Account a : Trigger.new)
   {
      if(a.Match_Billing_address__C)
       {
         if(a.BillingPostalCode != NULL)
               {
                   a.ShippingPostalCode= a.BillingPostalCode;
                } 
           }
     }
   
}

In if condition you don't event need to compare with boolean Variable true or false (but if you want you can). 

For checking true condition you can do
if(a.Match_Billing_address__C)

For checking false condition you can do
if(!a.Match_Billing_address__C)
kirubakaran viswanathankirubakaran viswanathan
Thank you Jithin!!! It works...

Thank you Ketankumar for your suggestion
Shiva SelviShiva Selvi
Completely agree, I'd also not do (http://www.trainingintambaram.in/informatica-training-in-chennai.html) that in a trigger. Though it may be good to know the real use case, as this is pro (http://www.trainingintambaram.in/informatica-training-in-chennai.html)bably something better tacked in an alternative functional desi (http://www.trainingintambaram.in/informatica-training-in-chennai.html)gn.


trigger trigger_ManagePrimaryMember on Team_Members__c (before update) { Set<Id> currentprimarymemberid = new Set<Id>(); Set<Id> currentprimarymembrteamid = new Set<Id>(); List<Team_Members__c> teamid =new List<Team_Members__c>(); List<Team_Members__c> Updateprimarymember = new List<Team_Members__c>(); for(Team_Members__c primarymember : trigger.new) { currentprimarymemberid.add(primarymember.Id); } teamid= [select Id,Team__c from Team_Members__c where Id in :currentprimarymemberid]; for(Team_Members__c teammembers :teamid) { currentprimarymembrteamid.add(teammembers.Team__c); } for(Team_Members__c teammembers :[select id,Name from Team_Members__c where Team__c in :currentprimarymembrteamid]) { if(!currentprimarymemberid.contains(teammembers.id)) { // making the checkbox to false for other member in the team teammembers.check_box__c= false; } } }