Using policy in a web service application
The create, update, and delete or void methods in the Dynamics GP service require the appropriate policy object to be passed to them. Your web service application must retrieve the appropriate policy instance to use. Your application can also control the external behaviors for the policy.
Retrieving a policy instance
To retrieve a policy instance to use for a method, use one of the following:
- GetPolicyByKey
- GetPolicyByOperation
Which you choose depends on whether you prefer to have GUIDs in your code that identify policies, or string values for operation names.
When a web service application retrieves the policy instance, the appropriate one will be returned, based on the context object that was used. The policy information will come from the company specified by the context object. If the context object has a role specified, the policy instance defined for that role will be returned. If no policy instance exists for that role within the specified company, the default policy for that operation will be returned.
If you don't supply a role key, the Dynamics GP service will attempt to find a role for the user and company specified in the context object. If only one role can be found, that role will be used. If more than one role is found, or no roles are found, the default role will be used.
If you want to use a policy instance for a specific role, be sure that you specify the role key for the context object. Otherwise, it's best to leave the role key property empty. This allows the Dynamics GP service administrator to control which policy instances are used based on the role a user is assigned to.
Setting external behaviors
After your web service application has retrieved a policy instance, you can decide whether to change the values for any external behaviors. This process involves locating the external behavior in the policy instance, finding the behavior option you want to use, and setting the selected behavior option.
**You will need to use the Dynamics GP Web Service Reference to find the GUID value that identifies the behavior you want to set. The reference will also provide the ID of the behavior option you want to use. For example, the following mutli-part C# example demonstrates how the "Create Reversing Transaction" behavior for the Create GL Transaction policy is used. First, the behavior is found within the policy instance:
Policy transactionCreatePolicy; Behavior reversingTrxBehavior; Guid behaviorGUID; BehaviorOption reversingTrxBehaviorOption; int behaviorOptionID; // Get the create policy for GL transactions transactionCreatePolicy = wsDynamicsGP.GetPolicyByOperation( "CreateGLTransaction", context); // Find the behavior in the list (from Dynamics GP Service Reference) behaviorGUID = new Guid("b0c9fe57-c4f0-4d6a-b0aa-ab4d6d7596c8"); reversingTrxBehavior = new Behavior(); foreach (Behavior b in transactionCreatePolicy.Behaviors) { if (b.Key.Id == behaviorGUID) { // Behavior was found reversingTrxBehavior = b; break; } }
**The next portion of the C# code demontrates how the behavior option to create a reversing transaction is found:
// Find the behavior option (from Dynamics GP Service Reference) behaviorOptionID = 2; reversingTrxBehaviorOption = new BehaviorOption(); foreach (BehaviorOption bo in reversingTrxBehavior.Options) { if (bo.Key.Id == behaviorOptionID) { // Behavior option was found reversingTrxBehaviorOption = bo; break; } }
**Some behavior options have a parameter that can be set. In this case, the date of the reversing transaction can be set. The next portion of the C# code shows how the parameter for the reversing date is set:
// Set the reversing date for the behavior option reversingTrxBehaviorOption.Parameters[0].Value = reversingTrxDate.ToShortDateString();
**Finally, the selected behavior option must be set for the behavior. The remainder of the C# code shows how the selected behavior is set for the "Create Reversing Transaction" behavior:
// Set selected behavior option to create a reversing transaction reversingTrxBehavior.SelectedOption = reversingTrxBehaviorOption;
The policy instance for the Create GL Transaction policy is ready to be passed to the CreateGLTransaction web method.