function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth Birari.ax1164Siddharth Birari.ax1164 

Dynamic Record Creation

Hi,

 

I've one requirement regarding dynamic Apex.

I've one string which contains the sObject API name.

 

Task 1: To determine if such object exists in my org or not.

Task 2: If it does exists in the org, I want to create its record dynamically.

 

I m done with task 1, but now, i am not able to create the record. Does any one knows how to create a record of the object whose API name is with you in string format.

 

Note : In step one i am able to get the Token for the object.

 

For Task 1 i ve following code snippet

 

 

  1.     public void retrieveObjectName(){
    1.         String strObjectName = 'Account';
    2.         Map<String, Schema.Sobjecttype> SobjectMap = Schema.getGlobalDescribe();
    3.         Schema.Sobjecttype sObjectToken = SobjectMap.get(strObjectName);
    4.         if(sObjectToken <> NULL)
    5.         System.debug('sObjectToken ' + sObjectToken);
    6.         else
    7.         System.debug('No Such Object Exists ');
    8.         Account obj = (Account)sObjectToken.newSObject();
    9.         insert obj;
        }

At line number 8, i've hardcoded 'Account'. But in my requirement this can be any existing object in my org.

Siddharth Birari.ax1164Siddharth Birari.ax1164

i am not able to find any solution over this.

Currently the idea what i am getting is to put multiple 'if' conditions to compare the token with various object api names.

but i don't find this way a robust. but as of now it is fulfiling my requirements.

 

 

mulvelingmulveling

You're on the right track with the use of the newSObject  instance method on the Schema.SObjectType token -- just one small step away from what you want, which is the base SObject class and its instance methods (e.g. get(String field), set(String field, Object value) -- refer to the Apex Language Reference). With the interface provided on SObject, you can usually get away without having to cast to a specific type.

 

So, your code would become this:

SObject obj = sObjectToken.newSObject();

insert obj;

 

Apex is not a dynamic language except for the facilities offered via SObjects, which are a very nice break indeed from the usual heavy-casting and having to define types for things that would be better left anonymous (especially with the extremely limited ability to create namespaces). Still, I'd really appreciate if Apex at least had the ability for Anonymous Classes ala Java. I'd much rather just have first-class functions, but that's not a likely addition to a language like Apex. I won't even touch on generics.

 

Mike

Siddharth Birari.ax1164Siddharth Birari.ax1164

Thanks mate..

 

I used the following way.

 

sObject obj = Schema.getGlobalDescribe().get(strObjectName).newSObject();

This also works.

 

Now, the next thing is, while creating the record, i've a validation rule written in string format on a specific field.

Is there any way, i can apply the rule, while creating the record.

mulvelingmulveling

That depends on what you mean...

When you DML insert/update a record in Apex, all relevant validation rules will be applied at the appointed time(s) in the commit/trigger-order-of-execution stack (Salesforce has good documentation on this stack -- I strongly recommend google'ing it). 

 

However, if you mean that you'd like to apply some validation rule logic on an in-mempry SObject on-the-fly -- without invoking DML in the process -- then you're out of luck. Configured validation and workflow rules are essentially locked away from access/fiddling via Apex.

 

While we're all pretty used to duplicating some light business logic between client (i.e. Javascript) and server (e.g. Java, Apex, Ruby, PhP, etc) in order to provide a nicer user interface, I'm a bit incredulous that we're now sometimes put a position to duplicate business logic twice on just the server side, because these rules are locked away. They're sure easy to configure (and hence heavily used by professional services where I work), but they can't do many of the things Apex can -- and when it comes time to supplement existing rules with Apex, we can run into this annoyance. 

 

I would love to see (or build) a scripting engine for Apex (ala Rhino in Java) that would allow for easy implementation of light one-off business logic without that huge brick wall isolating it from Apex. However, due to Apex's governor limits, such an implementation would be very tricky, and even then its usage will be limited to the lower processing-power applications. 

 

-- 

Mike