I have 2 custom objects in Salesforce.com
One is PersonAccount and one is Accounts.
Within the default "Account" object I have a field called user_id
PersonAccount acts as a junction table to link "Account" to Accounts
PersonAccount does a lookup in Person for user_id Lookup(Account)
How can I build a query to check something in Account to find all the matching items in Accounts?
Currently, Salesforce only permits a single level of nested queries. It cane be done like the following:
[SELECT ID, Name, Field1 from Object__c WHERE Id IN ( SELECT Id FROM Object2__c WHERE Field2 = 'SomeValue')]
However, with the junction object you don't actually need to use a nested query.
Unfortunately, your description isn't clear enough to understand your specific object set-up, so I am going to make some assumptions.
You have three objects, Accounts__c (your custom Accounts Objct), PersonAccount__c (your junction object), and Account (the default Account objects).
The PersonAccount__c object contains two lookup fields (for a true Junction, they should be Master-Detail). The first is to Accounts__c (we will call that lup_cust_accounts__c). The second is to Account (we will call that lup_account__c). [As an aside it is a really bad idea to have an Accounts and Account object. It is going to screw you up because Salesforce will automatically pluralize words and then you will be confused as to which is which.]
Salesforce allows dot relationship lookups in SOQL queries. So if you want to get the ID and Name from custom Accounts Objects when the associated Account object's Name is like "Test", you could do the following:
[SELECT lup_cust_accounts__r.Id, lup_cust_accounts__r.Name FROM PersonAccount__c WHERE lup_account__r.Name LIKE 'Test%'];
Notice the double underscore r instead of double underscore c? That is how you indicate a reference (lookup) rather than the specific field value.
Related
I have the following three custom objects:
Order__c
Design__c (has a lookup to Order and a lookup to Location, so the design ojbect is the junction object)
Location__c
On the order page layout I want to add a blank section that contains a VF page in order to display the Location records for all the design records for an order.
An order can have many designs and a design can have many locations. I need a way to group and display each design/locations in the VF page on the Order.
How can I build this query and display the results on the VF page?
I was trying a query like this: Select Location_r.Name, Location_r.Mockup From Design_c where Order_c = 'xxxxxxxxxxxxxx'
Also, is there a better way to display the results besides a VF page section in a related list?
Thanks for any help!
Regards.
First things, first. since design is related to the other 2 objects via lookup, it is not a junction object. junction objects are only when it's 2 parents have a master-detail relationship with the child.
I think what you're trying to achieve is to traverse a parent-child-parent relationship. You'll need to use subqueries for the last half. From the documentation- here you go: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
Query **child-to-parent relationships**, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator.
For example:
SELECT Id, Name, Account.Name
FROM Contact
WHERE Account.Industry = 'media'
This query returns the ID and name for only the contacts whose related account industry is media, and for each contact returned, the account name.
Query **parent-to-child**, which are almost always one-to-many. Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause. Note that for subqueries, you should specify the plural name of the object as that is the name of the relationship for each object.
For example:
SELECT Name,
(
SELECT LastName
FROM Contacts
)
FROM Account
The query returns the name for all the accounts, and for each account, the last name of each contact.
I've been thinking about creating a database that, instead of having a table per object I want to represent, would have a series of generic tables that would allow me to represent anything I want and even modifying (that's actually my main interest) the data associated with any kind of object I represent.
As an example, let's say I'm creating a web application that would let people make appointments with hairdressers. What I would usually do is having the following tables in my database :
clients
hairdressers: FK: id of the company the hairdresser works for
companies
appointments: FK: id of the client and the hairdresser for that appointment
But what happens if we deal with scientific hairdressers that want to associate more data to an appointment (e.g. quantity of shampoo used, grams of hair cut, number of scissor's strokes,...) ?
I was thinking instead of that, I could use the following tables:
entity: represents anything I want. PK(entity_id)
group: is an entity (when I create a group, I first create an entity which
id is then referred to by the FK of the group). PK(group_id), FK(entity_id)
entity_group: each group can contain multiple entity (thus also other groups): PK(entity_id, group_id).
role: e.g. Administrator, Client, HairDresser, Company. PK(role_id)
entity_role: each entity can have multiple roles: PK(entity_id, role_id)
metadata: contains the name and type of the metadata aswell as the associated role and a flag that describes if its mandatory or not. PK(metadata_id), FK(metadata_type_id, role_id)
metadata_type: contains information about available metadata types. PK(metadata_type_id)
metadata_value: PK(metadata_value_id), FK(metadata_id)
metadata_: different tables for the different types e.g. char, text, integer, double, datetime, date. PK(metadata__id), FK(metadata_value_id) which contain the actual value of a metadata associated with an entity.
entity_metadata: contains data associated with an entity e.g. name of a client, address of a company,... PK(entity_id, metadata_value_id). Using the type of the metadata, its possible to select the actual value of a metadata for this entity in the corresponding table.
This would allow me to have a completely flexible data structure but has a few drawbacks:
Selecting the metadatas associated with an entity returns multiple rows that I have to process in my code to create the representation of the entity in my code.
Selecting metadatas of multiple entities requires to loop over the same process as above.
Selecting metadatas will also require me to do a select for each one of the metadata_* table that I have.
On the other hand, it has some advantages. For example, instead of having a client table with a lot of fields that will almost never be filled, I just use the exact number of rows that I need.
Is this a good idea at all?
I hope that I've expressed clearly what I'm trying to achieve. I guess that I'm not the first one who wonders how to achieve that but I was not able to find the right keywords to find an answer to that question :/
Thanks!
I need to get a list of users whose role has the first 5 char as 'Sales'
I want to use this on a apex class
My first thought was to use something like this
select id from user where UserRoleId__r like 'Sales%'
But this throws an error
No such column 'UserRoleId__r' on entity 'User'.
Do i need to query the role object to get all ids and then query the user object using those ids?
is there a better way of doing this?
Thanks
You need to fix the name of the User Role column in your search. Here's is the correct syntax:
select id from user where userrole.name like 'Sales%'
You only need to append the __r suffix to relationships in queries when the object is custom. For built in objects, you only need the name of the object (no __r).
My client requested to have a field that can be a lookup on either contacts or users.
I know that Salesforce does something like this in some standard objects. For instance, the Task objects has fields "who" (lookup on account or contact) and "what" (lookup on case, opportunity, and more stuff). Also, I know the Owner field on Case can reference either a User or a Queue.
Can I make custom fields that look up on more than one kind of object?
You can't create a custom lookup field that references multiple types. You would have to create 2 different fields.
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§ion=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.
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!
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.