We have a trigger that inserts a Chatter Feed Item to a particular Chatter group when certain records are inserted, and we are passing in a value for the LinkUrl field to allow users to click the feed post and navigate to the record. We also want the users to be able to navigate to the inserted record from the Chatter group's email digest. Complicating matters is the fact that we restrict login access to an internal SSO portal with My Domains enabled.
I'm populating the LinkUrl field like so:
feeditem.LinkUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/' + newRec.Id; This seemed to work nicely at first, since this method returned our custom My Domain URL just as it should (something along the lines of https://my.custom.domain.salesforce.com). Unfortunately when inserting the records via Data Loader, this started returning https://na7-api.salesforce.com. I'd say it's not much of a problem except that now when our users hit this link, they are redirected to the standard Salesforce login page (which they cannot use, since we restrict their access to SSO and as such never generate a password for them).
So my question is two-fold: why does URL.getSalesforceBaseUrl().toExternalForm() return different results depending on how the records are inserted, and is there another method that I can use to fetch our custom domain?