Dynamics CRM - to abandon Business Process Flow through C # Plugin

Statement of needs:

      When there is a Entity Business Process Process, sometimes we need to change its state, before writing the blog has talked about can be achieved through JavaScript, Benpian it in terms of how to achieve the abandon of the BPF (abandon post by C # Plugin BPF will turn gray, BPF in the Stages become non-editable, you can not click the Back and Next, we can not Set Active; if you want to make Steps also can not be edited, can be controlled by JavaScript).

solution:

      Entities can be found when viewing components by Solution: Add an Entity in to a Business Process Flow of time, actually created a new Entity (later referred to BPF Entity), the relationship between the Entity and the BPF Entity is 1: N. Field of BPF its Primary Entity Entity exists Lookup, can be queried in the current BPF Entity through the field, then change its value on the State and Stauts can abandon the BPF.

Note: State -> StateCode, the Status -> StatusCode, these two fields are the BPF the Entity Default Field, each Entity has, showing a state, the state is changed by changing the Entity values of these two fields.

Sample code:

private void AbandonBPF(IOrganizationService service, Guid new_entity_id)
{
    using (OrganizationServiceContext orgService = new OrganizationServiceContext(service))
    {
        var bpf_entity = (from _bpf_entity in orgService.CreateQuery<Entities.new_bpfentity>()
                          where _bpf_entity.bpf_new_entityid.Id == new_entity_id
                          select _bpf_entity).FirstOrDefault();

        if (bpf_entity != null && bpf_entity.GetAttributeValue<OptionSetValue>("statecode").Value == 0)
        {
            //statecode = 1 and statuscode = 3 for abandon workflow
            SetStateRequest setStateRequest = new SetStateRequest()
            {
                EntityMoniker = new EntityReference
                {
                    Id = bpf_entity.BusinessProcessFlowInstanceId.Value,
                    LogicalName = Entities.new_bpfenity.EntityLogicalName,
                },
                State = new OptionSetValue(1),
                Status = new OptionSetValue(3)
            };
            service.Execute(setStateRequest);
        }
    }
}

Note: Here is new_entity Entity name, new_bpfentity BPF Entity is the corresponding name.

Function call:

Entities.new_entity entity =  ((Entity)context.InputParameters["Target"]).ToEntity<Entities.new_entity>();
AbandonBPF(service, entity.id);

 

Guess you like

Origin www.cnblogs.com/Sunny20181123/p/10936465.html