I have a formula field called ContactID18__c on the Account object. The formula is CASESAFEID(PersonContact.Id). Can this field be indexed or no because it is a custom field and a formula? Or is there a way to get the ContactID(18) from the Account without doing a formula and that field is indexed.
Yes, you can request to have indexes created on custom formula fields provided that they are deterministic - generally speaking, that they don't have cross-object references, dependency on the current date, or references to fields that have some special built-in functionality.
- Force.com Formula Fields, Indexes, and Performance Gotchas
- Force.com SOQL Best Practices: Nulls and Formula Fields
However, I'm not sure why you'd need a custom index on a field whose only purpose is to hold an 18-character Salesforce Id. Note that all Salesforce APIs (including Apex and SOQL) treat 18 and 15 character Ids as equivalent for the purpose of filtering on an
Id fields themselves are indexed.
You can always obtain 18-character Ids by extracting data using the Data Loader or Workbench. They export 18 character Ids by default, unlike Reporting.
Edit: based upon your comments, your process involved inserting Invoices and/or Quotes, and you wish to use your custom formula field as if it were an External Id of the parent Account or Contact. You don't need to do that. The field is providing you with the record Id of the Contact associated with the Person Account, whose Id presumably you already are holding. You can simply populate those Id values directly into Contact and Account lookup fields on the new objects you're creating. You should not need to perform an upsert, use an External Id, or create any new indexes.
A formula field cannot be indexed at this time although I understand it's something that is potentially in the works for a future time. Formula fields are only evaluated when the record is actually opened and cannot be indexed for that reason. You can populate a lookup field from another object or using workflow you can populate a text field.