Convert String to sObject using Apex in Salesforce
11 views
Jun 9, 2025
Schema.getGlobalDescribe().get().newSObject() can be used to convert String to sObject using Apex in Salesforce. Blog Post: https://www.infallibletechie.com/2017/09/how-to-convert-string-to-sobject-using.html
View Video Transcript
0:05
foreign
0:09
in this video we are going to see how to
0:12
convert string into Salesforce object
0:16
using Apex
0:19
if you have a string if you wanted to
0:21
convert that into an object and then if
0:24
you want to add the objects field values
0:27
then you can make use of this simple
0:30
example and then you can build on top of
0:33
it as per your business process and
0:35
requirement
0:37
so here the stringers or counter so I
0:40
have a variable called Str where the
0:42
value is set to account
0:44
using schema dot get Global describe
0:49
we will be having the subject for
0:52
all the
0:56
um objects so since it returns a map I
1:00
am using the get method and then I am
1:02
passing the object name here so here the
1:05
object name is account so once I pass it
1:08
I'm so using this new s object I am just
1:12
initializing that variable so
1:16
based on this method I am just
1:18
initializing
1:20
s object with the string
1:23
Str that is nothing but account so as
1:26
per this statement it will create an
1:29
instance for account object
1:32
in order to add values to the object
1:37
instantiation for example here obj is
1:40
nothing but
1:42
accounts
1:44
instance so for that I wanted to
1:48
put values like name and description
1:53
there are additional Fields you can use
1:55
the same put method however I have used
1:57
in
1:58
line number three and four and then you
2:00
can add all the values to the fields
2:03
so obj dot put off name is the field APA
2:06
name and test is the value
2:08
and here description is the field and
2:12
testing is the value for the description
2:14
field
2:15
so let's execute this and see the system
2:20
dot debug statement this should be
2:23
an instance of account
2:26
object record with name as test
2:31
description as testing
2:38
so here obj is its account
2:41
and within that account
2:43
name is set as test and description is
2:45
testessor sorry set as testing
2:48
so using this simple code we can easily
2:52
convert a string
2:54
into an object instance and then you can
2:57
add the
2:59
field values to the instance and then
3:02
you can just add a system.debug
3:04
statement to view the values
3:09
please check the video description
3:11
description I have shared my blog post
3:13
using which you can get this sample code
3:16
as a reference and then you can start
3:18
building on top of it
3:31
I hope it was helpful
3:43
thank you for watching
#Computers & Electronics
#Internet Software