0

I want to ask if there is any potential downfall of creating object in apex with Id that I would get from SOQL on diffrent object?

Pseudo code

SELECT Id, Foo__r.Id, Foo__r.Name from Bar__c

Foo__c oldFoo = new Foo__c();
oldFoo.Id = Foo__r.Id;
oldFoo.Name = 'NewName';

update oldFoo; // it works!

Something inside me is screaming that it's not good idea but it would be great to use it as I'll be conserving some SOQLs.

1 Answer 1

2

It's perfectly fine. In fact, you can do it more concisely by just picking up that object from the relationship field i.e.

Bar__c bar = [SELECT Id, Foo__r.Id, Foo__r.Name from Bar__c];

Foo__c oldFoo = bar.Foo__r;
oldFoo.Name = 'NewName';

update oldFoo; // it works!

You must log in to answer this question.

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