+ Start a Discussion
ifitwalazambia1.3879265308021304E12ifitwalazambia1.3879265308021304E12 

default value

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:

trigger ProductSale_Default_SaleAccount on Product_Sale__c (before insert, before update) {
             Account__c defaultSaleAcc = [SELECT Name FROM Product_Sale__c WHERE Name='Sales Revenue'];
            for (Product_Sale__c productsale : trigger.new) {
                        productsale.Sale_Account__c = defaultSaleAcc.Name;
            }
}

And I get this error: Compile Error: Illegal assignment from LIST to SOBJECT:Account__c at line 3 column 9

Any advices?
trsmithtrsmith
A few things to start:
  1. The error you are getting is because the query [SELECT Name FROM Product_Sale__c WHERE Name='Sales Revenue'] is returning a List<Account__c> not an Account__c object because there could be multiple accounts with that name. You could update the query to the following: [SELECT Name FROM Product_Sale__c WHERE Name='Sales Revenue' LIMIT 1]
  2. You need to set the Id in productsale.Sale_Account__c, so you'll need to change that to productsale.Sale_Account__c = defaultSaleAcc.Id;
  3. If this is intended to only provide a default value for that field, you probably want the trigger to only fire before insert. Firing on before update will always set the field to that value even if someone is trying to change it.
Those changes aside, a few other suggestions:
  1. You might consider hierarchical custom settings for storing the default account you want to set rather than query by name. This way, you could be guaranteed to set the right value even if someone adds another account by the same name. Also, this would allow for different default values depending on user roles, etc. For more info on custom settings, see here: https://help.salesforce.com/apex/HTViewHelpDoc?id=cs_about.htm&language=en
  2. If you decide to stick with the query, you should consider a try/catch or use a for query so that if there is no account found, you don't get an exception. Doing so could look like this:
trigger ProductSale_Default_SaleAccount on Product_Sale__c (before insert) {
            Id defaultAccId;
            for(Account__c a:[SELECT Id FROM Product_Sale__c WHERE Name='Sales Revenue' LIMIT 1]) {
                        defaultAccId = a.Id;
            }
            for (Product_Sale__c productsale : trigger.new) {
                        productsale.Sale_Account__c = defaultAccId;
            }
}
harsha__charsha__c
Adding up to @trsmith:

trigger ProductSale_Default_SaleAccount on Product_Sale__c (before insert) {
            Id defaultAccId = null;
    > for(Account__c a:[SELECT Id FROM Account__c WHERE Name='Sales Revenue' LIMIT 1]) {   // You need to have query on Account__c
                        defaultAccId = a.Id;
            }
            for (Product_Sale__c productsale : trigger.new) {
    >           if(productsale.Sale_Account__c != null && defaultAccId != null)// Added by assuming default value comes only when no value entered 
                     {
                               productsale.Sale_Account__c = defaultAccId;
                      }
            }
}

Regards,
- Harsha