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

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

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

  • Comments 5
  • Likes

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

<!-- #include file ="sagecrm.js"-->
<!-- Typical ASP Page Structure (Try - Catch) -->
<%
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.

  

Comments
  • Hi Jeff,

    I attempted to adapt this code to create a static group and on the surface it did seem to work, however I'm guessing that because of a static groups releationship to the key attribute tables there might be more to this?

    In particular, I am unable to use the "Add records to this group" functionality as this "cannot find" the group, and editing the group definition via the UI does not render the search criteria columns, and so the WhereClause used to create the group is ignored.

    Any ideas?

    Daniel

  • There is a Property 'IsFixedGroup' of TargetList Oject which is not documented. You need to set this for a static group:

    TargetBlock.IsFixedGroup = true;

    and then of course:

    TargetListRecord.repo_reportstyle = 'GroupTypeFixed';

    Also not documented: TargetBlock.IsNewGroupFromFind. But I don't know what this does.

    But Jeff: how can one add records to a static group via the ASP-API? This doesn't seem to work:

    function AddToTargetList(TargetListId, projectRecordId) {

           var TargetBlock = CRM.TargetLists;

           TargetBlock.TargetListID = TargetListId;

           TargetBlock.Retrieve();

           TargetBlock.Include(projectRecordId);

           TargetBlock.Retrieve();

           TargetBlock.Save();

    }

  • Has anybody got this to work on 7.1. I always get: An error occurred on the server when processing the URL. Please contact the system administrator

    I did have in working in 6.2

    Thanks

  • If this is not working in Sage CRM v7.1 then you may need to log a case.

  • Hello,

    I have problems with CRM 7.2.f.5. Does work the Target list com api in new Sage CRM versions?

    I want to retrieve an existing TargetList (static group), but don't work as in the API reference manual.