Display results of unknown SOQL query - salesforce

I have a requirement that I'd like to know if it is possible. I have visualforce page which will display the results of a SOQL query, however the SOQL query is dynamic and could be a query on Custom Objects or Standard Obejcts. I'm currently hard coding a SOQL query into the controller class whilst trying to figure out how it will work. I intend to display the results of the SOQL query in a pageBlockTable or dataTable in the Apex.
Is this possible in Salesforce, if so could anyone give me an example of how it would work both in Visualforce and Apex?

You should read about "dynamic references" (also called "dynamic bindings"). A good starting point: http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_sample_standard.htm
Basically if you have String fieldName = 'AccountNumber';, later in Visualforce you can refer to it directly:
<apex:outputField value="{!a.AccountNumber}"> or dynamically:
<apex:outputField value="{!a[fieldName]}">.
This is similar to having a.AccountNumber or a.get('AccountNumber') in Apex. Check out for example getting Value of a field by its Name in apex salesforce if you've never seen it.
You'll have to be careful around them because if your base object won't be account it will fail (for example there's no Contact.AccountNumber field). The example above deals with Accounts only but it'd be a good intro anyway.
Once you familiarize yourself with the basic idea you can explore world of fieldsets - predefined groups of fields that should come together - you can use them both for displaying and querying data, basically they're more powerful than hardcoded list of strings with the field names (like in first link) but still same idea.
At least you'll know now what keywords to look for ;)

Related

Cross fromula fields from Opportunity to Quote with custom fields

I'm trying to access some custom fields in the Quote object from Opportunity Object.
I tried to use cross formula fields to access the fields via SyncedQuote, but for some reason not all my custom fields from the Quote Object appear in the dropdown menue.
Is there any reason why only some some custom fields can be linked and others can't?
In general I want to build a VisufalForce PDF formula set up on the Opportunity Objects, for which I need data from the Quote custom fields. Is there any other possibilty to archieve that?
It is important to notice that my company doesnt have the enterprise licence, so no apex code is available. Can this be archived without?
Many Thanks!

paginate an union in Cakephp 3

I'm trying to paginate a union in cakephp 3. I have already found and read How do you modify a UNION query in CakePHP 3?, but am probably too new to cakephp to understand properly what is meant by "use a custom finder". I have read the relevant section in the cakephp book, but still fail to see how this helps to apply the epilog() to the query. Maybe someone could show a complete example?
I have two models Customers and Suppliers. I am trying to builds a search function that returns matching entries from both models, including pagination and sorting.
public function index($q=null) {
$this->loadmodel('Suppliers');
$this->loadmodel('Customers');
$customers=$this->Customers->find('all')->select(['type'=>"'Customers'",'id'=>'id','name'=>'name','phone'=>'phone'])
->where(['name like'=>'%'.$q.'%']);
$suppliers=$this->Suppliers->find('all')->select(['type'=>"'Suppliers'",'id'=>'id','name'=>'name','phone'=>'phone'])
->where(['name like'=>'%'.$q.'%']);
$customers->union($suppliers);
$results=$this->paginate($customers);
$this->set(compact(['q','results']));
}
As detailed in How do you modify a UNION query in CakePHP 3?, this results in the order,limit and offset only being applied to the first select.
Removing the paginate() and applying order,limit and offset manually via epilog() works, but than I'd have to recreate the whole pagination mechanism in my code. While this is my current plan in case I can't find a better solution, I don't believe this would be the right way to do it.
Could someone (#PhantomWatson, #ndm) show an example, how the suggested custom finder can solve this?

Show Opportunity related Contacts in Custom Object Field

I have the next issue.
I have a custom object called 'Application', and I have this requirement:
"Show all Contacts related to an Application. Create a field on Application object, must be read only".
I solve it with apex code. 'Application' has a lookup to Opportunity, Opportunity to Account, and all my contacts have AccountId, so this way, I get all the contacts using apex code in a trigger.
But, I've been ask to change this to a Formula field in Application object.
So, my issue is next. I'm not able to get all contacts with advance formula editor, because they're not part of any object. I have no master-detail relationship.
Does any one know how can I achieve this using configuration? I should not use apex code for this req.
Thank in advance guys.
I don't think you can do it.
In formulas / merge fields syntax there's no way to go "up, up then down" (Application -> Opportunity -> Account -> down to Contacts related list). There's also nothing that would let you loop through Contacts (and display what? Ids? Names? Emails?). Roughly speaking you can only go up through dots.
You might want to explore path of "cross object workflow" rules but I imagine that when I add a new Contact to Account it should somehow "spread itself" to all related Applications? There's no straight way to fire a workflow on delete too - so you'd eventually end up with inaccurate list.
I'd say trigger was a good solution. Maybe it ws unoptimized but if it has to be in a field - tough.
There might be a fairly simple way of achieving that by embedding a visualforce page within Application page layout.
This should be doable with pure Visualforce (so technically there will be no Apex code ;))
Something as simple as
<apex:relatedList list="Contacts" subject="Application__c.Opportunity__r.AccountId" />
would be a good start (if you want your own layout and not a rel. list - you should be still able to pull it off with <apex:repeat> or <apex:pageBlockTable>.
There's one BUT here: it's not a field, just a display trick. Forget about using it in reports, mobile applications etc.
Another way - would it be acceptable to be 1 click away from these contacts? You could make a report "Account with Contacts", filter it by Id of one Account and later use "URL hacking" to change the filter depending on from which Application you'll click it. This link could be either a formula field or a real custom button/link. Technically - it's pure config, no apex & VF.
You can read more about URL hacking at Ray Dehler's excellent post and specifically about dynamic Reports here or here.

Salesforce.com visualforce between 2 objects

In salesforce I need to create a visualforce page that includes the fields of 2 objects. The first object is the QUOTE object. The second objects is a custom object with several fields.
I want to create a visualforce page that shows the records of both QUOTE and the new object. Can I do this without creating a custom controller? If no any hints on the code for this new controller?
Can I do calculations between fields in a visualforce page?
Ideally I want this page to appear as soon as the QUOTE is set to ACCEPTED
1: You can't do this without a custom controller unfortunately, unless one object is related to the other and you're just happy displaying it as a related list on the parent object's page. For calculations you could use rollup summaries for some basic sums etc..
As for a custom controller, have a look at field sets for a super easy way to get fields into a VF page, you essentially configure groups of fields on your objects and then you can stick those groups onto a page with minimal markup.
2: For fields with complex calculations you'll want to do the sums in the controller and then expose the results through variables onto the page in the usual manner.
3: Not really possible without creating a custom edit page in the first place — you'd be better off having a button on the quote page to open up the Visualforce page, that page can simply display an error if the quote is not yet accepted. There are some other alternatives that might work though, like using a forumla field to generate a link to the page when the status is as you desire.
I'm happy to elaborate on any of this, but the fact that you're asking about number 2 would suggest to me that you don't have much experience developing on the platform (not a dig, just an observation), so unless you're comfortable coding in other environments you could find this quite tricky. That said, you're on stackoverflow so I'm thinking you probably know a little about coding at least!

Salesforce SoftPhone - Relate Call to Records in Two Different Objects

I am trying to relate phone calls to multiple records in Salesforce. The records are in two different custom objects, but you can think of the records like a Lead or an Opportunity. Our salespeople call multiple people (Leads) for given Opportunities, and I'd like to capture both pieces of information.
Is this possible in Salesforce? My current layout has a "Related To" tab that populates when you navigate to a given record, but it forces you to choose one record before auto-saving. Is it possible to have a format that maps the Activity generated by the call to multiple records?
This could be a format with multiple drop-down "Relate To" menus. Better yet, I could use a field that allows you type a record number in to to a free-response field to relate it to an Opportunity.
Thanks!
Austin
P.S. We are running Salesforce Professional with API. We are using a CTI adapter from BroadSoft.
I don't know the BroadSoft adapter and I couldn't find it on Salesforce AppExchange to give it a test run. But assuming it's a typical plugin for Salesforce, you'll have little control over it (i.e. cannot add a helper junction object that would act like many-to-many relationship bridge).
You should have no problems writing a piece of Visualforce to suit your needs though (especially the later requirement - use some text filter to display list that matches this filter).
I'll write a quick example. My business requirement for the sake of this example is "I want to show all Activities whose Subject field matches text I've put on new field in Opportunity, regardless whether they are linked or not. This should appear as similar to normal related list on Opportunity".
Add text field called "record" to your Opportunity object.
Create an Apex class:
public class AustinTest {
private Opportunity o;
public AustinTest(ApexPages.StandardController controller) {}
public List<Event> getActivities() {
Opportunity o = [SELECT Id, Record__c
FROM Opportunity
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
String searchTerm = '%' + o.Record__c + '%';
return [SELECT Id, Subject, Type FROM Event WHERE Subject LIKE :searchTerm];
}
}
Create a Visualforce Page:
<apex:page standardController="Opportunity" extensions="AustinTest">
<apex:pageBlock>
<apex:pageBlockTable value="{!activities}" var="a">
<apex:column value="{!a.Id}" />
<apex:column value="{!a.Subject}" />
<apex:column value="{!a.Type}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Go to the Page Layout editor for Opportunities and drop the Visualforce somewhere on the page. We have to drop it in the "detail" area (like normal fields, not like Related Lists), that's fine for now.
Experiment with it, decide if it's worth spending time on. With extra bit of tweaking we can make it display similar to normal Related List. I'll write more if needed. How would you like to have this list of matching records accessible anyway? As a separate tab? Related List? Something else?
Is it possible to have a format that maps the Activity generated by the call to multiple records? This could be a format with multiple drop-down "Relate To" menus.
The most concise answer: no. The drop down list and association is not modifiable.
There are ways to do this, but it requires either customization, eyescream post- it has some great info on how to do that. And you may need your CTI vendor to do portions of the customization.
Note: make sure you use CTI 2.0+ (preferably CTI 3.0+). This has lots of improved features for improving call logging and handling. See the wiki here:
http://wiki.developerforce.com/index.php/CTI_Toolkit
Are the calls stored in their own object? If so, you could add lookup fields that look up to each object you want to link to the call. If the calls aren't saved in their own object, I would recommend using a custom object to store them in as separate records. You can use lookup fields to link them to whatever objects you want, and you can display them as items in a related list on the record pages of those objects.
As an example, say I have a custom object called "Calls". The object would contain the following fields:
Call name (text field)
Opportunity (lookup field)
Lead (lookup field)
etc...
Then you can add a "Calls" related list to both Opportunities and Leads, and all call records that look up to that Lead or Opportunity record will be displayed in the list on both records.
Hope this helps!

Resources