function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Call a respective account and contact name basd on a linked custom object

I have a custom object, that includes multiple fields,


But the account and contact names are taken from the respective account and contact standard objects,


So if you go in the custom object fields, you will se that they are lookup fields for those standard objects,


Question: The following few lines call a specific user data based on their IP address:


public with sharing class main_sub {
    public List<Trainee__c> trainee{get;set;}
    public List<Contract> license{get;set;}

public PageReference Sub{
license = new List<Contract>();

license  = [select StartDate,

Host_Name__c,License_Type__c,Volume_Serial_Number__c,Expiration_Date__c,Physical_Address__c from Contract where Physical_Address__c=:'IP ADDRESS' ]; 



How can I call the Account name and Contact name for that IP address?


If someone can provide a link to an example that explains this, That would be great,



Eli Flores, SFDC DevEli Flores, SFDC Dev
Does the contract object have Account and Contact lookup fields? If so, you can use the __r notation to grab information from it. Let's say your Account lookup field onthe Contract object is LookupAccount__c. You could include the field LookupAccount__r.Name to get the name.

You can swap the __c in soql to __r to get information from the object related  to in the lookup field. 

If the Contract is not the object with the account and contact look up fields but is part of 3rd object that has lookups then you could still apply the same principle with something like this:

SELECT Account__r.Name, Contact__r.Name,Contract__r.Host_Name__c,Contract__r.License_Type__c,Contract__r.Volume_Serial_Number__c,Contract__r.Expiration_Date__c,Contract__r.Physical_Address__c
FROM thirdObject__c
WHERE Contract__r.Physical_Address__c = :'IP Address'

presuming that name of the lookup fields on ThirdObject__c are Account__c, Contact__c, and Contract__c respectively.