/// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the CustomFieldService.
      CustomFieldService customFieldService = (CustomFieldService) user.GetService(
          DfpService.v201311.CustomFieldService);

      // Create statement to select only active custom fields that apply to
      // line items.
      String statementText = "WHERE entityType = :entityType and isActive = :isActive LIMIT 500";
      Statement filterStatement = new StatementBuilder(statementText)
              .AddValue("entityType", CustomFieldEntityType.LINE_ITEM.ToString())
              .AddValue("isActive", true)
              .ToStatement();

      // Set defaults for page and offset.
      CustomFieldPage page = new CustomFieldPage();
      int offset = 0;
      int i = 0;
      List<string> customFieldIds = new List<string>();

      try {
        do {
          // Create a statement to page through custom fields.
          filterStatement.query = statementText + " OFFSET " + offset;

          // Get custom fields by statement.
          page = customFieldService.getCustomFieldsByStatement(filterStatement);

          if (page.results != null) {
            foreach (CustomField customField in page.results) {
              Console.WriteLine("{0}) Custom field with ID \"{1}\" and name \"{2}\" will be " +
                  "deactivated.", i, customField.id, customField.name);
              customFieldIds.Add(customField.id.ToString());
              i++;
            }
          }
          offset += 500;
        } while (offset < page.totalResultSetSize);

        Console.WriteLine("Number of custom fields to be deactivated: " + customFieldIds.Count);

        if (customFieldIds.Count > 0) {
          // Modify statement for action.
          filterStatement.query = "WHERE id IN (" + string.Join(", ", customFieldIds.ToArray()) +
              ")";

          // Create action.
          DeactivateCustomFields action = new DeactivateCustomFields();

          // Perform action.
          UpdateResult result = customFieldService.performCustomFieldAction(
              action, filterStatement);

          // Display results.
          if (result != null && result.numChanges > 0) {
            Console.WriteLine("Number of custom fields deactivated: " + result.numChanges);
          } else {
            Console.WriteLine("No custom fields were deactivated.");
          }
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to deactivate custom fields. Exception says \"{0}\"", ex.Message);
      }
    }
Exemple #2
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The DFP user object running the code example.</param>
        public override void Run(DfpUser user)
        {
            // Get the CustomFieldService.
            CustomFieldService customFieldService = (CustomFieldService)user.GetService(
                DfpService.v201311.CustomFieldService);

            // Create statement to select only active custom fields that apply to
            // line items.
            String    statementText   = "WHERE entityType = :entityType and isActive = :isActive LIMIT 500";
            Statement filterStatement = new StatementBuilder(statementText)
                                        .AddValue("entityType", CustomFieldEntityType.LINE_ITEM.ToString())
                                        .AddValue("isActive", true)
                                        .ToStatement();

            // Set defaults for page and offset.
            CustomFieldPage page           = new CustomFieldPage();
            int             offset         = 0;
            int             i              = 0;
            List <string>   customFieldIds = new List <string>();

            try {
                do
                {
                    // Create a statement to page through custom fields.
                    filterStatement.query = statementText + " OFFSET " + offset;

                    // Get custom fields by statement.
                    page = customFieldService.getCustomFieldsByStatement(filterStatement);

                    if (page.results != null)
                    {
                        foreach (CustomField customField in page.results)
                        {
                            Console.WriteLine("{0}) Custom field with ID \"{1}\" and name \"{2}\" will be " +
                                              "deactivated.", i, customField.id, customField.name);
                            customFieldIds.Add(customField.id.ToString());
                            i++;
                        }
                    }
                    offset += 500;
                } while (offset < page.totalResultSetSize);

                Console.WriteLine("Number of custom fields to be deactivated: " + customFieldIds.Count);

                if (customFieldIds.Count > 0)
                {
                    // Modify statement for action.
                    filterStatement.query = "WHERE id IN (" + string.Join(", ", customFieldIds.ToArray()) +
                                            ")";

                    // Create action.
                    DeactivateCustomFields action = new DeactivateCustomFields();

                    // Perform action.
                    UpdateResult result = customFieldService.performCustomFieldAction(
                        action, filterStatement);

                    // Display results.
                    if (result != null && result.numChanges > 0)
                    {
                        Console.WriteLine("Number of custom fields deactivated: " + result.numChanges);
                    }
                    else
                    {
                        Console.WriteLine("No custom fields were deactivated.");
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to deactivate custom fields. Exception says \"{0}\"", ex.Message);
            }
        }