Example: Creating and saving a Target list

Copy
// Shows an example of creating and saving a target list
// All steps are compulsory and should be in this order

<!-- #include file ="sagecrm.js" -->
<%

TargetBlock = CRM.TargetLists;

// Get the TargetBlock COM Object from the CRM base objectTargetBlock.
TargetListID = 0;

// Set the ID to zero, to indicate a new target listTargetBlock.
Category = "Person";

// Set the category. Other valid categories are Company and LeadTargetBlock.
Name = "COM List 1";

// Set the name of the target list, should be unique
TargetBlock.IsNewGroupFromFind = true;

// Set the target list to be a group rather than a saved search
TargetBlock.GroupAccessLevel  = 0;

// Enable all users to access the group
TargetBlock.IsFixedGroup = false;

// Specify that the group is dynamic
TargetBlock.ViewName = "vTargetListPerson";

// Set the view to be usedTargetBlock.
WhereClause = "Addr_City = N'London'";

// If required, specify a where clause.
// You must specify at least one display field. All fields must be returned by the view.
TargetField = TargetBlock.Fields.New();

// Create a new display fieldTargetField.
DataField = "Comp_Name";

// Specify its database field name
TargetField = TargetBlock.Fields.New();

// Create a second display name, optional
TargetField.DataField = "Pers_LastName";
TargetField = TargetBlock.Fields.New();

// Create a third display field, optional
TargetField.DataField = "Pers_FirstName";

// Add more fields as desired. You may add order by fields to sort the target 
listTargetField = TargetBlock.OrderByFields.New(); 

// Create a new order by field
TargetField.DataField = "Pers_LastName"; 

// Specify its database field name
TargetQuery = TargetBlock.Retrieve(); 

// Create and return the target list based on the above settings
// This demonstrates cycling through the returned targets, and setting every tenth target to be excluded
while (!TargetQuery.EOF)
    {
        I = 1;
        while ((!TargetQuery.EOF) && (I < 10))
            
            {
                TargetQuery.Next();
                I++;
            }
        if (!TargetQuery.EOF)
            {
                j = TargetQuery.FieldValue("Pers_PersonID");
                TargetBlock.Exclude(j);
            }
    }

// For the moment, we always return to the Actions page whether successful or not.
// 580 is the action number to go back to the target list browser page
// 585 is the action number to go back to the target list actions page
if (TargetBlock.Save()) 

// Save the target list
    {
        Response.Redirect(CRM.URL(580));
    }

else
    {
        Response.Redirect(CRM.URL(580));
    }

%>