function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AndrewNerneyAndrewNerney 

Apex trigger, getting error Invalid id value for this SObject type

Hi all, 

Here's what I hope is a simple/easy question. I've been away from coding for a good while and only have a bit of APEX experience, so it's fair to say I'm a code noob at the moment.

I need to do a cross-object field update from a custom object Physician__c to a Person Account. The relationship is 1:many. I have a dummy field on the Person Account which should be updated whenever the field Fax__c on the object Physician__c is changed.

I'm getting the following error when updating a Physician__c record. Help?

Apex trigger zUpdateMember caused an unexpected exception, contact your administrator: zUpdateMember: execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type: a18Z0000001Gx7XIAS: Trigger.zUpdateMember: line 10, column 1


Here is the code:

trigger zUpdateMember on Physician__c (after insert, after update) {

//LATER - MUST ADD CODE TO ONLY FIRE WHEN FAX FIELD HAS BEEN UPDATED

    List<Account> accountsToUpdate = new List<Account>();
 
   // For each patient account associated with the physician, update the dummy field z_PCPFaxExists__c
    for (Physician__c p : Trigger.new){
   
        Account tempAccount = new Account(Id = p.ID);
       // Only update accounts that we need to update
            If(p.Fax__c != null){
                tempAccount.z_PCPFaxExists__c = true;
            }
            Else
            { 
                tempAccount.z_PCPFaxExists__c = false;
            }
            accountsToUpdate.add(tempAccount);
//        }                         
    }
    // update Accounts
    update accountsToUpdate;

}
This is the Knowledge Base article that I used as a guide:

Apex Trigger cross-object field update

https://help.salesforce.com/HTViewSolution?id=000002636&language=en_US (https://help.salesforce.com/HTViewSolution?id=000002636&language=en_US)

Knowledge Article Number: 000002636


Description
In order to update a field on object B based on critera of object A we must use apex. (When not using roll up summary or formula)


Resolution
// Example: Update Contact based on update/insert of CampaignMember

trigger UpdateField on CampaignMember (after update, after insert) {
  
    List<Contact> contactsToUpdate = new List<Contact>();

   // For each CampaignMember, update contact
    for (CampaignMember o : Trigger.new){
        Contact tempContact = new Contact(Id = o.contactid);
   // Only update contacts that we need to update
        If(tempContact.Do_not_email__c != o.Do_not_email__c){
           tempContact.Do_not_email__c = o.Do_not_email__c;
           contactsToUpdate.add(tempContact);

        }

    }
    // update Contacts
    update contactsToUpdate;

}





James LoghryJames Loghry
The issue is you cannot set the Id of a Physician record as the Id of an Account record.  Futhermore, each object has a specific prefix in the Id, so Salesforce is throwing an exception because of that.  Instead, you'll want to look at your relationship field from Physician -> Account and use that instead.  The following code assumes that you have a child relationship from Physician to Account called "Account__c":

Replace the following line:
Account tempAccount = new Account(Id = p.ID);
With:

Account tempAccount = new Account(Id = p.Account__c);

and you should be good to go.
AndrewNerneyAndrewNerney
Hi James,

Thanks for your response. I figured it was something fairly simple like that. However, I'm not able to find the right field name on the Physicians__c object. I suspect that this setup isn't conducive for the example trigger that I found. Any suggestions?

The setup involves a lookup field called Primary_Care_Physician__c on the Account object. This is the relationship:

User-added image

User-added image