Dynamics 365 - Create, Update or delete records directly from database
As we all know that we don't have the access to the database and there are times where we need to create update or delete a record directly from database.
So, In order to get this thing done. I created a form from which I can directly execute the query. It can be enhanced but just to give you the idea how to do it.
Here is the form design.
List queryList = new List(Types::String);
ListIterator iterator;
str sqlQuery;
DialogButton diagBut;
str strMessage = "Are you sure you want to execute this query?";
str strTitle = "Execute" ;
super();
diagBut = Box::yesNo(strMessage, DialogButton::No, strTitle);
if (diagBut == DialogButton::Yes)
{
try
{
Connection conn;
SqlStatementExecutePermission permission;
;
queryList = strSplit(QueryControl.valueStr(), ";");
iterator = new ListIterator(queryList);
while(iterator.more())
{
sqlQuery = iterator.value();
permission = new SqlStatementExecutePermission(sqlQuery);
conn = new Connection();
permission = new SqlStatementExecutePermission(sqlQuery);
permission.assert();
conn.createStatement().executeUpdate(sqlQuery);
// the permissions needs to be reverted back to original condition.
CodeAccessPermission::revertAssert();
iterator.next();
}
QueryControl.text('');
Info(strFmt("Query executed successfully."));
}
catch
{
Info(strFmt("An error occurred."));
}
}
Create an action menu item and assign the form to that menu item and add it to system administration menu's extension.
It can execute multiple queries just add semi colon at the end to each query so that it can be splitted to multiple query to execute.
Note: Be very careful while executing any query directly on production environment.
So, In order to get this thing done. I created a form from which I can directly execute the query. It can be enhanced but just to give you the idea how to do it.
Here is the form design.
Set QueryControl's allow declaration property to YES.
Override ExecuteBtn's click event and write the following code.
List queryList = new List(Types::String);
ListIterator iterator;
str sqlQuery;
DialogButton diagBut;
str strMessage = "Are you sure you want to execute this query?";
str strTitle = "Execute" ;
super();
diagBut = Box::yesNo(strMessage, DialogButton::No, strTitle);
if (diagBut == DialogButton::Yes)
{
try
{
Connection conn;
SqlStatementExecutePermission permission;
;
queryList = strSplit(QueryControl.valueStr(), ";");
iterator = new ListIterator(queryList);
while(iterator.more())
{
sqlQuery = iterator.value();
permission = new SqlStatementExecutePermission(sqlQuery);
conn = new Connection();
permission = new SqlStatementExecutePermission(sqlQuery);
permission.assert();
conn.createStatement().executeUpdate(sqlQuery);
// the permissions needs to be reverted back to original condition.
CodeAccessPermission::revertAssert();
iterator.next();
}
QueryControl.text('');
Info(strFmt("Query executed successfully."));
}
catch
{
Info(strFmt("An error occurred."));
}
}
Create an action menu item and assign the form to that menu item and add it to system administration menu's extension.
It can execute multiple queries just add semi colon at the end to each query so that it can be splitted to multiple query to execute.
Note: Be very careful while executing any query directly on production environment.
To
know more on how ERP can help business book a free
consultation session at Dynamics Stream Dubai at www.dynamicsstream.com
Follow
us on LinkedIn for latest updates on Microsoft ecosystem - https://www.linkedin.com/company/dynamicsstream.com
Comments
Post a Comment