We are seeing a test failure in a couple of our Continuous Integration builds (that deploy the same code) with the error:

System.SObjectException: Contact.AccountId does not belong to SObject type Contact

The error is occurring in the put in this code (where the SObjectField array is hard coded and so has been successfully compiled):

public static SObject copyFieldsToNewSob(SObject sob, SObjectField[] fields) {
    SObject result = sob.getSObjectType().newSObject();
    for (SObjectField f : fields) {
        if (!String.valueOf(f).equalsIgnoreCase('Id')) {
            result.put(f, sob.get(f));
    return result;

One org fails all the time and the other 2 out of 3 times.

I'll raise a case, but before I do is anyone else seeing this or knows if it will be fixed soon?

  • 3
    I feel sorry for the sob that has to deal with that! Ah unfortunate abbreviations. Not as bad as std I suppose. – Adrian Larson Jun 14 '16 at 8:14
  • @Keith if you skip the 'AccountId' field, does put work for the other fields? Is the Account a Person Account? There is a blurb in the docs "Field tokens aren't available for person accounts. If you access Schema.Account.fieldname, you'll get an exception error. Instead, specify the field name as a string." – Arpi Jakab Dec 9 '16 at 23:07
  • @ArpiJakab Thanks for that comment; not sure its the cause here but good to know. – Keith C Dec 10 '16 at 10:51

Changing from this:

result.put(f, sob.get(f));

to this:

result.put(String.valueOf(f), sob.get(f));

works around the problem..

  • I suppose that's a fairly obvious workaround. Did you ever find a Known Issue or open a case? – Adrian Larson Aug 2 '16 at 19:28
  • @AdrianLarson I left it to my colleague who couldn't create a simple test case that failed so in the end we gave up pursuing the case. There is probably a bug in there somewhere... – Keith C Aug 2 '16 at 19:30

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.