crm操作销售合同实体

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;

    ///


    /// 销售合同
    ///

    public class ContractHelper
    {
        public static readonly string entityName = "contract";
        public Guid contractId = Guid.Empty;
        public IOrganizationService service;

        ///


        /// 创建销售合同
        ///

        public void Create()
        {
            Entity en = new Entity() { LogicalName = entityName };
            en["title"] = "销售合同测试";
            contractId = service.Create(en);
        }

        ///


        /// 将销售合同分派给其他用户或团队
        ///

        /// 用户或团队引用
        public void Assign(EntityReference assignee)
        {
            AssignRequest request = new AssignRequest();
            request.Target = new EntityReference() { LogicalName = entityName, Id = contractId };
            request.Assignee = assignee;
            AssignResponse response = (AssignResponse)service.Execute(request);
        }

        ///


        /// 取消合同
        ///

        /// 取消的状态
        public void CancelContract(int status)
        {
            CancelContractRequest request = new CancelContractRequest();
            request.ContractId = contractId;
            request.CancelDate = DateTime.Now;
            request.Status = new OptionSetValue(status);
            CancelContractResponse response = (CancelContractResponse)service.Execute(request);
        }

        ///


        /// 复制合同
        ///

        public void CloneContract()
        {
            CloneContractRequest request = new CloneContractRequest();
            request.ContractId = contractId;
            request.IncludeCanceledLines = true;
            CloneContractResponse response = (CloneContractResponse)service.Execute(request);
            //销售合同
            Entity contractEn = response.Entity;
        }

        ///


        /// 续订合同
        ///

        public void RenewContract()
        {
            RenewContractRequest request = new RenewContractRequest();
            request.ContractId = contractId;
            request.IncludeCanceledLines = true;
            //合同的状态
            request.Status = 0;
            RenewContractResponse response = (RenewContractResponse)service.Execute(request);
            //销售合同
            Entity contractEn = response.Entity;
        }

        ///


        /// 与其他安全主体(用户或团队)共享销售合同
        ///

        /// 访问权限
        /// 用户或团队引用
        public void GrantAccessRequest(AccessRights accessMask, EntityReference principal)
        {
            GrantAccessRequest request = new GrantAccessRequest();
            request.Target = new EntityReference() { LogicalName = entityName, Id = contractId };
            request.PrincipalAccess = new PrincipalAccess() { AccessMask = accessMask, Principal = principal };
            GrantAccessResponse response = (GrantAccessResponse)service.Execute(request);
        }

        ///


        /// 删除指定安全主体(用户或团队)对销售合同的所有访问权限
        ///

        /// 用户或团队引用
        public void RevokeAccess(EntityReference revokee)
        {
            RevokeAccessRequest request = new RevokeAccessRequest();
            request.Target = new EntityReference() { LogicalName = entityName,Id = contractId };
            request.Revokee = revokee;
            RevokeAccessResponse response = (RevokeAccessResponse)service.Execute(request);
        }

        ///


        /// 删除销售合同
        ///

        public void Delete()
        {
            service.Delete(entityName, contractId);
        }
    }

你可能感兴趣的:(CRM)