Displaying multi-subquery, dynamic field, dynamic object SOQL query results on a VF page - salesforce

I want to pass a SOQL query to a page, or have the users enter it on the page themselves, process it, and then display results in a table.
Biggest problem is displaying the results, as VF dynamic binding only seems to be working one level deep, after that it gives null pointer exceptions (seems to be some bug in SF).
I have a dynamic main object with multiple related lists coming from the query, for example: user may be pulling a list of Accounts, with all related Contacts, and all related Opportunities. Here is an example query:
select id, name, BillingState, (select id, name, title from Contacts), (select id, amount from Opportunities) from Account where name like '%Corp%'
Another time, the query might be on a completely different parent object, like:
select id, name, accountId, (select id, Cost from OpportunityLineItems), (select id, name from Attachments) from Opportunitiy limit 20
It's not a problem parsing the field and object names from the query, but using dynamic binding for displaying those results in a table on a VF page is a nightmare, and is not working for me. Any ideas? or maybe you have seen VF code for this specific situation somewhere?
As a side note, below is the error i keep running into on the VF page when trying to use dynamic binding
Error: java.lang.NullPointerException
Error: null

In these situations I always rely on an object relational mapper pattern, or referred to simply as a wrapper class. For the trickier fields, what you wind up displaying are single level, class member abstractions rather than fields directly from the result of a SOQL statement. One benefit of this is that you can simply do a few SOQL calls, prepare the data in any way that will best support your page and then render it smoothly. The additional work of having this abstraction class pays off very well in the end - imo.
Here's a related post that shows exactly how to do this in Apex. In your case, you would extend on this example to add the values of multiple SObjects to one instance of this "ORM-style" class and then populate a list of instances of it from within Apex. This list of custom object instances becomes perfect food for Visualforce to consume.

Related

Grab value from unrelated object where two fields match

I have two objects - "Account" and "Appointment". I'm trying to pull the value of the field "Status" from the "Appointment" object where "Account.Initial_Date" matches "Appointment.Date_Time". I initially tried making a new field in the "Account" object to return a text field and see if maybe it would return the first value:
Appointment__c.Status__c
Which results in the error:
"Field Appointment__c does not exist. Check spelling."
I was told that it's too difficult to link from "Appointment" to "Account" because there can be multiple appointments per account, which is why I'm trying to link based on the date fields. My next attempt was using VLOOKUP, but I read that this only works between custom objects, and I think I'm working with standard objects here... what kind of solution should I be looking for?
Adding the tag apex here in case this can only be achieved via a script of some sort - if that's the case, I'll make attempt via that.
I was told that it's too difficult to link from "Appointment" to "Account" because there can be multiple appointments per account
This is incorrect. That relationship appears to be exactly the same as that between Contact and Account - one Contact, many Accounts. It's a very common relationship pattern in Salesforce.
If an Appointment is logically related to an Account, it should have a relationship field referencing the Account object to which it is related.
However, having a one-to-many relationship does not mean you can trivially represent specific data points from the many side to the one side. The native tool to do so is the Roll-Up Summary Field, but it does not apply to your use case.
There's really three ways to implement your objective, which is essentially implementing a variant of a roll-up summary. VLOOKUP(), which works only in Validation Rules, does not apply here.
Write two Apex triggers (one on Account and one on Appointment) to react to all changes that would influence what value should appear in the Account__c.Status__c field.
Write equivalent Process and Flow declarative automation, which cannot get 100% of the way there because Process Builder and Flow cannot react to delete events.
Use the free and open source Declarative Lookup Rollup Summaries application to define a roll-up summary. DLRS can populate a field from the child object (Appointment) to the parent (Account) based on a sorting by another field (Date_Time__c).

soql getting data via a junction object

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.

Compare two views in salesforce

I am looking for a way to compare two views in salesforce. I want to create a visual force page that lets a user select two views associated with the Account object and show all the accounts that appear on both views.
I am struggling pretty hard here, I can't figure out how to get the results from the views, but I am hoping there is a way to get all accounts that match the filters for each view.
Here is my SOQL query:
Select Id, Name, Owner.Name FROM Account WHERE
Id IN ( SELECT AccountId FROM Opportunity WHERE RecordTypeId = :RecordType1ID AND StageName IN :StageOneList )
AND Id IN ( SELECT AccountId FROM Opportunity WHERE RecordTypeId = :RecordType2ID AND StageName IN :StageTwoList )
This is the basis of the VF page I have made so far. It is possible to filter the Account with Account Owner and a drop down list from province. The idea is, many people in the organization have already created views with the accounts filtered as they need it. Instead of including every possible account field as a filter, I would like a drop down list of the active users views associated with Account, and then they can select Opportunity 1 and Opportunity 2 and have a list of Accounts matching.
I assume you mean views as in the available views in the dropdown box on a standard tab for an object? If so I don't believe you can query the results from them directly although you can query the Account object using a SOQL statement where you provide the filter.
My suggestion would be either create a set VF page that has 2 drop downs to switch the SOQL query that is used to return the list of accounts being displayed (would mean you have a set of predetermined views and updates to them require code updates) or give more details of your use case and we may be able to provide other suggestions.
It sounds like you just need to compare the results of the filters here. My suggestion would be that you're really trying to do something that should be done with reports, not with views.
Put two enhancedList components on the page.

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.
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.

Order data using values stored in another table

I've been using cakephp for a while, but have not learned all the ins and outs yet so I may be missing something simple. Or the problem may lie with my database structure. Either way, if anyone has any idea of what I'm doing wrong, please share.
Is there a way to order the data returned by cakephp's find using values stored in another table?
I am creating custom form fields on a per category basis, so when I choose a particular category to post in, custom fields will be added to my form. I have 3 tables: Posts, Fields, and Answers. The Posts table stores the basic static information for the post, such as id, category_id, title, and description. The Fields table stores the custom field data, such as category_id, field_label, field size, etc. The Answers table stores the values that are entered for particular fields, such as post_id, field_id, value.
I am trying to display the posts for a particular category, and create html table headers on the fly, using select fields, set by a column toggle in the fields table, and also select the answers associated with that particular field and post.
I am able to select all the data I want, and paginate everything just fine, but what I can't seem to figure out is how to order the data using one of the dynamic column values. For example, if I have year, make, and model as 3 custom fields, I would like to click the year column to sort my results by the year values, and if I click the make column, I would like to sort my results by the make values, etc.
I know how to order the results by a particular field inside the posts table, such as id or title, but is it possible to order using the custom fields? Am I setting up the database and/or something else wrong, and if not is there are particular cakephp method or sql command that I need to use in order to sort by the custom fields? I'm not really well versed in complex sql commands.
Thanks.
I'd suggest you pass the field name and sort direction in the URL (GET param). So when you have your table header link, form it so that it links to a URL as so:
http://somesite.com/pages/index/sort:customfield1/dir:asc
Then when you're grabbing the data from the db in your find() query, include the named parameters as the order parameter that can be sent to find.
You'll need to determine a default sorting column and direction. Maybe have that be selectable with a boolean field in the schema -- if there are no parameters sent to the action above, pull the field from your other table that has default set to true in the record.
To clarify: when a user visits a given action, first you'll pull the custom fields from the other table. Then using those fields (either the default as mentioned above, or the named params passed in the URL) form the query for the actual data, using the order parameter.

Resources