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
Andrea SloanAndrea Sloan 

New Formula Checkist Field Validates to True (with two non related objects)

I've tried this declaratively and it did not work so I'm hoping someone can help me do this through VisualForce or Appx. Please keep in mind that I'm not familiar with the developer coding so any samples you could provide for me to try out would be great.
My_Account__c :  (API name) is a custom checkbox field that I'm trying to set to true based on certain conditions described below. This field is under the standard Account object.

To determine whether My_Account__c   field is true, we want ot look at the field values specified below residing within two objects that are NOT related objects:

Standard Object 1: Account
Field 1: Billing Country (standard text type field, part of the address component)

Custom Object 2: Territory_Ownerships__c (API name)
Field 1: Name (API name and it's text type) but I've relabeled the name of the custom field to Owner SourceCountry (the label name)
Field 2: Opportunity_Owner_Sanctuary_Director__c (API name and it's a lookup type)

My_Account__c should equal true when

Account's Billing Country value is the same as ( Name field value  in Territory_Ownerships__c object AND Opportunity_Owner_Sanctuary_Director__c in Territory object is the same as the name of the Salesforce user currently logged in) All else, the checkbox field should be false.

For example, my name is Andrea Sloan and I'm looking at a record in the Account's object whch has France listed as the billincountry name.
The territory record table shows that there's a record where the Name field contains France as a value and for that record the Opportunity Owner Sanctuary Director field has Andrea Sloan listed  as a value. When I log in, if I run a report with MyAccount checkbox field as a filter value to true, I should be able to see the given account record that had shown France as the billing country.

This checkbox field, should refresh for each user login. When I tried this declaratively, once the field was checked, it remained checked under a user who should have had theirs checked off. In other words, if your name is not listed as the owner for France as per my example then that field should not be checked.

Also, this verification should take into effect anytime an account record is created but also anytime any of the fields in question that I mentioned above from either of the objects are edited. Therefore, the account may move and no longer be in France and Andrea's name may no longer belong to France in the Territories object.

Any help someone could provide hopefully with the syntaxed code would be great! Thank you in advance!
Virendra Singh NarukaVirendra Singh Naruka
Hi Andrea,

Use following Trigger code 

trigger AccountTrigger on Account (before insert) {
    Set<String> accountBillingCity = new Set<String>();
        if(act.billingCity != null){
           String billCity =  act.billingCity;
        set<String> cityExistForUser = new set<String>();
        for(Territory_Ownerships__c  trOwnership:[Select  name  from
                                                 Territory_Ownerships__c where Opportunity_Owner_Sanctuary_Director__c =:UserInfo.getUserId() 
                                                 and  name in:accountBillingCity ]){
             String billCity =;
                String billCity =  act.billingCity;
                if(act.billingCity != null && cityExistForUser.contains(billCity) ){
                   act.My_Account__c  = true;


trigger Territory_OwnershipsTrigger on Territory_Ownerships__c (after insert, after undelete, after update, before delete) {
            map<String,Id> accountCityName = new map<String,id>();
                if(tOnwerShip.Territory_Ownerships__c != null 
                     && tOwnerShip.Territory_Ownerships__c == UserInfo.getUserId()){
                list<Account>  updateMyAccounts = new list<Account>();
                for(Account act:[Select id,My_Account__c from Account where billingCity in:accountCityName]){
                    act.My_Account__c = true;
                    update updateMyAccounts;
        // Write Same for Update and delete and undelete 
Andrea SloanAndrea Sloan
Hi Virendra:

Thank you for your help . Not knowing much of proper development code aside from cut and paste and try and copy one code to a similar scenario, I'm a bit confused here:

1. Shouldn't "accountBillingCity" it be accountBillingCountry based on the field I've specified?
2. What does this mean? cityExistForUser.add(billCity.toUpperCase());           What are we putting in upper case and why are we doing this?        3. Your comment says "// Write Same for Update and delete and undelete " I'm not sure what you mean by this. What is it that you wrote? How do I write it differently? Am I opening up a new ApexClass to do so? Or a new Apex Trigger? or is it part of the same Trigger?
4. Where do I put the code that you've given me? Is it under  Devloper- Apex Classes or Developer - Apex Trigger?
5. Do I need to create a VisualForce page and call this trigger from it?

Would you have a Skype contact  so we could call you if you're okay with this? It may be easier to clear up some of my questions.

Andrea SloanAndrea Sloan
Hi Virendra: Would you have answers to my above questions as I'm stuck. Thanks!