using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 銷售合同 /// </summary> public class ContractHelper { public static readonly string entityName = "contract"; public Guid contractId = Guid.Empty; public IOrganizationService service; /// <summary> /// 建立銷售合同 /// </summary> public void Create() { Entity en = new Entity() { LogicalName = entityName }; en["title"] = "銷售合同測試"; contractId = service.Create(en); } /// <summary> /// 將銷售合同分派給其餘用戶或團隊 /// </summary> /// <param name="assignee">用戶或團隊引用</param> 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); } /// <summary> /// 取消合同 /// </summary> /// <param name="status">取消的狀態</param> 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); } /// <summary> /// 複製合同 /// </summary> public void CloneContract() { CloneContractRequest request = new CloneContractRequest(); request.ContractId = contractId; request.IncludeCanceledLines = true; CloneContractResponse response = (CloneContractResponse)service.Execute(request); //銷售合同 Entity contractEn = response.Entity; } /// <summary> /// 續訂合同 /// </summary> 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; } /// <summary> /// 與其餘安全主體(用戶或團隊)共享銷售合同 /// </summary> /// <param name="accessMask">訪問權限</param> /// <param name="principal">用戶或團隊引用</param> 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); } /// <summary> /// 刪除指定安全主體(用戶或團隊)對銷售合同的全部訪問權限 /// </summary> /// <param name="revokee">用戶或團隊引用</param> 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); } /// <summary> /// /// 刪除銷售合同 /// /// </summary> public void Delete() { service.Delete(entityName, contractId); } }