Writing a Custom Controller extension to get related records and iterate over list/index and use with apex:repeat - salesforce

I have 3 custom objects with a Master-Detail Relationship and Lookup Relationship.
CustomA__c (related CustomB__c) <-> CustomB__c <-> CustomC_c (related CustomB_cc)
I´ve built a Visualforce page with HTML table to replicate a PDF document and a Custom Controller Extension, so far so good.
But I´m just a beginner with apex coding.
The problem is that I need to replicate the document as it is, when there are no related records for CustomA__c or less then 5, it should still show the full table (the empty rows). Max. rows/related records on the document is 5, no second page needed.
Currently I´m trying to accomplisch that by using apex:variable and apex:repeat as I´ve seen some examples, but perhaps there is also another solution. For the Visualforce page I already wrote the code for the rows with data and another apeax:repeat for the empty rows.
But I´m really strugling with the controller, i know i need to iterate over the list, the code that i already wrote is also put together out of examples as i just don´t understand it yet good enough.
Any help would be appreciated! Thanks, Josip
public with sharing class CustomAController {
public CustomA__c customa{get; set;}
public CustomA__c ca{get; set;}
public CustomAController (ApexPages.StandardController controller) {
ca = (CustomA__c )controller.getRecord();
customa= [SELECT Id, Name FROM CustomA__c WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
}
public List<CustomB__c > getrelatedCustomB() {
List <CustomB__c > cbList = New List<CustomB__c >(5);
for(CustomA__c acc:[SELECT Id, Name, (SELECT Id, Name, ... , CustomCfield__r.Name FROM CustomBs__r ORDER BY Name LIMIT 5) FROM CustomA__c WHERE Id = :customa.Id]){
for(CustomB__c cb:acc.CustomBs__r)
cbList.add(cb);
}
return cbList;
}
}

You can dramatically simplify your code by writing a direct query on the child object instead of a parent-child SOQL query.
public List<CustomB__c > getrelatedCustomB() {
return [SELECT Id, Name, ... , CustomCfield__r.Name
FROM CustomB__c
WHERE CustomA__c = :customA.Id
ORDER BY Name
LIMIT 5];
}
There's no need to iterate in Apex here.

Related

Laravel multiple user id tables

I'm pretty new to laravel and have a really basic question related to relationships.
Here is an example of my question:
I have a migration called money_transfers.
The migration contains the following things:
user_id (transfer sent by)
sentTo_id (transfer sent to)
amount
sent_at
BOTH user_id and sentTo_id refer to a User ID.
Now, what I want to do is the following:
Fetch the user the money was sent TO the same way as the user the money was sent BY. Just like in the example below:
$transfer->sentTo->name
or
$transfer->sentTo->id
You get what I mean. Thanks in advance :)
If you defined your foreign keys correctly in your migration table, Then it's just a matter of defining the right relationship:
class MoneyTransfer extends Model
{
public function sentBy()
{
return $this->belongsTo(User::class,'user_id');
}
public function sentTo()
{
return $this->belongsTo(User::class,'sentTo_id');
}
}
This way you can access the receiver attribute like this:
$transfer->sentTo->name;
And the sender attribute like this:
$transfer->sentBy->name;

How do I get info from lookup in salesforce query

I have a custom salesforce object Installation__c and it has a custom field Product__c which is a lookup to a custom object Product__c I am trying to get the fields from the child object using these query:
public with sharing class InstallationController {
#AuraEnabled
public static List<Installation__c> getItems() {
// Perform isAccessible() checking first, then
return [SELECT Id, Name, Installation_Display_Name__c, Product__c, Status__c, (SELECT Product__c.Name FROM Product__c) FROM Installation__c];
}
}
I get the error:
Didn't understand relationship 'Product__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
I have tried changing the Query to
FROM Product__rand FROM Product__c__r but neither seems to work, how do I fix my query?
If you're traversing up or down a relationship hierarchy, the __c suffix becomes __r (r for 'relationship') until you finally get to the field that you're looking for (which still ends in __c if it's a custom field). So in your case, it will be
public with sharing class InstallationController {
#AuraEnabled
public static List<Installation__c> getItems() {
// Perform isAccessible() checking first, then
return [SELECT Id, Name, Installation_Display_Name__c, Product__r.Name, Status__c FROM Installation__c];
}
}
So, the change here is, for the relationship you have to use Product__r.Name
Click into the relationship on the object that has the look up. Copy the relationship name adding __r to it
This example would be Test_Drives__r

Dapper.Contrib: How to get a row by filtering on column other than ID?

My class is like below:
[Table("tblUser")]
public class User
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
}
Using Dapper.Contrib, is there a way to get the User record by Title instead of Id?
If I query like below, it works. But, I want to filter on Title column which is not a key.
await connection.GetAsync<User>(Id);
Looking at the documentation, Dapper.Contrib does not support retrieval of records using criteria other than key. In other words, it does not support any kind of predicate system in its current implementation.
Using GetAll, you can further filter it with linq. But remember that this is not being executed on RDBMS. It will be executed on application side or in memory. That means, entire data will be loaded first and then it will be filtered.
Personally, I will choose to use Dapper (bypassing Contrib) for such specific scenario. Other part of the project will still use Contrib.

Selecting data from multiple tables in MVC

I'm am in the middle of creating my first, substantial .NET MVC application. I have come across a problem and I am not quite sure the proper way to go about it.
In my application I have quite a large database. For a number of features I need to select data from up to 5 tables and send it back to the view, and I am not quite sure how to go about it since view takes either Models or View Models?
I understand the concept of View Models quite well, but is creating one every time I need to send data from multiple tables the only solution to this? And if so could anyone tell me the best practices when doing it
Thanks in advance for any help
Yep, you'll have to have a View Models per view. I work on application with about 600 views and we tried re-cycling view models and it ended up in tears. Now there is a model for each view (mostly).
To send data from multiple tables you'll need to run joins on your tables and select into a view model.
Here I presume you use Entity Framework:
public class ComplexViewModel
{
public String Name { get; set; }
public String Category { get; set; }
public String Level { get; set; }
}
var db = new MyDbContext();
var result = from name in db.Names
join category in db.Categories on name.CategoryId equals category.CategoryId
join level in db.Levels on category.LevelId equals level.LevelId
select new ComplexViewModel()
{
Name = name.Name,
Category = category.CategoryName,
Level = level.LevelName,
};
return result.ToList();
More examples of joins can be found are recommended to review.

Salesforce Apex - Populating an object from SOQL query

I have a simple SOQL query that returns information relating to a Contact and CampaignMember. I'm attempting to populate a custom object with the results of the SOQL query. However I get the following error when loading the Visualforce page:
Invalid field campaign.name for CampaignMember
List campaignMembers = [select campaign.name, contact.id,contact.firstname, contact.lastname, status, campaignId from CampaignMember where contactId = '003U000000U0eNq' and campaignId in :campaigns];
for (Integer i = 0; i < campaignMembers.size(); i++) {
results.add(new CampaignMemberResult(
(String)campaignMembers[i].get('CampaignId'),
(String)campaignMembers[i].get('campaign.name'),
true
));
}
I've ran the SOQL query seperately in the Developer Console and it queries successfully. Why can I not pull in the campaign.name from the SOQL query within the for loop?
The error you see is caused by the fact you should write it as campaignMembers[i].Campaign.Name. Or if you insist on the getter syntax, campaignMembers[i].getSobject('Campaign').get('Name').
Any special reason you need the wrapper object (or whatever CampaignMemberResult is)?
I have strange feeling you're writing too much code to achieve something simple ;) The syntax with campaignMembers[i].Campaign.Name will also mean you don't have to use casts to String.
Plus - if you need to know "in which campaigns does this Contact occur" you have 2 ways:
flat
select contact.id,contact.firstname, contact.lastname,
campaignid, campaign.name,
status
from CampaignMember
where contactId = '003U000000U0eNq'
subquery
From contact you go down to the related list of campaignmembers, then up to campaigns to get their names
SELECT Id, FirstName, LastName,
(SELECT CampaignId, Campaign.Name FROM CampaignMembers)
FROM Contact WHERE Id = '003U000000U0eNq'
Example how to use "flat" result straight in visualforce (without CampaignMemberResult):
Apex:
public List<CampaignMember> flatMembers {get;set;} // insert dick joke here
flatMembers = [select contact.id,contact.firstname, contact.lastname,
campaignid, campaign.name,
status
from CampaignMember
where contactId = '003U000000U0eNq'];
VF:
<apex:pageBlockTable value="{!flatMembers}" var="cm">
<apex:column value="{!cm.Contact.LastName}" />
<apex:column value="{!cm.Status}" />
<apex:column value="{!cm.Campaign.Name}" />
EDIT
My end goal is a Visualforce page to be displayed on the contact
record showing a list of all campaigns with a checkbox alongside each
indicating if the contact is a member or not.
You do realize it can quickly grow into a pretty long table? Maybe some filter on campaigns (if you feel like reading about sth advanced - check the documentation for "StandardSetController"). Also I'm pretty sure there are some ways to add Contacts/Leads to Campaigns from Campaign reports - maybe something out of the box would save your time and be more maintainable...
But code solution would be pretty straightforward, start with a helper wrapper class:
public class CampaignWrapper{
public Boolean selected {get;set;}
public Campaign c {get; private set;}
public CampaignWrapper(Campaign c){
this.c = c;
selected = !c.CampaignMembers.isEmpty();
}
}
Then a query and build the list of wrappers:
List<CampaignWrapper> wrappers = new List<CampaignWrapper>();
for(Campaign c : [SELECT Id, Name, (SELECT Id FROM CampaignMember WHERE ContactId = '...')
FROM Campaign
LIMIT 1000]){
wrappers.add(new CampaignMember(c));
}
You should be all set ;) If it's just for displaying - you might not even need the wrapper class (some tricks in visualforce expressions maybe or use Map<Campaign, Boolean> even...
1000 records is the limit of collections passed to Visualforce (10K if your page will be readonly). Past that - pagination, most likely with use with abovementioned StandardSetController.

Resources