5

We have a country and district text field on the User object. We have country and district pick list field on the opportunity object.

When a user creates a new opportunity, the very first time, we want the Country and District pick list to be auto populated matching their country and district field on their User record.

I can write a before Insert/upd trigger to auto populate and after they hit Save, but I could not figure out a way to auto populate the Country and District picklist on the opportunity the very first time, when they try to create it..

Any suggestions; we are using the standard Oppty page layout not VF; we dont want to take that route just to accomplish this request..

  • does it matter if the picklist is populate on save (or) do you want the picklist option to be visible when the user is creating the new oppty? – Rao Jul 8 '15 at 16:23
  • Should be populated and visible on the page layout as soon as I hit "New" button.May be I should have phrased my question better; The country and district picklist field will be displayed on the page layout when I hit "New" button for Opportunity. When the Opportunity page loads (after hitting "New" button), the country and district picklist should have the value selected as the User's country and district at that stage. Also provide user the option to overwrite it. (before they hit Save) – user5352 Jul 8 '15 at 17:21
  • "Should be populated and visible on the page layout as soon as I hit "New" button" I think to do that, you have to either override the new button the pass the values via URL or override the page layout as a visualforce page – benahm Feb 28 '17 at 11:21
1

If I were you, I'd either create a custom button that populates those picklist fields with the values from the running User's record OR create a visualforce page and extension to override the "New" action on the Opportunity object.

Either way, you'd just need to prefill the URL with the parameters from the User record.

1

I suggest this solution

  1. Add new values on the country & district picklist for example

--CurrentUserCountry--

--CurrentUserDisctrict--

  1. Set those values as the default values for country & district picklists on the Opp

  2. Create a process builder that fires on Opportunity Create

  3. When the country & district are still set to the default values, update them through the process builder to the user's country & district

If the values are different from the default values that means the user changed them, so you don't need to override the values

0

A Process Builder would solve this. You could grab it from the Running User and then make the update to your Opportunity. You could have it run only on Record Create. The only issue is, if you plan on doing a data load of opportunities, this is not bulkified. Assuming you are, you could still populate the Country and District yourself on your upload and then have a filter criteria on your Process Builder to ignore records that that field is not null.

Steps: Navigate to Process Builder

On the "Add Object" box, select it and choose Opportunity. Keep "only when a record is created" selected. Press Save.

Click on the "Add Criteria" box, put in the filter (if you want), on when those two fields are NULL. Otherwise, do run every time. Press Save.

Click on the "Add Action" box, select Record Update, click on the Opportunity (funky to get to, its left of the dropdown list where you select it). Now, look for the two fields below to update. When you make the updates, select "Reference" and then navigate to the Running User's fields that you want to reference. Save. Activate. Grab a cuppa tea.

  • Thanks all for your comments..May be I should be rephrased my question better; – user5352 Jul 8 '15 at 17:13
  • Thanks all for your comments..May be I should have phrased my question better; The country and district picklist field will be displayed on the page layout when I hit "New" button for Opportunity. When the Opportunity page loads (after hitting "New" button), the country and district picklist should have the value selected as the User's country and district at that stage. Also provide user the option to overwrite it. (before they hit Save) – user5352 Jul 8 '15 at 17:22
  • Could you do a URL hack and overwrite the button? Have the Button be your own, pass in the District and Country into the page when you click it... that might be the easiest even though its not technically supported by Salesforce. Take a look at this post: kell.screenstepslive.com/s/knowledgebase/m/16056/l/… – David Litton Jul 8 '15 at 18:27
0

I'm going to go out on a limb here and guess you attempted to write your trigger something like this:

trigger SetDistrictAndCountry on Opportunity (before insert, before update) {
    Set<Id> ownerIds = new Set<Id>();
    for(Opportunity o : trigger.new){
        ownerIds.add(o.OwnerId);
    }
    Map<Id,User> ownerMap = new Map<Id,User>([Select Id, Country, District 
                                              From User 
                                              Where Id = :ownerIds]);
    for(Opportunity o : trigger.new){
        o.Country__c = ownerMap.get(o.OwnerId).Country;
        o.District__c = ownerMap.get(o.OwnerId).District;
    }
}

This framework worked for me in Sandbox using different user fields, but it should be the same sort of logic. This would then have the benefit of being properly bulkified in the event you are mass inserting Opportunity records. I would however suggest moving the logic out of the trigger itself and into a handler class as is generally best practice. Plenty of documentation on that here: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

  • Thanks all for your comments..May be I should have phrased my question better; The country and district picklist field will be displayed on the page layout when I hit "New" button for Opportunity. When the Opportunity page loads (after hitting "New" button), the country and district picklist should have the value selected as the User's country and district at that stage. Also provide user the option to overwrite it. (before they hit Save) – user5352 Jul 8 '15 at 17:22
  • Ah, officially I don't think there's a way to do that without using a VF page. You may be able to accomplish this using a custom "new" button that would execute some JavaScript to use the SOAP API to grab the user's District/Country values and reload the parent with some URL hacking, but this wouldn't be supported by Salesforce as these values may change. – Todd Sprinkel Jul 8 '15 at 18:58
0

This solution would require a bit of code but another way to accomplish this task would be to create a lightning component quick action and set your defaults that way. For an example of how to create a record specific lightning component quick action check this link here.

  • This is not available for picklist fields – benahm Feb 28 '17 at 11:07
  • @benahm missed that in the question, updated with an alternative. – Jenny B Mar 29 '17 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.