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
Tilak DeTilak De 

Dynamic Insert Query

Let me first describe the requirement -

 

I have a managed package that contains an Object called Student. I have to create a many-to-many relationship with another object. I have a junction object for this.

Just imagine, that this package can get installed into different orgs, and the many-to-many relationship can happen with some object called Class or Teacher or Exam (depending on the requirement of the org.)

 

I am creating the second master-detail relationship of the junction object when I have installed the package in the org, and I am storing the API name of the second master-detail relationship in a Custom Settings. (Custom settings also belong to my package)

 

The Student-Teacher or Student-Exam, assignment is happening from a VF page(also belonging to the package). So I need to dynamically select the field API name of the second master-detail, assign value to it and save it.

 

Is there any way to create a Dynamic Insert DML? 

 

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

Also, you might find the "Setting and Retrieving Field Values" section of this page interesting.

All Answers

colemabcolemab

What do you mean by "dynamically select the field API name of the second master-detail"?  Don't you have this stored in your custom setting?

 

Maybe you are looking for the describe methods.

Tilak DeTilak De

Yes. I have it stored in Custom Settings. But the value in the custom settings will change from org to org. So the value that I will be querying from the Custom Settings needs to supplied to the DML insert. So the DML will have the second-master-details API name being taken from the Custom Settings dynamically.

 

Indeed I am looking for some Schema method, that might provide us this flexibility for DML Insert, but I am not able to figure out the correct approach.

 

Any help is much appreciated.

colemabcolemab

If you are looking for an example that details some of the schema / describe methods, please see my blog post here.  The post contains a link to the git hub repository that contains the code.

 

The post is on how to create a visual force component that will take an object coming in and using the describe methods will display all of the fields on that object for the purposes of building a SOQL where clause.

 

Hopefully that will give you a good intro to how the methods can be used.

colemabcolemab

Also, you might find the "Setting and Retrieving Field Values" section of this page interesting.

This was selected as the best answer