Hello, currently I am trying to find a way to create new records in a script task. What I am trying to do is create a new order off of a order, so a duplicated order, but some fields need to be changed. I need to be able to copy all of the products from that order to the new one. I was going to use a loop in a script task but I'm just not sure how I would create the new opp products from the script task and populate them. I thought about looping through the opp products and firing a business process for each one separately, but I read performance would take a beating so that isn't a good way. So two main things I would like to know how to write data using script tasks (c#), also I would like to know how to write data on the client side (javascript) thanks!
Like
Hello,
There's a few different ways to create data in Creatio, however, the first place to look is using the entity classes (for C#, server-side). See https://academy.creatio.com/documents/technic-sdk/7-15/working-database…
Here's an example in the simplest form:
var invoice = UserConnection.EntitySchemaManager.GetInstanceByName("Invoice").CreateEntity(UserConnection); invoice.SetDefColumnValues(); invoice.SetColumnValue("StartDate", DateTime.Now); invoice.SetColumnValue("PaymentAmount", 100); invoice.Save(false, false);
To create a record via client-side code, you'd use an InsertQuery
var insert = Ext.create("Terrasoft.InsertQuery", { rootSchemaName: "UsrMyEntity" }); insert.setParameterValue("UsrMyParentId", this.get("Id"), Terrasoft.DataValueType.GUID); insert.setParameterValue("UsrMyDateProperty", new Date(), Terrasoft.DataValueType.DATE); insert.execute(function() { // do any refreshing if needed here }, this);
See docs for InsertQuery here https://academy.creatio.com/api/jscoreapi/7.15.0/index.html#!/api/Terra…
Hope this helps.
Ryan
Dear Tyler,
Adding to what Ryan wrote, there are several other ways how it can be done. For example, if you don't want to trigger business processes when you create a record in script task you can use Insert class:
https://academy.creatio.com/documents/technic-sdk/7-15/building-queries-adding-data-insert-class
Or in your case if you want to copy products from one order to the other you can use InsertSelect class:
https://academy.creatio.com/documents/technic-sdk/7-15/adding-data-subqueries-insertselect-class
Here is an example of how to do it:
var OrderNumber = "123";
var id = Guid.NewGuid();
var ins = new Insert(UserConnection)
.Into("Order")
.Set("Number", Column.Parameter(OrderNumber))
.Set("Id", Column.Parameter(id));
var affectedRows1 = ins.Execute();
//var OrderNumber = "123";
//var id = Guid.NewGuid();
var OrderId = new Guid("0297f280-04e8-4110-af7b-b7dff66cd26b");
var insel = new InsertSelect(UserConnection)
.Into("OrderProduct")
.Set("OrderId", "ProductId")
.FromSelect(
new Select(UserConnection)
.Column(Column.Parameter(id))
.Column("ProductId")
.From("OrderProduct")
.Where("OrderId").IsEqual(Column.Parameter(OrderId))
as Select);
var affectedRows2 = insel.Execute();
Best regards,
Dennis
Tyler Rainey,
In case of Ad-hoc DB queries (Insert class) you can either use multi-row data insert or insert from select:
https://academy.creatio.com/documents/technic-sdk/7-15/multi-row-data-insert-insert-class
https://academy.creatio.com/documents/technic-sdk/7-15/adding-data-subqueries-insertselect-class
In case of Entity classes you would need to insert records in a loop.