1

I have a invoice__c custom object with Account__c lookup field (data type -Lookup(Account)). Through API, receiving lookup field input value as account name instead of salesforce Id. I need to compare and get salesforce Id for the Account__c name((Field label - Account Name, Field Name-Name data type -Name) from Account object and insert into invoice__c account lookup field. For this I have written below trigger and got an error as "Account: id value of incorrect type". Looks issue in name to ID conversion.

Could some one correct me the below code.

 trigger invoiceAccountInsert on invoice__c (before insert) {
   Set<String> accountNames = new Set<String>();
   Map<String, Id> accountNameToId = new Map<String, Id>();
   for(invoice__c record: Trigger.new) {
     accountNames.add(record.Account__c);
     System.debug('Account__c >>>>>>' + record.Account__c);
   }

   list<Account> acclist = new list<Account>();
   acclist = [SELECT Id, Name FROM Account WHERE Name IN :accountNames]; 

   for(Account acc:acclist)
   {
    accountNameToId.put(acc.Name, acc.Id);
    System.debug('acc.Name >>>>>>' + acc.Name);
    System.debug('acc.Id >>>>>>' + acc.Id);
    System.debug('accountNameToId >>>>>>' + accountNameToId);
   }
   insert acc;
}
  • In addition to Davids answer. Just a suggestion, remove the insertion from loop. – Ysr Shk Jan 17 '18 at 16:48
  • Thank you Ysr for your suggestion. I tried with that way but no luck. Might be different issue. Will check it. – Djs Jan 17 '18 at 17:44
  • Could you please let me know- – Ysr Shk Jan 17 '18 at 17:46
  • Could you please let me know why you doing this - you're retrieving account from query and inserting again (which are already present in db) – Ysr Shk Jan 17 '18 at 17:48
  • I assume you replaced name with Id in where part – Ysr Shk Jan 17 '18 at 17:49
2

You cannot populate a value other than a valid Salesforce Id in a lookup field. You will need to bring in your account names for matching in a separate field that is of type Text, and populate the lookup field only when you locate the correct Salesforce Id.

Salesforce validates foreign keys (lookup fields) before the data even gets to your trigger. If you send data in a lookup field other than an Id, Salesforce will throw an error immediately. You will need to change your data model to make Account__c a Text field or change your integration to send the Name data in a different field.

  • Thank you David for the information. Current data type is name. Will this not work with 'name' data type? I have only option is by comparing the input value with name and insert the corresponding salesforce Id into lookup field. It would be great if you could share any other options or example to make it work. – Djs Jan 17 '18 at 17:41
  • @Djs, would you please edit your question to make clear the names and data types of all of the fields involved? – David Reed Jan 17 '18 at 17:42

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.