0

I have a custom object Vehicle__c with a lookup field Car__c which is also a custom object. Type of field Car__c is String. Car__c has just two fields - 'Car Name' with value 'FourWheeler' and 'ExteralId' also with value 'FourWheeler' I am instantiating Vehicle and then I am trying to assign value to the field which is causing an exception -

Vehicle__c vehicleObj = new Vehicle__c();
vehicleObj.Car__c = "FourWheeler";

The exception I am getting is -

exp:'common.apex.runtime.impl.ExecutionException: Invalid id: FourWheeler'

For a non lookup field this type of assignment works without any issues. What is wrong with the way I am assigning the value to the lookup ? What is the correct way ?

5
  • Welcome to SFSE! I'd recommend checking out Apex Basics and Database on Trailhead, which walks through how to relate objects with one another.
    – David Reed
    May 24, 2021 at 23:10
  • Thanks @David. So would the assignment look something like - vehicleObj.Car__c = [Select Id from Car__c WHERE name = 'FourWheeler']; May 24, 2021 at 23:29
  • The reference fields takes actual Salesforce Ids of parent records they are trying to link. In this case, Car__c value should be salesforce id of Car__c record you created earlier. As David suggested, you need basic fundamentals of Salesforce and how it works. Without it, it will be hard to troubleshoot such basic issues.
    – metasync
    May 24, 2021 at 23:36
  • @realfire Can you please take a look at my comment. I think it looks according to your comment. May 24, 2021 at 23:48
  • Yes, that would work but there are lots of edge cases you need to take care before productionizing this (like none or more than one matches etc)
    – metasync
    May 24, 2021 at 23:50

1 Answer 1

1

If you have an external Id, and it's unique, and exists, you can specify it as an object:

Vehicle__c vehicleObj = new Vehicle__c();
vehicleObj.Car__r = new Car__c(Name='FourWheeler');

Note that __r represents a relationship; we're telling Salesforce we want to use an External Id that matches the field and value we specified.

If you don't know for sure, then you should definitely query it, and then decide if you want to use the Id:

Car__c[] cars = [SELECT Name FROM Car__c WHERE Name = 'FourWheeler'];
Vehicle__c vehicleObj = new Vehicle__c();
if(cars.size() == 1) {
  vehicleObj.Car__c = cars[0].Id;
}
insert vehicleObj;
4
  • I created the object as suggested above and assigned the value. It works fine until here - Car__c[] cars = [SELECT Name FROM Car__c WHERE Name = 'FourWheeler']; Vehicle__c vehicleObj = new Vehicle__c(); if(cars.size() == 1) { vehicleObj.Car__c = cars[0].Id; } May 25, 2021 at 6:13
  • But as soon as I do a SecureDML.insertRecords(vehicleObj), I am again getting a different exception -- Exception Message: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Vehicle: id value of incorrect type: aAz5e000000wuQ5CAI: [Vehicle__c] May 25, 2021 at 6:16
  • @KnightoftheVale You're trying to put a Car__c Id into a Vehicle__c lookup field? Make sure that your code is putting the right data in the right fields.
    – sfdcfox
    May 25, 2021 at 11:43
  • Okay, what you suggested actually worked. The problem was that the id value that was getting assigned earlier and causing the error was coming from a similarly named object in a different managed package. I modified the lookup to point to the correct object and after that id values are getting inserted without a problem. Thanks a lot for your help. May 25, 2021 at 15:36

You must log in to answer this question.

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