Creating SObject Dynamically by Using Id

By | November 8, 2016

While working on one of our projects, we faced a challenge where we were supposed to create the object, already existing in the database, dynamically from just an “Id”. We had no information other than the Id field of the record. We can use the functions of SObjectType class to create the instance of a SObject (any Standard or Custom object in the Salesforce) dynamically.

Read More: Salesforce Development Services

Please find below screenshots for the syntax to create the record dynamically:

Code for dynamic record creation

Code for dynamic record creation

You will see in the above code that we have written one method which accepts the first argument as the Id of the record to be updated, the second argument is the string argument that contains the name of the field to be updated, and the third argument being the object type which holds the new value for the field. Within this function, we can use the syntax for dynamic record creation based on Id alone. Once we get the record using the SObjectType, we can populate the field based on the values sent and update the record accordingly into the database. Here is the output of the above code:

Code output for dynamic record creation

Code output for dynamic record creation

This is one of the ways how you can get the reference of an already existing record for any SObject dynamically by simply using its Id and perform desired operations on it.

About Us
Greytrix as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce integration with Sage Enterprise Management (Sage X3), Sage Intacct, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.

Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star app listed on Salesforce AppExchange.

For more information, please contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts