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
Benjamin Lewis 8Benjamin Lewis 8 

Is there a way to pre-populate a field but let it still be overwriteable?

I am working within the opportunity product section and want to have more fields pre / auto populate with numbers based on the product selection in the same way the standard price does but on other custom fields.  Just like the standard price can be overwritten, I want these to pre-populate but also allow the user to overwrite them.  Any suggestions?
Best Answer chosen by Benjamin Lewis 8
Hemant_SoniHemant_Soni
Hi Benjamin,
The problem with salesforce is we can not update code of save And Add Product button. And without updating button code we can not pre-populate value. But if it is must compulsory then you need to create complete custom page and button which we did this functionality.

Thanks
Hemant

All Answers

Hemant_SoniHemant_Soni
Hi Benjamin,

For pre-population we need to write trigger if you can provided proper name of object and field then i will write trigger for you.

Thanks,
Hemant
Benjamin Lewis 8Benjamin Lewis 8
Hi Hemant, thanks for your help! The following fields are ones I want to pre-populate, the ones to the right in the image are what I want to pre-populate them with based on the Product (Product2) selected when entering the Opportunity Product.  I have the following fields (except quantity which I want to bring in from the opportunity) entered in my Price Book Entry object tied to each Product2.

Is this enough information to work with?  Thanks again!!

Picture of Objects and Fields needing to be pre-populated and the source fields
Benjamin Lewis 8Benjamin Lewis 8
Hi, are there any suggestions for how I can pre-populate certain fields by referencing other fields but keep the fields over-writeable?
Hemant_SoniHemant_Soni
Hi,
Sorry For late reply. Please use below code and change name of field's.
trigger PrePopulateOpportunityProduct on OpportunityLineItem (before insert) {
    Set<Id> setOfOppId = new set<Id>();
    Set<Id> setOfPriceBookId = new set<Id>();
    map<Id,Opportunity> mapOfOpportunityIdWiseOpportunity = new map <Id,Opportunity>();
    map<Id,PricebookEntry> mapOfPriceBookIdWisePricebookEntry = new map <Id,PricebookEntry>();
    for(OpportunityLineItem OLI : trigger.new){
        setOfOppId.add(OLI.OpportunityId);
        setOfPriceBookId.add(OLI.PricebookEntryId);
    }
    
    for(Opportunity Opp : [Select Id,Name,hemant1993__No_of_Employees__c From Opportunity Where Id IN : setOfOppId]){
        mapOfOpportunityIdWiseOpportunity.put(Opp.id,Opp);
    }
    
    for(PriceBookEntry oPriceBook : [SELECT Id, hemant1993__Implementation_Fee__c, hemant1993__Minimum_Monthly_Fee__c, hemant1993__Annual_Fee__c, hemant1993__Update__c, Name FROM PricebookEntry Where Id IN : setOfPriceBookId]){
        mapOfPriceBookIdWisePricebookEntry.put(oPriceBook.Id,oPriceBook);
    }
    
    For(OpportunityLineItem lineItem : trigger.new){
        if(lineItem.OpportunityId != null && lineItem.PricebookEntryId != null){
            if(mapOfOpportunityIdWiseOpportunity.containsKey(lineItem.OpportunityId)){
                lineItem.hemant1993__EXP_Quality__c = mapOfOpportunityIdWiseOpportunity.get(lineItem.OpportunityId).hemant1993__No_of_Employees__c ;
            }
            if(mapOfPriceBookIdWisePricebookEntry.containsKey(lineItem.PricebookEntryId)){
                lineItem.hemant1993__Implementation_Fee__c = mapOfPriceBookIdWisePricebookEntry.get(lineItem.PricebookEntryId).hemant1993__Implementation_Fee__c;
                lineItem.hemant1993__Minimum_Monthly_Fee__c = mapOfPriceBookIdWisePricebookEntry.get(lineItem.PricebookEntryId).hemant1993__Minimum_Monthly_Fee__c;
                lineItem.hemant1993__Annual_Fee__c = mapOfPriceBookIdWisePricebookEntry.get(lineItem.PricebookEntryId).hemant1993__Annual_Fee__c;
                lineItem.hemant1993__Update__c = mapOfPriceBookIdWisePricebookEntry.get(lineItem.PricebookEntryId).hemant1993__Update__c;
            }
        }
    }
}
Please let me know if any issue you are facing and if it help then mark it solved.

Thanks,
Hemant
 
Benjamin Lewis 8Benjamin Lewis 8
Hi Hemant, I appologize, I just saw that you had replied.  Can you show me the path to where I would enter this code?  Thanks!
Benjamin Lewis 8Benjamin Lewis 8
Hi Again, I found out where to add the trigger (opportunity product).  I added your code and changed the field names - the code works to populate the Opportunity Products Section when on a selected opportunity.  However, I was looking for code that would pre-populate the salesforce user input box that pops up after adding a new opportunity -> click "save & Add product" button -> check the product(s) you want on the product selection page for the opportunity -> click select and then it has the user input page for the product(s) you selected where it prompts you to enter the quantity, sales price (pre-populated naturally but overwriteable) plus the other custom fields I had added (annual fee, minimum monthly, etc) that I was also hoping to pre-populate like the sales price pre-populates here.  Basically, I would like your code to apply here but not on the opportunity product page within salesforce itself were you to click the opportunity and then click the product name.  Does that help / make sense?
  User-added image
Hemant_SoniHemant_Soni
Hi Benjamin,
The problem with salesforce is we can not update code of save And Add Product button. And without updating button code we can not pre-populate value. But if it is must compulsory then you need to create complete custom page and button which we did this functionality.

Thanks
Hemant
This was selected as the best answer