2

Good day everyone,

I have installed an npsp package and then I created a visual force that will save a new affiliation . I've tried to used an external Id to save the data. I've already tried saving data using external Id in account and contact. But when I tried in npsp affiliation I've encounter some problem "Illegal assignment from Account to Id". What am I missing ? Below is my sample code. Please help. Thanks.

npe5__Affiliation__c aff =  new npe5__Affiliation__c();

Account accReference = new Account();
accReference.DI_extId__c = 'acc_12345';
aff.npe5__Organization__c   = accReference;

Contact conReference = new Contact();
conReference.DI_extId__c = 'con_12345';
aff.npe5__Contact__c    = conReference;


Account accountList = new Account();
accountList.Name = 'TEST ACCOUNT 1';
accountList.DI_extId__c = 'acc_12345';

Contact contactList = new Contact();
contactList.Last = 'TEST CONTACT 1';
contactList.DI_extId__c = 'con_12345';

Insert accountList;
Insert contactList;
Insert aff;
5

Change:

aff.npe5__Organization__c   = accReference;

To

upsert accReference;
aff.npe5__Organization__c   = accReference.Id;

accReference is an Account sObject, you need the ID. Same thing goes for the contact record.

UPDATE:

After careful investigation and getting the idea of what you're actually trying to do, you need to set the __r relationship instead:

aff.npe5__Organization__r = accReference;

| improve this answer | |
  • I think this not how External ID works. I want to save my data using External ID. – Hope Aug 21 '15 at 4:34
  • I think you can only set the external Id to the object's external Id field, but not to a Salesforce lookup field. So, Account.External_Id = '1234'; will work, but Contact.AccountId = '1234'; won't work. – Boris Bachovski Aug 21 '15 at 4:41
1

First issue: External IDs only work for an upsert operation, where you are performing inserts.

Second issue: for those lookup fields, __c holds an id value, __r points to another record via a relationship.

So, your code though it is incomplete, might look like this:

// create an affiliation
npe5__Affiliation__c aff =  new npe5__Affiliation__c();

// create an account instance
Account accReference = new Account();
accReference.DI_extId__c = 'acc_12345';

// connect the field to the Account via reference
aff.npe5__Organization__r   = accReference;

// upsert the account
upsert accReference DI_extId__c;

// get the id of the account after DML and set the id for the lookup reference on the affiliation by reaching through the relationship
aff.npe5__Organization__c = aff.npe5__Organization__r.id;

// upsert the affliation
upsert aff;
| improve this answer | |

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.