1
votes

my case :- i have two custom objects order-master and Sellar- Products have look-up relation on order-master. when i select sellar- product i need to populate its mrp__c, Selling_price_cc in order-master. i have written a trigger for this(working) and getting values after save the sellar-master record but i need values before submit record.

trigger AutoPopulate on Order_Master__c (before insert, before update) {
Set <ID> SetSpIds = new Set<ID>();

for(Order_Master__c om : trigger.new){
if(om.SellerProductId__c != null){
 SetSpIds.add(om.SellerProductId__c);
 }
   } 

   MAP<ID, Sellar_Products__c> mapSp = new MAP<ID, Sellar_Products__c>
    ([select MRP__c, Offer_Rate__c, Selling_Price__c from Sellar_Products__c 
             where id in:SetSpIds]);
  for(Order_Master__c om : trigger.new){
 if(om.SellerProductId__c != null){
 Sellar_Products__c Sp = mapSp.get(om.SellerProductId__c);
 om.Item_Price__c = Sp.MRP__c;
 om.Discount__c = Sp.Offer_Rate__c;
  om.Total_cost__c = Sp.Selling_Price__c;
    }

 }
}
1

1 Answers

0
votes

You can't set such values from the trigger as triggers don't have UI. If you want to have some pre-filled value in it you can create a new VF page with a controller and override the default button on the layout for this Object.

You can see in the below link a good example of how you can do the VF using PageLoadEvent action and pre-fill the data you want. Note that the suggestion for setting Default values from the field settings is not the best solution as it may backfire in some cases but you can give it a try too as it is the fastest and easy to try solution.

https://salesforce.stackexchange.com/questions/30231/how-to-set-initial-value-of-any-field-on-standard-record-via-trigger