-2

Our App syncs to Salesforce objects using a Listing ID field that matches Account or Opportunity or Case Number but the field needs to be set to an External ID and those number fields do not allow that. So,

We added a custom field to Account record that we configured to operate as External ID And we want the value in that field to be copied from the 18 digit Account Id

We've tried adding a trigger to

trigger CopyAccountNumberToCustomField on Account (before insert) {
    for (Account acc : Trigger.new) {
        // Check if Account Number is not null or empty
        if (String.isNotBlank(acc.AccountNumber)) {
            acc.Listing_ID__c = acc.AccountNumber; // Copy Account Number to Listing_ID custom field
        }
    }
}

And we tried adding a trigger after insert

trigger CopyAccountNumberToCustomField on Account (after update) {
    for (Account acc : Trigger.new) {
        // Check if Account Number is not null or empty
        if (String.isNotBlank(acc.AccountNumber)) {
            acc.Listing_ID__c = acc.AccountNumber; // Copy Account Number to Listing_ID custom field
        }
    }
}

neither one worked

Any tips on how to copy an Account Number into a custom text field on Account record create?

5
  • 1
    In your second trigger example, is the after update a typo, or do you really have it as after update and not after insert?
    – Derek F
    Commented Sep 18, 2023 at 22:02
  • I've tried it with both after update and after insert neither one works.
    – Peter C
    Commented Sep 18, 2023 at 22:10
  • 1
    Are you explicitly populating AccountNumber in whatever code/process you're using to create Accounts? AccountNumber is not an auto-number field, so it will be null (blank) unless you populate it.
    – Derek F
    Commented Sep 18, 2023 at 22:13
  • We're trying to get the 18 digit Salesforce ID... what is that field called?
    – Peter C
    Commented Sep 18, 2023 at 22:28
  • thank you sir. much obliged.
    – Peter C
    Commented Sep 18, 2023 at 23:28

1 Answer 1

1

Every object in Salesforce has an Id field which stores a unique reference for each record of that object. This can be used as an external Id by external systems. You do not need to copy it to another field in a trigger.

The API name for this field on every object is Id. So if you did want to copy it to another field, you would do:

acc.Listing_Id__c = acc.Id;

For more information about Ids, see here: Locate the Unique ID of a Record in Salesforce

And for a really detailed description of how they are formed: What are Salesforce ID's composed of?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .