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
SamirKhanSamirKhan 

autoPopulate lookup field

I have two custom Object , Salesorder and SalesYear.SalesYear has two records - 2015 and 2016.SalesOrder Has a lookup to Salesyear.What i am trying to achieve is when i click on new salesorder , it should auto populate a value for salesyear i.r 2016
For this i have written a trigger, and it doesnot works for some reason which i am not aware of.
Can some one guide me in achieving this?
 
trigger SalesOrderTrigger on gii__SalesOrder__c (before insert, before update) {



    Sales_year__c objSales_year = new Sales_year__c();
    objSales_year = [select id from Sales_year__c where name = '2016'];
    for(gii__SalesOrder__c objOrder : trigger.new){
        objOrder.Sales_Year__c= objSales_year.id ;
    }
}

 
Head In CloudHead In Cloud
If you want to pre populate a field, this can not be done by using triggers. You should use URL hacking for this. Find an example here:
http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html
v varaprasadv varaprasad
Hi Samir,


Through trigger we cannot auto populate field values.

but we can set predefined values like

Sample :
trigger test account(before insert){
for(account a : trigger.new{
    if(a.type == 'prosepect'){
         a.rating = 'hot';
    }
  }
}

So whenever new records inserted automatically rating == hot ;

 
Nish321Nish321
If you want to prepopulate the values of sales year on button click, URL hacking is the only solution.