Compartilhar via


UpdatePolicy

Description

Updates a policy instance for the specified role. Changes made to the behaviors for the policy instance will be saved.

Parameters

Parameter

Type

Description

policy

Policy

The policy for the policy instance that is being updated.

roleKey

RoleKey

The role key object that specifies the role for the policy instance being updated.

context

Context

Specifies information about how the method will be called.

Interfaces

  • Dynamics GP
  • Common
  • Field Service
  • Financials
  • Human Resources/Payroll
  • Inventory
  • Manufacturing
  • Project Accounting
  • Purchasing
  • Sales

Examples

The following C# example retrieves the Update Customer policy for the default role. It modifies the selected behavior option for the Create Active behavior, and then updates the default instance of the policy.

Cc508648.LegacyEndpoint(en-us,MSDN.10).gif** Legacy endpoint**

using System;
using System.Collections.Generic;
using System.Text;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Policy createCustomerPolicy;
            PolicyKey policyKey;
            RoleKey roleKey;
            Behavior createActiveBehavior;
            BehaviorOption setToInactiveBehaviorOption;
            Guid behaviorGUID;
            int behaviorOptionID;

            // Create an instance of the service
            DynamicsGP wsDynamicsGP = new DynamicsGP();

            // Be sure that default credentials are used
            wsDynamicsGP.UseDefaultCredentials = true;

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (sample company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context object
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create the role key for the role (default role)
            roleKey = new RoleKey();
            roleKey.Id = "00000000-0000-0000-0000-000000000000";

            // Get the complete policy for Create Customer
            policyKey = new PolicyKey();
            policyKey.Id = new Guid("94880780-186c-43c8-b484-efcdd03cbdbe");
            createCustomerPolicy = wsDynamicsGP.GetCompletePolicyByKey(policyKey, roleKey, context);

            // Set the behavior for the 'Create Active' behavior
            // Find the behavior in the list (from Web Service Reference)
            behaviorGUID = new Guid("5c098de9-952c-478c-ad4b-cf992b55ea46");
            createActiveBehavior = new Behavior();
            foreach (Behavior b in createCustomerPolicy.Behaviors)
            {
                if (b.Key.Id == behaviorGUID)
                {
                    // Behavior was found
                    createActiveBehavior = b;
                    break;
                }
            }

            // Find the behavior option (from Web Service Reference)
            behaviorOptionID = 2;
            setToInactiveBehaviorOption = new BehaviorOption();
            foreach (BehaviorOption bo in createActiveBehavior.Options)
            {
                if (bo.Key.Id == behaviorOptionID)
                {
                    // Behavior option was found
                    setToInactiveBehaviorOption = bo;
                    break;
                }
            }

            // Set selected behavior option to create inactive customers
            createActiveBehavior.SelectedOption = setToInactiveBehaviorOption;

            // Update the policy instance for the default role
            wsDynamicsGP.UpdatePolicy(createCustomerPolicy, roleKey, context);
        }
    }
}

Cc508648.NativeEndpoint(en-us,MSDN.10).gif** Native endpoint **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Policy createCustomerPolicy;
            PolicyKey policyKey;
            RoleKey roleKey;
            Behavior createActiveBehavior;
            BehaviorOption setToInactiveBehaviorOption;
            Guid behaviorGUID;
            int behaviorOptionID;

            // Create an instance of the service
            DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (sample company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context object
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create the role key for the role (default role)
            roleKey = new RoleKey();
            roleKey.Id = "00000000-0000-0000-0000-000000000000";

            // Get the complete policy for Create Customer
            policyKey = new PolicyKey();
            policyKey.Id = new Guid("94880780-186c-43c8-b484-efcdd03cbdbe");
            createCustomerPolicy = wsDynamicsGP.GetCompletePolicyByKey(policyKey, roleKey, context);

            // Set the behavior for the 'Create Active' behavior
            // Find the behavior in the list (from Web Service Reference)
            behaviorGUID = new Guid("5c098de9-952c-478c-ad4b-cf992b55ea46");
            createActiveBehavior = new Behavior();
            foreach (Behavior b in createCustomerPolicy.Behaviors)
            {
                if (b.Key.Id == behaviorGUID)
                {
                    // Behavior was found
                    createActiveBehavior = b;
                    break;
                }
            }

            // Find the behavior option (from Web Service Reference)
            behaviorOptionID = 2;
            setToInactiveBehaviorOption = new BehaviorOption();
            foreach (BehaviorOption bo in createActiveBehavior.Options)
            {
                if (bo.Key.Id == behaviorOptionID)
                {
                    // Behavior option was found
                    setToInactiveBehaviorOption = bo;
                    break;
                }
            }

            // Set selected behavior option to create inactive customers
            createActiveBehavior.SelectedOption = setToInactiveBehaviorOption;

            // Update the policy instance for the default role
            wsDynamicsGP.UpdatePolicy(createCustomerPolicy, roleKey, context);

            // Close the service
            if(wsDynamicsGP.State != CommunicationState.Faulted)
            {
                wsDynamicsGP.Close();
            }
        }
    }
}