最近的一个项目中基于MsCrm二次开发的操作总结

来源:互联网 发布:多益网络邮箱 编辑:程序博客网 时间:2024/05/21 10:24

 1:查询数据分页显示

 public account[] GetAccountsByCondition(object[] nStatus, string sName, string sNumber, string sStartDate, string sEndDate, int pageNumber, int pageCount, ref int total, AccountCategoryType nAcccountType)
        
{
            ColumnSet cols 
= new ColumnSet();
            cols.Attributes 
= new string[] "accountid""accountnumber""primarycontactid""mls_suppliertierid""mls_agencytierid","name""fax""emailaddress1""telephone1""statuscode""statecode""mls_suppliercategoryid" };

            
// Account类型
            ConditionExpression conditionType = new ConditionExpression();
            conditionType.AttributeName 
= "accountcategorycode";
            
if (nAcccountType == AccountCategoryType.Supplier)
            
{
                conditionType.Values 
= new object[] { (int)AccountCategoryType.Supplier, (int)AccountCategoryType.AgencyAndSupplier };
            }

            
else if (nAcccountType == AccountCategoryType.Agency)
            
{
                conditionType.Values 
= new object[] { (int)AccountCategoryType.Agency, (int)AccountCategoryType.AgencyAndSupplier };
            }

            
else
            
{
                conditionType.Values 
= new object[] {(int)AccountCategoryType.AgencyAndSupplier };
            }


            conditionType.Operator 
= ConditionOperator.In;

            
// 状态
            ConditionExpression conditionStatus = new ConditionExpression();
            conditionStatus.AttributeName 
= "statuscode";
            conditionStatus.Values 
= nStatus;
            conditionStatus.Operator 
= ConditionOperator.In;

            
// Active
            ConditionExpression conditionState = new ConditionExpression();
            conditionState.AttributeName 
= "statecode";
            conditionState.Values 
= new object[] { AccountState.Active.ToString() };
            conditionState.Operator 
= ConditionOperator.Equal;

            
// 构建开始时间条件
            ConditionExpression conditionStartDate = new ConditionExpression();
            conditionStartDate.AttributeName 
= "createdon";
            conditionStartDate.Values 
= new object[] { sStartDate };
            conditionStartDate.Operator 
= ConditionOperator.OnOrAfter;

            
// 构建结束时间条件
            ConditionExpression conditionEndDate = new ConditionExpression();
            conditionEndDate.AttributeName 
= "createdon";
            conditionEndDate.Values 
= new object[] { sEndDate };
            conditionEndDate.Operator 
= ConditionOperator.OnOrBefore;

            
// 构建开始和结束时间条件
            ConditionExpression conditionDate = new ConditionExpression();
            conditionDate.AttributeName 
= "createdon";
            conditionDate.Values 
= new object[] { sStartDate, sEndDate };
            conditionDate.Operator 
= ConditionOperator.Between;

            
// 名称关键字
            ConditionExpression conditionNameLike = new ConditionExpression();
            conditionNameLike.AttributeName 
= "name";
            conditionNameLike.Values 
= new object[] "%" + sName + "%" };
            conditionNameLike.Operator 
= ConditionOperator.Like;

            
// 编号模糊查询条件
            ConditionExpression conditionNumberLike = new ConditionExpression();
            conditionNumberLike.AttributeName 
= "accountnumber";
            conditionNumberLike.Values 
= new object[] "%" + sNumber + "%" };
            conditionNumberLike.Operator 
= ConditionOperator.Like;

            FilterExpression filter 
= new FilterExpression();


            
if (sStartDate.Length > 0 && sEndDate.Length > 0 && sName.Length > 0 && sNumber.Length > 0)
            
{
                
if (sEndDate != sStartDate)
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionDate, conditionNameLike, conditionNumberLike };
                }

                
else
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate, conditionNameLike, conditionNumberLike };
                }

            }

            
if (sStartDate.Length > 0 && sEndDate.Length > 0 && sName.Length == 0 && sNumber.Length == 0)
            
{
                
if (sStartDate != sEndDate)
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionDate };
                }

                
else
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate };
                }

            }

            
if (sStartDate.Length > 0 && sEndDate.Length > 0 && sName.Length > 0 && sNumber.Length == 0)
            
{
                
if (sEndDate != sStartDate)
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionDate, conditionNameLike };
                }

                
else
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate, conditionNameLike };
                }

            }

            
if (sStartDate.Length > 0 && sEndDate.Length > 0 && sName.Length == 0 && sNumber.Length > 0)
            
{
                
if (sEndDate != sStartDate)
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionDate, conditionNumberLike };
                }

                
else
                
{
                    filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate, conditionNumberLike };
                }

            }

            
if (sStartDate.Length > 0 && sEndDate.Length == 0 && sName.Length == 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate, conditionNumberLike };
            }


            
if (sStartDate.Length > 0 && sEndDate.Length == 0 && sName.Length > 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate, conditionNameLike, conditionNumberLike };
            }

            
if (sStartDate.Length > 0 && sEndDate.Length == 0 && sName.Length > 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate, conditionNameLike };
            }

            
if (sStartDate.Length > 0 && sEndDate.Length == 0 && sName.Length == 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionStartDate };
            }


            
/*========================================================================================*/
            
if (sStartDate.Length == 0 && sEndDate.Length == 0 && sName.Length == 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length == 0 && sName.Length > 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionNameLike, conditionNumberLike };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length == 0 && sName.Length > 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionNameLike };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length == 0 && sName.Length == 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionNumberLike };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length > 0 && sName.Length > 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate, conditionNameLike, conditionNumberLike };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length > 0 && sName.Length == 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length > 0 && sName.Length == 0 && sNumber.Length > 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate, conditionNumberLike };
            }

            
if (sStartDate.Length == 0 && sEndDate.Length > 0 && sName.Length > 0 && sNumber.Length == 0)
            
{
                filter.Conditions 
= new ConditionExpression[] { conditionType, conditionState, conditionStatus, conditionEndDate, conditionNameLike };
            }



            filter.FilterOperator 
= LogicalOperator.And;

            
// 分页
            PagingInfo page = new PagingInfo();
            page.Count 
= pageCount;
            page.PageNumber 
= pageNumber;

            
// 按时间进行排序
            OrderExpression orderByDate = new OrderExpression();
            orderByDate.AttributeName 
= "createdon";
            orderByDate.OrderType 
= OrderType.Descending;

            
// 查询记录总数
            QueryExpression queryCount = new QueryExpression();
            queryCount.EntityName 
= EntityName.account.ToString();
            queryCount.ColumnSet 
= cols;
            queryCount.Criteria 
= filter;

            
if (crmServiceManager == null)
            
{
                crmServiceManager 
= CrmServiceManager.GetCrmServiceManager();
            }

            BusinessEntityCollection entitys 
= crmServiceManager.RetrieveMultiple(queryCount);
            
if (entitys != null && entitys.BusinessEntities.Length > 0)
            
{
                
// 总记录数
                total = entitys.BusinessEntities.Length;
            }

            
else
            
{
                
return null;
            }


            
// 构建数据查询
            QueryExpression query = new QueryExpression();
            query.EntityName 
= EntityName.account.ToString();
            query.ColumnSet 
= cols;
            query.Criteria 
= filter;
            query.PageInfo 
= page;
            query.Orders 
= new OrderExpression[] { orderByDate };

            
if (crmServiceManager == null)
            
{
                crmServiceManager 
= CrmServiceManager.GetCrmServiceManager();
            }

            BusinessEntityCollection entities 
= crmServiceManager.RetrieveMultiple(query);
            
if (entities != null && entities.BusinessEntities.Length > 0)
            
{
                account[] accounts 
= new account[entities.BusinessEntities.Length];
                Array.Copy(entities.BusinessEntities, accounts, entities.BusinessEntities.Length);

                
return accounts;
            }


            
return null;
        }

 

总体思路:先查询出满足条件的记录总数 然后再设置分页显示

2:如何捕捉错误
MSCRM平台仅仅提供一个错误异常SoapException,其消息提示为:"Server was unable to process request.",所以我们需要重新定义其错误异常处理。
catch (System.Web.Services.Protocols.SoapException ex)  
{   
    TextWriter log 
= TextWriter.Synchronized(File.AppendText(@"C:CRM_Debugerror.txt"));  
    log.WriteLine(
"SoapException: " + DateTime.Now.ToString());  
    log.WriteLine(
"Error Message: " + ex.Detail.InnerText);  
    XmlDocument error 
= new XmlDocument();  
    error.LoadXml(ex.Detail.InnerXml);  
    log.WriteLine(
"Error Code: " + error.SelectSingleNode("/error/code").InnerText);  
    log.WriteLine(
"Error Description: " + error.SelectSingleNode("/error/description").InnerText);  
    log.WriteLine(
"Error Type: " + error.SelectSingleNode("/error/type").InnerText);  
    log.WriteLine();  
    log.Close();  
}
  
catch (SqlException ex)   
{  
    
//  
}
  
catch (Exception ex)   
{  
    
//  
}
  

The Detail property of the SoapException will have more information about the exact error that occurred in the platform. The Detail property type is an XmlNode, of which the InnerXml property value has the following format:

<error>   <description>Error description goes here.</description>   <number>0xABCD1234</number>   <type>Platform</type></error>




3:数据类型转化
 public static object GetNetTypeFromCrmType(object value)
        
{
            
if (value == nullreturn null;

            
if (value is CrmBoolean)
            
{
                
if (((CrmBoolean)value).IsNull) return null;
                
return ((CrmBoolean)value).Value;
            }

            
if (value is CrmDateTime)
            
{
                
return NetTypes.CrmDateTimeToDateTime((CrmDateTime)value);
            }

            
if (value is CrmDecimal)
            
{
                
if (((CrmDecimal)value).IsNull) return null;
                
return ((CrmDecimal)value).Value;
            }

            
if (value is CrmFloat)
            
{
                
if (((CrmFloat)value).IsNull) return null;
                
return ((CrmFloat)value).Value;
            }

            
if (value is CrmMoney)
            
{
                
if (((CrmMoney)value).IsNull) return null;
                
return ((CrmMoney)value).Value;
            }

            
if (value is CrmNumber)
            
{
                
if (((CrmNumber)value).IsNull) return null;
                
return ((CrmNumber)value).Value;
            }

            
if (value is Status)
            
{
                
if (((Status)value).IsNull) return null;
                
return ((Status)value).Value;
            }

            
if (value is Key)
            
{
                
return ((Key)value).Value;
            }

            
if (value is Lookup)
            
{
                
if (((Lookup)value).IsNull) return null;
                
return ((Lookup)value).Value;
            }

            
if (value is Customer)
            
{
                
if (((Customer)value).IsNull) return null;
                
return ((Customer)value).Value;
            }

            
if (value is Owner)
            
{
                
if (((Owner)value).IsNull) return null;
                
return ((Owner)value).Value;
            }

            
if (value is Picklist)
            
{
                
if (((Picklist)value).IsNull) return null;
                
return ((Picklist)value).Value;
            }

            
if (value is CrmReference)
            
{
                
if (((CrmReference)value).IsNull) return null;
                
return ((CrmReference)value).Value;
            }

            
return value;
        }

上面的那个方法 效率太低。如果知道了数据类型 就直接省去了判定 直接转化
  /// <summary>
        
/// 合同转化类(从.Net向MsCrm转化)
        
/// </summary>
        
/// <param name="info"></param>
        
/// <returns></returns>

        public static contract GetContractInstance(ContractInfo info)
        
{
            contract value 
= new contract();
       
            
if (info.AllotmentType != 0)
            
{
                value.allotmenttypecode 
= info.AllotmentType;
            }


            
if (info.BillingCustomerID != System.Guid.Empty && info.BillingCustomerID!=null)
            
{
               value.billingcustomerid 
= new Customer();
               value.billingcustomerid.type 
= EntityName.account.ToString();
               value.billingcustomerid.Value 
= new Guid(info.BillingCustomerID.ToString());
            }


            
if (info.ContractID != System.Guid.Empty && info.ContractID!=null)
            
{
                value.contractid 
= info.ContractID;
            }

            
if (info.ActiveDate != "")
            
{
                value.activeon 
= CrmTypes.CreateCrmDateTime(info.ActiveDate);
            }

            
if (info.ExpiresDate != "")
            
{
                value.expireson 
= CrmTypes.CreateCrmDateTime(info.ExpiresDate);
            }

            
if (info.BillingStartDate != "")
            
{
                value.billingstarton 
= CrmTypes.CreateCrmDateTime(info.BillingStartDate);
            }

            
if (info.BillingEndDate != "")
            
{
                value.billingendon 
= CrmTypes.CreateCrmDateTime(info.BillingEndDate);
            }

            
if (info.BillingFrequency != 0)
            
{
                value.billingfrequencycode 
= info.BillingFrequency;
            }

            
if (info.BilltoAddress!=null && info.BilltoAddress != System.Guid.Empty)
            
{
                value.billtoaddress 
= info.BilltoAddress;
            }

            value.contractlanguage 
= info.ContractLanguage;

            
if (info.ContractServiceLevel != 0)
            
{
                value.contractservicelevelcode 
= info.ContractServiceLevel;
            }

            
if (info.ContractTemplateID != System.Guid.Empty && info.ContractTemplateID!=null)
            
{
                value.contracttemplateid 
= info.ContractTemplateID;
            }

            
if ((int)info.ContractKind != 0)
            
{
                value.mls_contracttype 
= (int)info.ContractKind;
            }


            
if (info.CustomerID != System.Guid.Empty && info.CustomerID!=null)
            
{
                value.customerid 
= new Customer();
                value.customerid.type 
= EntityName.account.ToString();
                value.customerid.Value 
= new Guid(info.CustomerID.ToString());
            }

           
            value.mls_cmmnamt 
= info.CommissionAmount;
            
// 佣金抽取基于方式
            if ((int)info.CommissionBasedOnType != 0)
            
{
                value.mls_cmmnbasedon 
= (int)info.CommissionBasedOnType;
            }


            value.mls_cmmnextprog 
= info.ExternalProgram;
            value.mls_cmmnmacro 
= info.MacroExpression;
            value.mls_cmmnmax 
= info.CommissionMax;
            
// 佣金提取方法类型
            if ((int)info.CommissionMethodType != 0)
            
{
                value.mls_cmmnmethod 
= (int)info.CommissionMethodType;
            }

            value.mls_cmmnmin 
= info.CommissionMin;
            value.mls_cmmnpct 
= info.CommissionPercent;

            
if (info.ServiceAddress!=null && info.ServiceAddress != System.Guid.Empty)
            
{
                value.serviceaddress 
= info.ServiceAddress;
            }


            
if (info.OriginatingContract != null && info.OriginatingContract != System.Guid.Empty)
            
{
                value.originatingcontract 
= info.OriginatingContract;
            }

            value.title 
= info.Title;
            value.usediscountaspercentage 
= info.UseDiscountAsPercentage;
            
if ((int)info.Status != 0)
            
{
                value.statuscode 
= (int)info.Status;
            }


            
return value;
        }


        
/// <summary>
        
/// 合同转化类(从MsCrm向.Net转化)
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>

        public static ContractInfo GetContractInfoInstance(contract value)
        
{
            ContractInfo info 
= new ContractInfo();

            
if (value.contractid != null)
            
{
                info.ContractID 
= value.contractid.GetValue();
            }


            info.Title 
= value.title;
            
if (value.allotmenttypecode != null)
            
{
                info.AllotmentType 
= value.allotmenttypecode.GetValue();
            }

            info.ContractNumber 
= value.contractnumber;
            info.ContractLanguage 
= value.contractlanguage;
            
if (value.contracttemplateid != null)
            
{
                info.ContractTemplateID 
= value.contracttemplateid.GetValue();
            }

            
if (value.customerid != null)
            
{
                info.CustomerID 
= value.customerid.GetValue();
            }

            info.ContractLanguage 
= value.contractlanguage;
            
if (value.contractservicelevelcode != null)
            
{
                info.ContractServiceLevel 
= value.contractservicelevelcode.GetValue();
            }

            info.ContractTemplateAbbreviation 
= value.contracttemplateabbreviation;
            
if (value.contracttemplateid != null)
            
{
                info.ContractTemplateID 
= value.contracttemplateid.GetValue();
            }

            
if (value.mls_contracttype != null)
            
{
                info.ContractKind 
= (ContractType)value.mls_contracttype.GetValue();
            }

            
if ((Object)value.netprice != null)
            
{
                info.NetPrice 
= value.netprice.GetValue();
            }

            
if ((Object)value.totaldiscount != null)
            
{
                info.TotalDiscount 
= value.totaldiscount.GetValue();
            }

            
if ((Object)value.totalprice != null)
            
{
                info.TotalPrice 
= value.totalprice.GetValue();
            }

            
if (value.usediscountaspercentage != null)
            
{
                info.UseDiscountAsPercentage 
= value.usediscountaspercentage.GetValue();
            }

            
if (value.billingcustomerid != null)
            
{
                info.BillingCustomerID 
= value.billingcustomerid.GetValue();
            }

            
if (value.billingfrequencycode != null)
            
{
                info.BillingFrequency 
= value.billingfrequencycode.GetValue();
            }

            
if (value.billingendon != null)
            
{
                info.BillingEndDate 
= value.billingendon.Value;
            }

            
if (value.billingstarton != null)
            
{
                info.BillingStartDate 
= value.billingstarton.Value;
            }

            
if (value.billtoaddress != null)
            
{
                info.BilltoAddress 
= value.billtoaddress.GetValue();
            }

            
// 原始合同
            if (value.originatingcontract != null)
            
{
                info.OriginatingContract 
= value.originatingcontract.GetValue();
            }

            
if (value.serviceaddress!=null)
            
{
                info.ServiceAddress 
= value.serviceaddress.GetValue();
            }

            
// 合同状态
            if (value.statuscode != null)
            
{
                info.Status 
= (ContractStatus)value.statuscode.GetValue();
            }

            
if (value.createdby != null)
            
{
                info.CreatedBy 
= value.createdby.GetValue();
            }

            
if (value.createdon != null)
            
{
                info.CreatedDate 
= value.createdon.Value;
            }

            
if (value.activeon != null)
            
{
                info.ActiveDate 
= value.activeon.Value;
            }

            
if (value.modifiedby != null)
            
{
                info.ModifiedBy 
= value.modifiedby.GetValue();
            }

            
if (value.modifiedon != null)
            
{
                info.ModifiedDate 
= value.modifiedon.Value;
            }

            
if (value.cancelon != null)
            
{
                info.CancelDate 
= value.cancelon.Value;
            }

            
if (value.duration != null)
            
{
                info.Duration 
= value.duration.GetValue();
            }

            info.EffectivityCalendar 
= value.effectivitycalendar;
            
if (value.expireson != null)
            
{
                info.ExpiresDate 
= value.expireson.Value;
            }


            
if ((Object)value.mls_cmmnamt != null)
            
{
                info.CommissionAmount 
= value.mls_cmmnamt.GetValue();
            }

            
if ((Object)value.mls_cmmnmin != null)
            
{
                info.CommissionMin 
= value.mls_cmmnmin.GetValue();
            }

            
if ((Object)value.mls_cmmnmax != null)
            
{
                info.CommissionMax 
= value.mls_cmmnmax.GetValue();
            }

            
if (value.mls_cmmnbasedon != null)
            
{
                info.CommissionBasedOnType 
= (CommisionBaseOn)value.mls_cmmnbasedon.GetValue();
            }

            info.ExternalProgram 
= value.mls_cmmnextprog;
            info.MacroExpression 
= value.mls_cmmnmacro;
            
// 佣金提取方式类型
            if (value.mls_cmmnmethod != null)
            
{
                info.CommissionMethodType 
= (CommisionMethod)value.mls_cmmnmethod.GetValue();
            }

            
if (value.mls_cmmnpct != null)
            
{
                info.CommissionPercent 
= value.mls_cmmnpct.GetValue();
            }
 
            
return info;
        }
原创粉丝点击