I’m having a requirement, where I guess I’ve to use Dynamic DML.But I’m new to Dynamic Apex as well as Dynamic DML.
When I will create a Folder in Box.com server by using API call, I will get a response with Folder ID. (Well I’m able to make the API call and able to get Folder ID)
I am having a custom field named “FolderID__c” in Account object.
So I have to write a method which will take 2 parameter .One is objecttype and other is folderID and it will do the insert operation in that particular objectType.(maybe it’s Account, maybe Contact , which should be dynamic).
What I have done so far:
String IdVar = ApexPages.CurrentPage().getparameters().get('id'); //It will fetch the record ID
String objecttype = IdVar.getSobjectType().getDescribe().getName(); //It will fetch object type from record ID
//By using API call I’m able to get folderID. So I’m having folder ID
//Below is the method, which will update FolderID in our sObject dynamically.
public static updateFolderId(String folderID){
Account acc=new Account();
SObject s = new Account();
s.put(FolderID__c,folderID);
acc=(Account)s;
update acc;
}
Here I know that I am working on Account object, so I can easily type cast the Account object. But in dynamic we don't know in which object we would be working on, so how to typecast it dynamically.