2

I have a custom object called Sales Rep Location. This object looks up to the Quote and it is filled when creating a new Quote record. What we would like to is have this Sales Rep Location be filled out automatically based on the User creating the Quote.

In order to do this, we would need to create a lookup on the User object to the Sales Rep Location object, so that a User would be able to select which location they belong to. However, Salesforce only allows you to create a Hierarchical Relationship which looks up to the User object and nothing else.

Is it possible to link a custom object to a User object? Is there any other workaround I'm just not seeing that would auto-populate the Sales Rep Location field on the Quote record based on the user's Sales Rep Location? And if so, does it require an apex trigger?

2

You can add an 18 character text field on User that you call SalesRepLocation__c that you can store the required ID in. You just won't get much helpful user interface by default.

You could manually populate that field or build some Visualforce to do it (or buid a trigger if it can be automatically derived). That Visualforce could make use of a proper lookup field to "Sales Rep Location" on some other object so the full search mechanism is available and then the Apex controller code can take that selected ID and save it in your User object text field.

A trigger would then be needed to fill the value in on the Quote:

trigger MyTrigger on Quote (before insert, before update) {
    User u = [select SalesRepLocation__c from User where Id = :UserInfo.getUserId()];
    for (Quote q : Trigger.new) {
        q.SalesRepLocation__c = u.SalesRepLocation__c;
    }
}

You should also consider adding a trigger to User if you want to update Quote if the User.SalesRepLocation__c changes.

0

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.