Is it possible to get the PersonContactID from an Opportunity field lookup.

I am trying to get the PersonContactID to get a different record that is not related to the Opportunity Record itself

I am new to coding and so far nothing i have done has worked

public class ID_Document{
    public Identification_Documents__c DocID {get; set;}
    private string AccountId;
    private string opportunityid;
    public ID_Document()
        opportunityid = ApexPages.currentPage().getParameters().get('oppId');
        AccountId = ApexPages.currentPage().getParameters().get('Test_ID__c');
        DocID = new Identification_Documents__c();

    public PageReference Save()
        DocID.Account__c = 'AccountId';
        insert DocID;      
        return new PageReference ('/' + opportunityid);
  • Never, ever, hard code an Id. – Adrian Larson Oct 23 '18 at 14:28
  • arr, sorry that was for testing to much sure i had the correct ID value, – Van Tay Le Oct 23 '18 at 14:32

You can query the PersonContactId directly by using either the AccountId you have (I'm assuming it's the PersonAccount Id) or the Opportunity.AccountId if the AcountId is for the PersonAccount.

Either of those will allow you to create a method to directly obtain the PersonContactId. Its important to remember there should only be ONE ContactID for a PersonAccount, that will allow you to query that Contact using just the AccountId in a query that would look something like this:

Id contactId = [SELECT Id FROM CONTACT WHERE AccountId = :accountId LImit 1].Id;


In response to comments...

The Opportunity will be a child of the Account. Opportunity has a field on it for AccountId. If you do not have it at the time you need the ContactId, you should be able to query Opportunity to get the Account Id. It will be much easier for you to do that separately than to try to do it in a single query.

Here is how to obtain it using the opportunityId variable in your controller:

Opportunity opp = [SELECT AccountId FROM Opportunity WHERE Id = : opportunityid limit1];  
// Id of personAccount = opp.AccountId;
Id contactId = [SELECT Id FROM CONTACT WHERE AccountId = :opp.AccountId LImit 1].Id;

If you have any issues with the above, either declare Id personAccount = opp.AccountId or try using opp[0].AccountId and if necessary, use list<opportunity>opp in the query declaration. Sometimes these things do not compile as they should when limit 1 is used in the query.

| improve this answer | |
  • Hey there... that SoQL is a little wonky. :-) – Sebastian Kessel Oct 23 '18 at 14:55
  • 1
    Arggh, thanks, was in a rush! Will correct. – crmprogdev Oct 23 '18 at 15:00
  • arr i still cant get this code right. Because the Account lookup is on the Opportunity form, do i need to pass through the opportunity id to the query? if so how do i do this? – Van Tay Le Oct 24 '18 at 12:38
  • If the accountId is not available at the time you need it, you will need to query for it. See my edited answer. – crmprogdev Oct 24 '18 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.