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

How to populate custom Lookup field in trigger
I am new to the apex scene and with the help of another developerforce.com community member I was able to create a simple trigger. The trigger acts as such: upon an Account being marked as Type 'Customer', create a new custom object called Conversion and map over the appropriate fields. Code below
trigger AddConversionUponAccountCustomer on Account (after insert, after update) { Conversions__c[] cList = new Conversions__c[]{}; for(Account a : trigger.new) { if(a.Type == 'Customer') cList.add(new Conversions__c (Name = a.name, phone__c = a.Phone, email__c = a.Email__c)); } insert cList; }
The trigger works great, but I need to be able to grab the Account that the Conversions record was created off of and attach it. I created a new lookup field called Account__c under my Conversions custom object, but I am having trouble integrating this into my trigger so that when the Account is marked 'Customer', a new Conversions record is created, with all of the appropriate fields mapped over, AND THE ACCOUNT IS ATTACHED IN THIS FIELD. Can somone please offer me some advice on how I can incorporate a lookup field into the above? (Did I mention I am new :p)
You should be able to just do something like this I think - a lookup field needs an Id so set Account__c equal to the Id of the account being processed by the trigger:
All Answers
You should be able to just do something like this I think - a lookup field needs an Id so set Account__c equal to the Id of the account being processed by the trigger:
It's really weird, I was trying that earlier and it was giving me an error that it was an Invalid ID (I actually have the error copied), But now all of the suddent it is working like a charm. Mustve been a bug, or maybe it was just operator problems :)
Thanks for the reply.
TRY this code defenitly u get the solution for Autopopulation just you have to make Looup relation with User
trigger populateContactfromUser on Contact (before insert , before update){
Set<ID> setConIds = new Set<ID>();
for(Contact obj : trigger.new){
if(obj.User_Name__c!= null)
setConIds.add(obj.User_Name__c);
}
MAP<ID , User> mapCon = new MAP<ID , User>([Select Id,Email,Phone from User where id in: setConIds]);
for(Contact obj : trigger.new)
{
if(obj.User_Name__c != null)
{
User c = mapCon.get(obj.User_Name__c);
obj.Email= c.Email;
//Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
}
}
}