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

Using a trigger to autopopulate fields
I would like to write a trigger whereby the user can enter the name of a Contact via a lookup field and the Contact's address and phone number will be automatically displayed in textfields. Please help. Thank you.
No that is not possible trigger will only work after you perform any dem activity. In your case insert or update. It will not populate merely by selection on native page layout. If you want to achieve this you have to create VFP and it will increase your work.
If you don't want these address fields to come on edit layout you can use formula field as well. But then also no option that it will be filled before you click save button.
All Answers
you need to write a trigger like this ,
I have used MyObject__c in this replace with you object and fields
Thanks a lot for your quick response.
Wouldn't it be after update since I want the fields to auto populate after the user insert the name of the contact.
I tried the code and I keep getting an error saying: Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Contact>].add(String) at line 14 column 26.
Irrespective of the above thread, if you want to add values in map you need to use "PUT" instead of Map.Add() ;
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Change mapCon.add(obj.Contact__c); to
And before insert is better as in after insert you will need to make a dml statement to Update your record, incase of before insert trigger you will not required as it's getting inserted itself. In case you go for after insert , after update, your update will again take you beck to the trigger so you will need to write extra logic for avoid such recurring of this trigger. So go with before insert, before update.
Let me know if any issues in it.
Thank you. That seem to have done the trick. I dont get the error message anymore. However, the fields dont update themselves until after i hit save. If there anyway to have the values appear before i hit save. Maybe I entered something wrong, here is what I put:
trigger populateContact on Member_Assess__c (before insert , before update){
Set<ID> setConIds = new Set<ID>();
for(Member_Assess__c obj : trigger.new){
if(obj.Member_Name__c != null)
setConIds.add(obj.Member_Name__c);
}
MAP<ID , Contact> mapCon = new MAP<ID , Contact>([Select Address__c from Contact where id in: setConIds]);
for(Member_Assess__c obj : trigger.new)
{
if(obj.Member_Name__c != null)
{
Contact c = mapCon.get(obj.Member_Name__c);
obj.Address__c = c.Address__c;
//Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
}
}
No that is not possible trigger will only work after you perform any dem activity. In your case insert or update. It will not populate merely by selection on native page layout. If you want to achieve this you have to create VFP and it will increase your work.
If you don't want these address fields to come on edit layout you can use formula field as well. But then also no option that it will be filled before you click save button.
Thank you so much for all your help
hi i need to populate Trigger on account: If custom address is null and billing address is not null, populate the custom address with billing address...
i try your code but it shows
trigger populateContact on Account ( before insert , before update)
{
Set<ID> setConIds = new Set<ID>();
for(Account obj : trigger.new)
{
if(obj.BillingStreet != null)
setConIds.add(obj.Billing Street);
}
MAP<ID , Account> mapCon = new MAP<ID , Account>([Select ShippingStreet from Account where id in: setConIds]);
for(Account obj : trigger.new)
{
if(obj.Billing Street != null)
{
Account c = mapCon.add(obj.Account);
obj.ShippingStreet = c.ShippingStreet;
//Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
}
}
}
Please help me on this paste th code
hi i need to populate Trigger :
If custom address is empty and billing address is have a address, need to move custom address with billing address...
trigger populateAdd on Account (before insert , before update){
Set<String> setConIds = new Set<String>();
for(Account obj : trigger.new){
if(obj.cust_address__c != null)
setConIds.add(obj.cust_address__c);
}
MAP<ID , Account> mapCon = new MAP<ID , Account>([Select cust_address__c from Account where id in: setConIds]);
for(Account obj : trigger.new)
{
if(obj.BillAddress__c != null)
{
Account c = mapCon.get(obj.cust_address__c);
obj.cust_address__c = c.BillAddress__c;
}
}
}
Why its not updated
Thanks for giving solution for Auto population