+ Start a Discussion
DaveKempDaveKemp 

How to instantiate an object from it's ID (string) without using a SOQL query

Hi,

 

Is there a way to retrieve an object given its ID without having to do a SOQL query, I ask because I'm need to reduce the number of SOQL queries in my code that creates Service Contracts, ContractLineItems and Assets when converting them from Opportunities?

 

Thanks,

David

bob_buzzardbob_buzzard

You can't retrieve an object based on its id, but if you want to update to add some fields or the like, you can specify the id when instantiating the object as follows:

 

id theId; // wherever this comes from

My_Object__c myObj=new My_Object__c(id=theId);

 

you can then set various values into myObj and when you update it, the vaues will be applied to the record in the database.

DaveKempDaveKemp

Many thanks Bob (I loved that show)

 

I'll see if this gives me enough to work with

 

Regards,

David