I'd like to create a trigger that automatically assign a value to a lookup field.
I've tried to emulate what I've read on these 3 posts (but without success)
https://developer.salesforce.com/forums?id=906F000000094NcIAI; How to Give Default value to a LookUp field in salesforce; Trigger based on Record Type + Default Value + Lookupfield;
Here is the situation. I have a Product_Sale__c object. Sale_Account__c is a lookup field in that object. That field is looking up for the Name (the Id?) of an account on Account__c. One of the account on Account__c is 'Sales_Revenue'. I'd like that account to be the default one on Sale_Account__c.
Here is what I wrote (after help from Peter Knolle ):
trigger ProductSale_Default_SaleAccount on Product_Sale__c (before insert, before update) {
Account__c var = [
SELECT Name
FROM Account__c
WHERE Name='Sales Revenue'
];
for (Product_Sale__c productsale : trigger.New) {
productsale.Sale_Account__c = var.Id;
}
}
But nothing the field is still blank on the when I create a new "Product Sale", I was expecting it to already show the 'Sales Revenue' account.
Any advices?