Hi,

For a few weeks I struggled with sending a HTTP POST Request to a third party application via script task.

I have a collection object that needs to be sent via the web service and eventually catch the response and use the data inside process parameters.

 

Here you can find the code for doing that using a JSON object.

 

string x_param = Get("x_process_param");

string y_param = Get("y_process_param");

string z_param = Get("z_process_param");



var ProductLines = Get>("ReadDataUserTask6.ResultCompositeObjectList");

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url/api/v1/Documents/?token="+x_param);

httpWebRequest.ContentType = "application/json";

httpWebRequest.Method = "POST";

int ProductLineIndex = 0;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

using (JsonTextWriter writer = new JsonTextWriter(streamWriter))

{

    JObject InvoiceJSon = new JObject (

                            new JProperty("y",y_param),

                            new JProperty("z",z_param));

                            

    JArray JProductLinesArray = new JArray();

    foreach(var ProductLine in ProductLines) {

        

        var ProductName = "";

        decimal Qty;

        decimal UnitPrice;

        

        ProductLine.TryGetValue("Name", out ProductName);

        ProductLine.TryGetValue("Quantity", out Qty);

        ProductLine.TryGetValue("Price", out UnitPrice);

        

        JProductLinesArray.Add(new JObject(

            new JProperty("ChargeVAT","true"),

            new JProperty("Number",++ProductLineIndex),

            new JProperty("ProductName",ProductName),

            new JProperty("Qty",Qty),

            new JProperty("Rate",1),

            new JProperty("UnitPrice",UnitPrice),

            new JProperty("VATRate",17)));

    }                        

    

    InvoiceJSon.Add(new JProperty("DocumentLines",JProductLinesArray));

    

    InvoiceJSon.WriteTo(writer);

    

}



var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

{

    var result = streamReader.ReadToEnd();

    var LinkToPdf = JObject.Parse(result)["LinkToPdf"].ToString();

    var CaspitDocNum = JObject.Parse(result)["Number"].ToString();

    

    Set("DocLinkToPdf",LinkToPdf);

    Set("FinanceDocId",DocNum);

}

return true;

 

Furthermore, I attached a screenshot with the methods that I'm using in the process

 

Hope it helps.

 

Have fun :)

Good luck

 

Like 1

Like

Share

2 comments

Hi!

 

Thank you for contacting us! 

 

Could you please provide us with additional screenshots of the issue appeared to understand the case better?

 

Regards, 

Anastasiia

Anastasiia Markina,

Hi Anastasiia,



There is no issue. I struggled with this issue and I solved it.



The above code is my collaboration to the community :)

 

Hope it helps devs with the same issue.

 

Best Regards,

Raz

Show all comments

Hello,

I tried the script task to read multiple records as below:

EntityCollection entities = Get("MyEntity");

var result = new Collection();

foreach(Entity entity in entities) {

                var productName = entity.GetTypedColumnValue("Name");

                string temp = productName.ToString();

                result.Add(temp);

                }

string displayValue = result.ConcatIfNotEmpty(",");

Set("UsrLatestOrderProduct", displayValue);

return true;

But, here I'm getting below errors:

Like 0

Like

1 comments

Dear Riddhi,

 

The issue happened due to the incorrect using of generic methods (Get<T> and GetTypedColumnValue<TResult>) and generic type (Collection<T>). In order to resolve the issue you should add the type argument for every generic construction in the code. Please see the example of the code:

 

EntityCollection entities = Get<type>(“MyEntity”);

var result = new Collection<type>();

var productName = entity.GetTypedColumnValue<string>(“Name”);

 

Please find additional information in the article by the link below:

 

https://academy.creatio.com/documents/technic-bpms/7-15/script-task-process-element

 

Best regards,

Norton

Show all comments

Is there any formalized documentation on how to create/modify script tasks.

 

To be more specific how would one make use of a process parameter inside a Script task?

Like 0

Like

4 comments

Hello Michael, 

Please, check the following link to find the required information: https://academy.bpmonline.com/documents/technic-bpms/7-14/script-task-process-element

Best regards,

Olga

Olga Avis,

Is there any additional documentation?

Michael Dorfman,

Script tasks - are c# methods inside a business process class. Please feel free to open a source code of a business process with a script task and you'll see a simple c# class. It works according to the native c# documentation.

https://www.oreilly.com/library/view/essential-c-70/9781509303595/

Your second question was completely covered by the article mentioned in the Olga's post.

Eugene Podkovka writes:

Michael Dorfman,

Script tasks - are c# methods inside a business process class. Please feel free to open a source code of a business process with a script task and you'll see a simple c# class. It works according to the native c# documentation.

https://www.oreilly.com/library/view/essential-c-70/9781509303595/

Your second question was completely covered by the article mentioned in the Olga's post.

 In addition to my question is there any documentation on the classes and methods available for use such as reading data in the system using a script task?

Previously I was directed to review existing script tasks but that only explains so much.

Show all comments

Is there a way to read all the users inside an organizational role and then send those email addresses to the next step of a process?

Like 0

Like

2 comments

The functionality can be implemented using a script task element in order to select all contact emails which are included in the role. The contact emails are selected from the database via EntitySchemaQuery. Then join all selected emails into a string using ";" as separator. After that use the newly built string of emails as an email for sending.

The basic business process "Send email to case group" works in the same way. Please feel free to use the business process as an example. Pay attention to the script task element "Prepare Recipient Emails" (https://prnt.sc/p0tv3t).

Show all comments

I am trying to generate a printable from the opportunity object (which will be a quote) and attach this printable to an email after generating.

 

I am getting stuck on the generate and save printable script task. 

This is the error I am getting

Terrasoft.Common.ItemNotFoundException: Value "Name" was not found.
   at Terrasoft.Core.Entities.EntityColumnValueCollection.GetByName(String name)
   at Terrasoft.Core.Entities.Entity.SetColumnValue(String valueName, Object value)
   at Terrasoft.Core.Process.UsrlabSavePrintableAndEmailQuote1Custom6.ScriptTask1Execute(ProcessExecutingContext context)
   at Terrasoft.Core.Process.ProcessFlowElement.Execute(ProcessExecutingContext context)

 

I'm not sure what the issue is. I tried hardcoding the name in the script but that doesn't seem to work either. 

 

My script action that is failing.

var reportService = new Terrasoft.Configuration.ReportService.ReportService();
Terrasoft.Configuration.ReportService.ReportData report = reportService.GenerateMSWordReport(
    (PrintableId.ToString()), ObjectId.ToString(), ConvertToPdf);
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("Opportunity");
var fileEntity = entity.CreateEntity(UserConnection);
fileEntity.SetDefColumnValues();
fileEntity.SetColumnValue("Id", OpportunityId);
fileEntity.SetColumnValue("TypeId", AttachmentType);
fileEntity.SetColumnValue("Name", "ExamplePrintable.docx");
fileEntity.SetColumnValue("Data", report.Data);
fileEntity.Save();
 
 
return true;

 

Let me know if you need any more information.

Also, any documentation where I can read up on this myself. Like the fileEntity variable, or just how this code works in general.

 

Any help is appreciated thanks!

Like 0

Like

1 comments

Hi Tyler,

Implemented this week a very similiar feature throw a business process.

I reversed engeneered this market place app: https://marketplace.bpmonline.com/template/creating-quotes-opportunities

But I used the user task instead of the script task in the processes as I was getting in trouble passing the process parameters to the script task.

 

Hope it helps,

Luis

Show all comments

I am running this code as a script task in a business process.

string POid = Get<string>("PurchaseOrderID");
var arParts = POid.Split('_');
 
 
		int i = Convert.ToInt32(arParts[1]);
		int newid = i + 1;
		string prefix = string.Empty;
		if(newid<10){
			prefix="00000";
		}
		else if (newid<100)
		{ prefix="0000";
		}
		else if (newid<1000)
		{
			prefix="000";
		}
		else if (newid<10000)
		{
			prefix="00";
		}
		else if (newid<100000)
		{
			prefix="0";
		}
 
		string intstringid = newid.ToString();
		string name = prefix + intstringid; 
		string newPOid = "PO_" + name;
		Set("NewPurchaseOrderID", newPOid);
return true;

I have "PurchaseOrderID" and "NewPurchaseOrderID" as the parameters that I've defined for this business process. They are spelled as in the code, I've checked that.

 

When I try to run this process, I get the error: Object reference not set to an instance of an object.

 

Can't seem to find which object I have not initiated.

 

Any help would be appreciated.

 

Thanks.

-AK

Like 0

Like

1 comments

It seems that  "PurchaseOrderID"  parameter has no value, so in the following row

var arParts = POid.Split('_')

the error "Object reference not set to an instance of an object" occurs because POid is null. To know for sure please debug the code. The article by the link below will be helpful in that: https://academy.bpmonline.com/documents/technic-sdk/7-13/server-code-debugging

Show all comments

Hi,

I have a EntityCollection and i would like to know how can i add Objects to that EntityCollection in my ScriptTask.

EntityCollection ec = Get("ProcessParameter1");

var entity;

ec.Add(entity);

Set("ProcessParameter1",ec);

Like 0

Like

1 comments

The Add method is correct. Additionally, I'd not recommend using a simple list instead. 

Show all comments

Hi,

I'm currently trying to obtain the "Id" from one of the contacts in my "Contact" table but I'm getting the same error:

Terrasoft.Common.ItemNotFoundException: Value "Id" was not found.

   at Terrasoft.Core.Entities.EntityColumnValueCollection.GetByName(String name)

   at Terrasoft.Core.Entities.Entity.InternalGetColumnValue(String valueName)

   at Terrasoft.Core.Entities.Entity.GetColumnValue(String valueName)

   at Terrasoft.Core.Process.UsrProcess2MethodsWrapper.ScriptTask1Execute(ProcessExecutingContext context)

   at Terrasoft.Core.Process.ProcessScriptTask.InternalExecute(ProcessExecutingContext context)

   at Terrasoft.Core.Process.ProcessFlowElement.Execute(ProcessExecutingContext context)

 

This is the script task i'm using for it:

var result = "";

var userConnection = Get("UserConnection");

    

EntitySchemaManager esqManager = userConnection.EntitySchemaManager;

var rootEntitySchema = esqManager.GetInstanceByName("Contact") as EntitySchema;

var esqResult = new EntitySchemaQuery(rootEntitySchema);

esqResult.AddColumn("Id");

esqResult.AddColumn("Name");

var entities = esqResult.GetEntityCollection(UserConnection);

result = entities[0].GetColumnValue("Id").ToString();



Set("ProcessSchemaParameter1", result);

return true;

Note: If i try to get the "Name" instead i dont get any error.

Like 0

Like

1 comments

Try this

var opportunityCarQuery = new EntitySchemaQuery(UserConnection.EntitySchemaManager,"OpportunityCar");
opportunityCarQuery.AddAllSchemaColumns();
 
var filter = opportunityCarQuery.CreateFilterWithParameters(FilterComparisonType.Equal, "Car", (Guid)carNodeId);
opportunityCarQuery.Filters.Add(filter);
 
var opportunityCarEntities = opportunityCarQuery.GetEntityCollection(UserConnection);
 
foreach (var opportunityCarEntity in opportunityCarEntities)
{
	var theId = opportunityCarEntity.GetTypedColumnValue&lt;Guid&gt;("Id");
}

 

Show all comments

I've created a webservice that passes the received data from my api to a "Collection of Objects with attributes" parameter inside my business process.

I would like to know how can i convert that business process parameter to a JArray.



JArray jarray = Get("ProcessSchemaCadastros");

This is the line i have in my script task to get my data from business process parameter.

Like 0

Like

2 comments
Best reply

I found a way to pass my problem, instead of trying to convert into a JArray i used:

var entities = Get<ICompositeObjectList<ICompositeObject>>("WebService1.UsrWBCadastros_Out");

then i just worked with the data from 'entities'.

 

It's much easier to transfer a JSON from the web service to a text variable in a business process. Then parse the JSON in the business process.

I found a way to pass my problem, instead of trying to convert into a JArray i used:

var entities = Get<ICompositeObjectList<ICompositeObject>>("WebService1.UsrWBCadastros_Out");

then i just worked with the data from 'entities'.

 

Show all comments

Hi, 

 

I want to call a business process from a script task of another business process. I did this because I want to send email to a collection of contacts, so I will do a foreach loop in the script task, and call the second business proces that will send the emails. 

The problem is that the second business process, its call from the first one, and appears in the process log as running, but it doesnt do anything, neither send the mail nor finish running. 

The script task code:

var _sendMailId = new Guid("451f9ef3-2a11-4ca8-9107-02e23e56f9f3");

var _manager = UserConnection.ProcessSchemaManager;

var _schema = (ProcessSchema)_manager.GetInstanceByUId(_sendMailId);

var _moduleProcess = _schema.CreateProcess(UserConnection);

_moduleProcess.Execute(UserConnection);



return true;

First business process: Mutiple email sender

 

 

Second Business process: MBS Send Email

Process log:

 

 

Thank you.

 

Javier Collazo

 

Like 0

Like

2 comments
Best reply

The problem here was that the business process that was call form the parent business process, wasn't receiving the parameter. The solution to this problem you can find it in the following question:



https://community.bpmonline.com/questions/calling-business-process-para…

 

Best regards, 

Javier

Hello,



At first you should be confident about business process that sends email is working properly. So, try to start it manually. If it will work, then something is wrong with your script task. Perhaps, you have just missed some parameters that are required in "MBS send email process" 

At second, try to save the process once again and compile the system.



Best regards,

Alex

The problem here was that the business process that was call form the parent business process, wasn't receiving the parameter. The solution to this problem you can find it in the following question:



https://community.bpmonline.com/questions/calling-business-process-para…

 

Best regards, 

Javier

Show all comments