I just start to learn Salesforce and now I try to create VisualForce page that display info about primary contact. I tried to use {!Opportunity.OpportunityContactRoles.Contact.Name}, but that way I can add only all related contacts. Maybe you should advice how I can add only primary contact? Maybe I should write my own controller?
I wrote this Extension:
public with sharing class opportunityControllerExtensionForPDF {
private final Opportunity opportun;
public opportunityControllerExtensionForPDF(ApexPages.StandardController stdController) {
this.opportun = (Opportunity)stdController.getRecord();
}
@AuraEnabled(cacheable=true)
public ID getPrimaryContactId() {
OpportunityContactRole ocr = [
SELECT Id, ContactId
FROM OpportunityContactRole
WHERE (isPrimary=True
AND OpportunityId = :opportun.Id)
WITH SECURITY_ENFORCED
LIMIT 1
];
return ocr.ContactId;
}
@AuraEnabled(cacheable=true)
public Contact getPrimaryContact() {
Id conId = getPrimaryContactId();
return [
SELECT Name, Phone, Email
FROM Contact
WHERE Id = :conId
WITH SECURITY_ENFORCED
LIMIT 1
];
}
}
Also I wrote such string in Visualforce:
<apex:outputText value="{!primaryContact.Phone}" />
But I see this message instead of Visualforce page: List has no rows for assignment to SObject
Where I made a mistake?