Run a sub process for each record in a selected folder

Hi all,

I have a process where a user selects a contact folder from a list on a preconfigured page. A sub process then needs to run for each contact in the selected folder.

I've read the article for Programmatically Using Section Folder Filters in Processes or Server-Side Code in Creatio | Customer FX and I have borrowed a script task for creating the list of contacts.

var sectionName = "Contact";
var folderId = "2d3c0306-1e43-4ba7-943b-a3d261b66897"; //I will pass this in from the preconfigured page
 
// get folder SearchData
var folderSchema = UserConnection.EntitySchemaManager.GetInstanceByName(sectionName + "Folder");
var esq = new EntitySchemaQuery(folderSchema);
var dataCol = esq.AddColumn("SearchData").Name;
var folderData = esq.GetEntity(UserConnection, folderId).GetBytesValue(dataCol);
 
// convert filter data to esq filters
var serializedFilters = System.Text.Encoding.UTF8.GetString(folderData, 0, folderData.Length);
var dataSourceFilters = Terrasoft.Common.Json.Json.Deserialize<Terrasoft.Nui.ServiceModel.DataContract.Filters>(serializedFilters);
 
// MUST INCLUDE using Terrasoft.Nui.ServiceModel.Extensions;
var folderFilters = dataSourceFilters.BuildEsqFilter(UserConnection.EntitySchemaManager.GetInstanceByName(sectionName).UId, UserConnection);
 
// now can include folderFilters as filters in new esq
var contactEsq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact");
contactEsq.AddColumn("Id");
contactEsq.Filters.Add(folderFilters); // using the filters from the folder
var contacts = contactEsq.GetEntityCollection(UserConnection);

I would like to either pass the results into a collection or run a sub process for each contact directly from the script task.

Any help would be much appreciated,

Like 0

Like

3 comments

An easy way to accomplish what you're after is to: 

  1. Create a collection parameter in the process
  2. Populate the contacts from the ESQ into the collection parameter (see link below)
  3. Then use normal subprocess for each contact in the collection

See here for how to add the results of the ESQ from the folder to a collection param: https://customerfx.com/article/working-with-collection-parameters-in-a-process-in-creatio/

Ryan

Ryan Farley,

Thanks Ryan. I'll give this a go now.

Ryan Farley,

I'm still struggling to get the result I'm after with the following code:

Parameters: 
- ContactFolderId (Guid)
- ContactsList (Collection)
    - ContactsList.ContactId
    - ContactsList.Name

var sectionName = "Contact";
var folderId = Get&lt;Guid&gt;("CommunityFolderId"); //"2d3c0306-1e43-4ba7-943b-a3d261b66897";
 
	var folderSchema = UserConnection.EntitySchemaManager.GetInstanceByName(sectionName + "Folder");
	var esq = new EntitySchemaQuery(folderSchema);
	var dataCol = esq.AddColumn("SearchData").Name;
 
	var folderEntity = esq.GetEntity(UserConnection, folderId);
	var folderData = folderEntity.GetBytesValue(dataCol);
 
	var serializedFilters = System.Text.Encoding.UTF8.GetString(folderData, 0, folderData.Length);
	var dataSourceFilters = Terrasoft.Common.Json.Json.Deserialize&lt;Terrasoft.Nui.ServiceModel.DataContract.Filters&gt;(serializedFilters);
 
	var folderFilters = dataSourceFilters.BuildEsqFilter(
		UserConnection.EntitySchemaManager.GetInstanceByName(sectionName).UId,
		UserConnection
	);
 
	var contactEsq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact");
	contactEsq.AddColumn("Id");
	contactEsq.AddColumn("Name");
	contactEsq.Filters.Add(folderFilters);
 
	var contacts = contactEsq.GetEntityCollection(UserConnection);
 
	var list = Get&lt;CompositeObjectList&lt;CompositeObject&gt;&gt;("ContactsList");
 
	foreach (var contact in contacts)
	{
		var item = new CompositeObject();
		item["ContactId"] = contact.GetTypedColumnValue&lt;string&gt;("Id");
		item["Name"] = contact.GetTypedColumnValue&lt;string&gt;("Name");
		list.Add(item);
	}
return true;

When I run the process (passing 2d3c0306-1e43-4ba7-943b-a3d261b66897 into the ContactFolderId parameter, I'm getting an error saying:


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.GetTypedColumnValue[TResult](String valueName)

I tried changing it to the following alternatives but I got the same error:

var item = new CompositeObject();
	item["ContactId"] = contact.GetTypedColumnValue&lt;Guid&gt;("Id");

and

var item = new CompositeObject();
	item["ContactId"] = contact.Get&lt;Guid&gt;("Id");

I feel like I'm missing something obvious somewhere.

Show all comments