Creating a Dynamic Group in Sage CRM using the COM object TargetLists

2 minute read time.

The COM API has an object within it called TargetLists. This object can be used within internal script (Validate, Table Level Script etc) to create both Dynamic and Static Groups.

Below is a simple example of the object being used in an ASP page to create a Dynamic Group. You will see that I had to 'tweak' the code to make sure the created group was completely defined.

You can read about the TargetList object in the online documentation.

I have marked the code in sections. Please see the notes at the end of this article.

Example Code



<%
TargetBlock = CRM.TargetLists;
TargetBlock.TargetListID = 0;
TargetBlock.Category = "Person";
TargetBlock.Name = "Example List";
TargetBlock.Description = "List created using API";
TargetBlock.ViewName = "vTargetListPerson";
TargetBlock.WhereClause = "Addr_City = N'London'";

//section 2
TargetField = TargetBlock.Fields.New();
TargetField.DataField = "Comp_Name";
TargetField = TargetBlock.Fields.New();
TargetField.DataField = "Pers_LastName";
TargetField = TargetBlock.Fields.New();
TargetField.DataField = "Pers_FirstName";

//section 3
TargetField = TargetBlock.OrderByFields.New();
TargetField.DataField = "Pers_LastName";

//section 4
TargetQuery = TargetBlock.Retrieve();

//section 5
TargetBlock.Save();

//section 6
var TargetListRecord = CRM.FindRecord("custom_reports","repo_reportid="+TargetBlock.TargetListID);
TargetListRecord.repo_reportstyle = 'GroupTypeDynamic';
TargetListRecord.repo_printoptions = 1;
TargetListRecord.repo_privateuserid = 0;
TargetListRecord.SaveChanges();

//section 7
Response.Redirect(CRM.URL(580));
%>

Notes

Section 1

The TargetListID is set to zero to indicate a new target list is to be created.
The category is set. Other valid categories for Dynamic Groups would be the standard Categories used in Sage CRM e.g. Cases, Company, Lead, Opportunity, Order, Person and Quote.
The name of the target list should be unique.
The view used and the whereclause needed is set.

Section 2

At least one display field has to be specified and all fields must be returned by the view referenced in section 1.
You need to create a display field and specify its database fieldname. This is repeated for each other optional field needed.

Section 3

Order by fields to sort the target list can be added. You need to create a new order by field and specify its database fieldname.

Section 4

The SQL query that underlies the Group (defined as the view and whereclause) needs to be invoked to allow the Group to be saved.

Section 5

The Group can then be saved.

Section 6

I found the code for the TargetList object needed to 'tweaked' to get it to work fully for Dynamic Groups. The newly created Group had to be updated. The Group definition is held in the Custom_reports table. As useful background article is "Reports, Saved Searches and Groups".

The report style (repo_reportstyle) has to be set and the valid values for Groups are 'GroupTypeDynamic' or 'GroupTypeStatic'.

The field repo_printoptions needs to be set to equal 1 for both Static and Dynamic Groups (Target Lists). This has been discussed previously in the article "custom_reports settings and Summary Reports".

The field repo_privateuserid has to be set to 0 to all all users to see the newly created Group. Please see the article "Making a Private Group (Target List) Public" for more details.

The changes to the custom_reports record then need to be saved in the database.

Section 7

The screen is then redirected to the Group listing and should show the newly created dynamic group listed with the other groups.