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
SFDC n12SFDC n12 

Formula field help needed

Hi,

I need help on the following requirement


1) I am having a text field in account object called as "Dealer Principal"

i want to update that field with the contact first name and contact last name if the contact title ="Dealer Principal" which is assocaited with that account

so i am trying to create a formula field on contact object which is a cross lookup and check if the title is "Dealer principal" then i am calling the account field to update, I need help on that formula


IF
(
 Title ="Dealer Principal",
Account.Dealer_Principal__c = LastName

)

i am getting ' )'  missing


Help me with the formula 

Thanks in Advance

 
Hargobind_SinghHargobind_Singh
Hi SFDC n12, 

You can't use a formula field to update another field value. Formula fields are expressions, that can show a value as a result. If you do want to update a field, consider using a trigger. 
 
Marcos Hidalgo GonzálezMarcos Hidalgo González
You need (if I'm right) a field at account level that gets the name of the Contact marked as "Dealer Principal". Isn't it?

You don't need a trigger for that... you need a Workflow on Contact object.

Your workflow will be triggered always (everytime a contact is updated or created) if Title field equals "Dealer Principal". Workflow will have a field update, field update will be for the custom field Dealer_Principal__c on related Account, and the formula should be something like this one:

IF (Title ="Dealer Principal", Account.Dealer_Principal__c = LastName, "")

Your formula was almost right but you forgot the false result of the IF, that is, what it will return if condition is false.

Hope that helps! :)
SFDC n12SFDC n12
I wrote a trigger on this case


trigger ContactTrigger on Contact (after insert,after update) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }
  
    List<Account> accounts = [Select ID, Dealer_Principal__c,(Select Id,Title,Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String accName = '';
        for(Contact c : a.Contacts){
        if(c.Title=='Dealer Principal'){
            accName +=c.Name+ ' ';                      
        }
   
        a.Dealer_Principal__c=accName;
       }
   
    update accounts;
  
}
}



and it worked for normal record type


now i need to add an condition like if the account record type is "Group accoount" then the child account contact name with title (Dealer Principal) should be updated in the group account


help me how to add it in my trigger
Marcos Hidalgo GonzálezMarcos Hidalgo González
Sorry, my previous answer is wrong, SF doesn't allow updates from contact to account in workflows... some objects does (like update from opp. line item to opportunity) but it doesn't from contact to account... sick!

Give me a couple of minutes and I can send you the trigger code.
Marcos Hidalgo GonzálezMarcos Hidalgo González
Here comes the trigger:

trigger Test on Contact (before insert) {
    // Get Contact related account data, problem is Account.RecordType.Name is not available withing trigger.new
    list <Id> AccountIds = new list<Id>();
    for (Contact c : Trigger.new) AccountIds.add(c.AccountId); // this can be done with subqueries in SOQL, but I don't like them and sometimes they go wrong... I prefer up to 200 iterations vs my 200k limit than going into subqueries.
    map <Id,Account> mAccounts = new map<Id,Account>([Select Id, Name, Dealer_Principal__c, RecordType.Name From Account WHERE Id IN :AccountIds and RecordType.Name = :'Group accoount']); // Using a map helps you not nesting for loops later, always avoid loop nesting and queries inside loops. Be carefull as Record type name is hardcoded, if you change this name later you'll be in trouble as trigger will stop working... use custom labels or Custom settings instead of the literal.
    
    // List for account update later
    list <Account> lAccountUpdate = new list<Account>();

    for(Contact c : Trigger.new) {
        // Note that only accounts of the specific record type will be in the map.
        if (c.Title=='Dealer Principal' && mAccounts.containsKey(c.AccountId)){
            Account AuxAccount = mAccounts.get(c.AccountId);
            AuxAccount.Dealer_Principal__c = c.LastName;
            lAccountUpdate.add(AuxAccount);
        }
    }

    // Updating accounts.
    Update lAccountUpdate;
}

 
SFDC n12SFDC n12
Thanks for the trigger

My case is this should happen for all record types of account 


and if its a group account only then the parent account contact name should be populated else it should populate the name of the assocaited contact, so if we hardcode the recordid ="group" in trigger the query will return the list of records with that condition only right ?
 
Marcos Hidalgo GonzálezMarcos Hidalgo González
Yes, it seems I got you wrong, in the example trigger, only "Group" are used... for your particular case you'll have to get all parenting spectrum for your accounts. Coding will be much different depending on how deep your account tree is... for example, if your group has accounts inside and then those accounts has another accounts inside and so on... you will have to get parent grandparent grandgrandparent and so on for Contact's Account.

Anyway, the trick will be to play with account selection instead where you populate the account map, instead of direct population, query for a list of accounts including Parents, iterate them and get only the topmost ids if an account has them.

It is easy to do, but ask if you have problems with it. Describe how is your account hierarchy, maybe recordtype has nothing to do with it and you just want the topmost account.