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 

Runtime validation

Hello everyone...

 

This is my second post regarding Dynamic Record Creation.

Although now i am able to create a record of any object at runtime.

 

By any object i mean i've one method where i get the name of the existing object in string format, basis which i create the record.

 

Also i get the associated field names with corresponding values through say some parameters, i populate them and finally i create the record.

 

Till now everything is OK.

 

Now, the thing which i want is to apply validation rules on the records which i create at runtime.

 

But there is a catch. Validation rules have not been created on those object. I've the rules defined in string format.

 

Following example will make it clear..

 

  1. String objName = 'Account'
  2. Stirng field1 = 'Name'                               //Account Field
  3. String field2 = 'Age'                                  //Account Field
  4. String validation = 'Age < 25';
  5. Sobject objSobjectInstance = Schema.getGlobalDescribe().get(objName).newSObject();
  6. objSobjectInstance.put(field1, 'Test');
  7. objSobjectInstance.put(field2, 20);
  8. insert objSobjectInstance;

 

Here in above code, the record will get created. But i want to apply validation rule (line 4) to be applied on Age.

 

Any idea, how can i do this?

 

 

bob_buzzardbob_buzzard

It looks to me like you'll have to write your own rules parser/engine i.e. you'll need to be able to turn a string of 'age < 25' into field, operator, value triplets and evaluate them.  Its not too difficult to do if they are that simple, but can turn into quite a lot of code if you are allowing nesting.

Siddharth Birari.ax1164Siddharth Birari.ax1164

Thanks Bob..

 

Actually same things are revolving in my mind..

Yes, it will be huge code, but keeping it apart,

m looking forward for parsing alogrithm itself.

 

coz i need to evaluate

if(age > 25)

 

i.e. (20 > 25)

 

by parsing it and evaluating it later, i need to have a boolean.

 

please do suggest any parsing algorithm if you are known to any.

bob_buzzardbob_buzzard

The way I've done this in the past is to break it up into operand, operator, operand triplets and store in a tree.  That allows nesting, where operands can be a field, a literal value or another triplet.  The code to parse walks the tree and carries out the tests.  The chunky code is usually a huge if statement that determines the type of operator and executes the test.

Siddharth Birari.ax1164Siddharth Birari.ax1164

Hi bob,

 

Thanks for your suggestions.

here one thing what i wonder about is tree. since apex doesn't support pointers, how would i create the tree?

one more challenge in front of me is, the expression to validate is purely string. any remedy on this?

 

Thanks in advance.

bob_buzzardbob_buzzard

You'd have to create the tree yourself, through references to other class instances.  You'll need to break the string up in order to do this - one whitespace or brackets I would have thought.