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
Steve BairSteve Bair 

Automatically update field based on value in another field

I have created an object (table) with three fields -- number, name, and person.  There are only five records in this object each with a unique "number" (1 - 5).
 
On the account view, I have placed a picklist with the numbers 1 through 5 to match the "number" field in the custom object. 
 
My goal is to place two more fields on the account view such that, when the user selects a pick list value, the other two fields will be filled in with the corresponding "name" and "person" fields from the custom object.
 
Any help will be appreciated -- or the location of docs.
 
TIA
werewolfwerewolf
Because it requires a lookup to a custom object that is not explicitly a parent of the account, you can only do this with an Apex trigger.  The Force.com Cookbook would be the best place to look for examples, although I'm not sure if it'll have anything that addresses this specifically.

Note that you will not be able to do this instantly in edit mode -- it will only happen once you save the account, and then the trigger can run and update the other fields.

That said, are you sure you want to duplicate the data in that custom object into the Account fields?  What if you need to change a name -- then won't you have to go through all Accounts and change it in all of them?  You might actually be better served by making a lookup from account to your custom object, and setting that lookup through your trigger instead (you'll still need a trigger to set that lookup though).  In the Summer release you'll be able to make formula fields that get data from parent objects like your custom object, so you could make those name and person fields as formula fields.  Then if you change the data in the custom object, your accounts will just automatically inherit that change.
Steve BairSteve Bair

Thanks.  You are right, I do not want to duplicate the data in the account fields -- I just want the user to see the info that corresponds to the "number" field selection.  It's ok if these don't show up until the record is saved.

It seems like a trigger would be overkill.  Could this be done with a formula?

Thanks for your help.

werewolfwerewolf
No, a trigger is not overkill.  You can't do this with just a formula field because a formula field isn't going to look up to some arbitrary other object and say "find me the one where the number field equals something."  You have to set that lookup from Account to your custom object, and a trigger is the only way to do so.
Steve BairSteve Bair

Then a trigger it shall be.

Thank you very much for your help.

jrotensteinjrotenstein
An S-Control should be sufficient for your needs, and would be easier to implement than an Apex trigger.

Rather than creating new fields that are populated from the other object, have the S-Control retrieve and display the information from the other object. It would run a SOQL query to retreive the object matching the selected number, then display it on-screen.

There's plenty of S-Control samples inthe discussions boards and wiki, but let us know if you're having difficulties.
Steve BairSteve Bair
Thanks again for the help.  All of it has been very appreciated.
werewolfwerewolf
For the record I disagree with jrotenstein.  An Apex trigger will likely be less code.  Plus, if you put an inline Scontrol in there, it will have to make an additional API query every time you load up the page to go get that custom object data.  Therefore I'd go with the Apex trigger if I were you, but he rightly says that both options are available to you.
pacstratspacstrats
werewolf,

Do you have any examples of triggers where you are using the values of a custom field in object 1 to reference the values in object 2? I've run into the same scenario where I need the system to auto-populate the lookup field of the Lead object with the ID/Name of a related custom object (Employee) based on a value stored in the Lead object. The value in the Lead is created based on workflow, and I need that value to create the relationship with the custom object.

Any help would be greatly appreciated.

T
pacstratspacstrats
I think I may have figured it out, but not sure if it's the most effective method:

trigger EmployeeUpdate on Lead (before insert, before update) {
List EmployeeName = new List();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
EmployeeName.add(Trigger.new[i].EmployeeName__c);
}
List EmployeeList = new List([Select Id from Employee__c where Name in :EmployeeName]);
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].EmployeeName__c != null)
{
Trigger.new[i].Employee__c = EmployeeList[i].Id;
}
else
{
Trigger.new[i].Employee__c = null;
}
}
}
werewolfwerewolf
Yes, that looks about right.
pacstratspacstrats
Any idea how to prevent the record from updating if the fields don't match?
werewolfwerewolf
You can put an addError on the offending field, as per this doc.
Krishna SambarajuKrishna Sambaraju
Hi pacstrats,

Do you have the Lookup field of the custom object Employee on the Lead object? In that case you can create formula fields on Lead object to get the values from the Employee object. No need of a trigger.

Regards,
Krishna.
Chris NidelChris Nidel
Krishna - how would one do this?  That seems like a simpler solution.  thanks.
Krishna SambarajuKrishna Sambaraju
Hi Chris,

If you have a custom object Employee__c and you have a lookup field on lead to this custom object, you can create formula fields to get the field values from this custom object.

For example if you want to bring the value of fieldA from Employee object then you can create a formula field with the following value
Employee__r.fieldA__c

Hope this helps.

Regards,
Krishna.
covore7855 covore7855covore7855 covore7855
Indian Escorts in Dubai (http://www.modelofdubai.com) is pretty famous for providing various kinds of escort services. +971-563680438
Escorts in Abu Dhabi (http://www.uaehotcollection.com) Indian Escorts in Abu Dhabi Call Mannat +971561033256, A well known professional, educated escorts, who offers high class Independent Abu Dhabi Escorts Service.
Indian Call Girls Dubai (https://www.vvipmodel.com) Call/WhatsaPP +971561033256, Indian Escorts in Dubai, Call Girls Dubai. Indian Celebrities Escorts In Dubai Contact Indian Independent Escort in Dubai, Very Cheap Price and safe Independent Escorts Dubai.
Indian Escort in Abu Dhabi (http://www.simmi-patel.com) Indian Escort Girl in Abu Dhabi With. Our Abu Dhabi Escorts and Independent Agency Abu Dhabi Escorts aren't simply standard girls as you will verify.
Dubai independent escort (http://www.tanishaverma.com) If you are searching for the same that is the escort with elegant personality then your search is finish as you have landed in the right site.