You need to sign in to do that
Don't have an account?

Update a checkbox on a record to uncheck all other existing records
So attached is the code i have in place that im looking to have uncheck a checkbox on a record if its in a list after a record has been updated or added. The class is also there to stop the infinate loop. But its still allowing for me to have multiple checked address records. Can anyone see what im missing...
Hopefully someone has dealt with this before. Also i have a primary billing checkbox as well. Can i add that into this same trigger or should i create a new one for that.
Apex Code: trigger PrimaryAddressValidation on ShipTo_Address__c (after insert, after update) { if (SingleExecution.hasAlreadyDone()) { return; } SingleExecution.setAlreadyDone(); Contact c=[select id from contact where id in(select Contact__c from ShipTo_Address__c where id in:trigger.new)]; list<ShipTo_Address__c> addList=new list<ShipTo_Address__c>([select id from ShipTo_Address__c where Contact__c in:Trigger.newMap.keySet()]); Id selectedAddressId; list<ShipTo_Address__c> updateList=new list<ShipTo_Address__c>(); for(ShipTo_Address__c add:Trigger.new) { if(add.Default_Shipping_Address__c==true) { c.OtherStreet = add.Address__c; c.OtherCity = add.City__c; c.OtherState = add.State__c; c.OtherCountry = add.Country__c; c.OtherPostalCode = add.ZIP__c; } } for(ShipTo_Address__c aa:addList) { if(aa.Id!= selectedAddressId) { aa.Default_Shipping_Address__c=false; updateList.add(aa); } } update updateList; update c; }
Class: public with sharing class SingleExecution { private static boolean blnAlreadyDone = false; public static boolean hasAlreadyDone() { return blnAlreadyDone; } public static void setAlreadyDone() { blnAlreadyDone = true; } public static void forceResetAlreadyDone() { blnAlreadyDone = false; } }
Hopefully someone has dealt with this before. Also i have a primary billing checkbox as well. Can i add that into this same trigger or should i create a new one for that.
Try this:
All Answers
Static variables are only static within the scope of the request. They’re not static across the server, or across the entire organization.
More info here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
so if Amy has 3 relates addresses and:
801 43rd st is primary shipping = true
919 lavy way is primary billing = true
and we add in:
4226 rosewood ave as primary shipping = true
now:
801 43rd st is primary shipping = false
hopefully i was able to clearly give an example of what i am trying to do.Thanks for your help...
I found the extra AND In when trying to complie. This is great the code works and is keeing it from having more then one at a time awesome. Now one last question. So i add in the below code (that i already had working) so that it updates the contact fields when the checkbox is checked. I added it to your code and it works but if the address has both checkboxes checked it will only update the billing not the shipping. Should i just leave it as its own trigger or can it be added into this one. My added code is at the bottom. I also left the after functions in as we have an api integration setup with an outside tool that will update the address information.
Compilation error: Variable does not exist: c.Id PrimaryAddressValidation.trigger /Sanbox/src/triggers line 109 Force.com compilation problem
Try this:
Thanks again for all your help