1

I am fairly new to triggers, so please bear with me.

Goal:

The related stores dynamically update based on the Account location's Postal/Zip Code.

Requirement:

When a new Account is created or updated with a valid Postal/Zip Code value, dynamically update the related list "Phone Stores", with Phone Stores that share the same Postal/Zip Code value.

Example:

XYZ Corporation Postal/Zip Code value was updated from "97219" to "97217".

There are 2 Phone Stores with the value of "97219" (9PS1,9PS2) There are 4 Phone Stores with the value of "97217" (7PS1,7PS2,7PS3,7PS4).

XYZ Corporation originally had 9PS1 and 9PS2 linked to it via the Phone Store related list. The link between XYZ Corporation 9PS1 and 9PS2 need to sever, only to be replaced by 7PS1, 7PS2, 7PS3, 7PS4.

Questions

  1. Is this possible to do within Salesforce.com?
  2. How do I even begin to code this with apex?
  3. Should there be visual force elements to this, instead of the classic related list?
2

This is definitely possible within Salesforce and a perfect example of a useful trigger! :-) I don't think there's a need for this trigger to run after creating an Account though: When you create an Account it will never immediately have Phone Stores related to it.

You would start with a trigger on the Account object. A best practice for Triggers is to make sure that they do not contain any logic. You want to add a class (called a Trigger Helper class) to contain your logic, and call it's methods from the trigger. This way you can easily see which actions on an object cause which methods to run.

trigger AccountTrigger on Account(before insert, after insert, before update, after update, before delete, after delete, after undelete){
    if(Trigger.isUpdate && Trigger.isAfter){
        AccountTriggerHelper.updatePhoneStoresZipcodes(Trigger.oldMap, Trigger.newMap);
    }
}

In the AccountTriggerHelper class you want to define a method called updatePhoneStoresZipcodes. In this method, you can retrieve all Phone Stores where the Account__c field is in the list of updated Accounts. Next, you can map the Phone Stores to the changed Accounts and compare zipcodes.

public with sharing class AccountTriggerHelper {
    public static void updatePhoneStoresZipcodes(Map<Id, Account> accountsOld, Map<Id, Account> accountsNew) {
        // Here we create a list of Account IDs that we can use in our query for Phone Stores
        Set<Id> accountIds = new Set<Id>();
        for(Account a : accountsNew.values())
            accountIds.add(a.Id);

        // Make sure to change object/field names!
        // Here we create a map with a list of Phone Stores per Account
        Map<Id, List<Phone_Store__c>> phoneStoresToAccount = new Map<Id, List<Phone_Store__c>>();
        PhoneStore__c phoneStores = [SELECT Id, Zipcode__c, Account__c FROM PhoneStore__c WHERE Account__c IN : accountIds];
        for(PhoneStore__c ps : phoneStores){
            if(!phoneStoresToAccount.containsKey(ps.Account__c))
                phoneStoresToAccount.put(ps.Account__c, new List<Phone_Store__c>();
            phoneStoresToAccount.get(ps.Account__c).add(ps);
        }

        // Here we compare zipcodes and add Phone Stores to a set
        Set<PhoneStore__c> updatePhoneStores = new Set<PhoneStore__c>();
        for(Account a : accountsOld.values()){
            // Check whether zipcode changed on Account
            if(accountsNew.get(a.Id).Zipcode__c != a.Zipcode__c){
                // Check whether Account has any Phone Stores
                if(phoneStoresToAccount.containsKey(a.Id){
                    for(PhoneStore__c ps : phoneStoresToAccount.get(a.Id){
                        // Check whether the old zipcode is the same and if so, update it on the Phone Store
                        if(ps.Zipcode__c == a.Zipcode__c){
                            ps.Zipcode__c = accountsNew.get(a.Id).Zipcode__c;
                            updatePhoneStores.add(ps);
                        }
                    }
                }
            }
        }

        update updatePhoneStores;
   }
}

Keep in mind I typed this code in a textbox and haven't tested it. Let me know if it doesn't work!

| improve this answer | |
0

I think this should be possible with a trigger. It is obvious that you are talking of a "Before Insert and Before Update" trigger. I have a question, do you have a custom object that contains the Phone Store in relation to the Postal/Zip Code? I don't fully understand the relation from the Store Phones and the Account. If you have a relation in which the account postal/zip code is connected to the store phones we can build this, don't see why not.

To begin you should start looking to the:

trigger AccountZipCodeStorePhones on Account(before insert, before update){ // your code }

You probably should be looking to the Trigger.oldMap and Trigger.newMap variables because I think you will be comparing those two. Don't think you need a Visualforce for this, I'm guessing that the update will happen on an Store Phone field?

Could you please explain a bit more the Store Phone and Account relation?

| improve this answer | |
0

I assume you 'Phone Stores' is a related object to 'Account'. You can think of using 'Lightning Process Builder' to build this functionality.

https://help.salesforce.com/HTViewHelpDoc?id=process_overview.htm

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.