Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiselect lookup in salesforce using apex

I want to create a multi-select Contact Lookup.

What i want :

When user clicks on a lookup then he should be able to select multiple contacts from that.

What i have done:

I have created an object and a field inside that object using both

"Lookup" and

"MasterDetail Relationship" and

"Junction Object"

When i try to use this Field for any input text/Field then it always provides an option to select only one value from lookup but i want to have an option to select multiple.

Even in the Junction object i have created 2 master-detail relationships still lookup allows only one value to be selected.Moreover it makes the field mandatory which i don't want.

Links that i followed: http://success.salesforce.com/questionDetail?qId=a1X30000000Hl5dEAC

https://ap1.salesforce.com/help/doc/user_ed.jsp?loc=help&section=help&hash=topic-title&target=relationships_manytomany.htm

Can anybody suggest me how to do this.

Its same as we use Email CC/BCC under Send Email option for any Lead.

like image 323
Swati Avatar asked Sep 20 '11 11:09

Swati


2 Answers

Even you use a junction object a lookup is just that, it references (looks up to) one other record: when you create a record on the junction object you still have to set each lookup individually and you're still creating only one record.

Master Detail relationships are essentially lookups on steroids, one object becomes the child of the other and will be deleted if the parent object is deleted, they're not going to provide an interface to lookup to many records at once.

If you're not a developer then your best bet is to either just create on junction object record at a time, or look into using dataloader. You could prepare your data in Excel or similar and then upload all the records into Salesforce in one go.

If you are a developer, or have developers at your disposal, then what we've done in the past is create a Visualforce page to do the job. So if, for example, you wanted to link a bunch of contacts up to an Account, we'd have a single account lookup field on the page, then some search fields relating to fields on the contact. Using a SOQL query you can then find all contacts matching the search parameters and display them in a list, where you may want to provide checkboxes to allow the user to select the contacts they want. Then it's just a case of looping through the selected contacts, setting their Account field to be the chosen account.

There are areas in Salesforce (such as the send Email functionality you mentioned) where it's clear to see that bespoke work has been done to fulfil a specific task — another instance of what you want is in the area where you can manage campaign members. This is the model I've copied in the past when implementing a Visualforce page as described.

Good luck!

like image 61
Matt Lacey Avatar answered Sep 30 '22 11:09

Matt Lacey


For adding multiple junction objects at one time, the only solution we have found is a custom Visualforce page, as described by LaceySnr.

For a slightly different problem, where we need to assign many of object B to object A, We have trained our users to do this with a view on object B. We are assigning Billing Accounts (B) to Payment Offices (A). The view on Billing Account has check boxes on the left side. The user checks the Billing Accounts to be assigned, then double-clicks on the Payment Office field on any of the checked rows. A pop-up asks if you want to update only the single row or all checked rows. By selecting 'all checked rows', the update is done to all of them.

The view is created by the user, who enters the selection criteria (name, address, state, etc.). All user-created views are visible only to them.

like image 36
JanitaB Avatar answered Sep 30 '22 11:09

JanitaB