首页 » 技术分享 » CRM系统之Salesforce 销售订单trigger

CRM系统之Salesforce 销售订单trigger

 

trigger salesorderTrigger on salesorder__c (before insert,before update,after insert) {
    salesorder__c salesorder = Trigger.new[0];
    Account a = new Account();
    if(Trigger.isInsert&&Trigger.isBefore){ 

        if(salesorder.customer__c!=null){
            a  = [SELECT Id,technologicalrequirements1__c,Customer_status__c, Industry, Material_type2__c, Name, Phone__c, deliveryaddress__c FROM Account WHERE Id = : salesorder.customer__c];
            List<Contact> contactList = [select id,Name,FirstName,LastName,MobilePhone,Phone_number2__c from Contact where AccountId=:salesorder.customer__c limit 1];
            if('未启用'.equals(a.Customer_status__c)&&contactList.size()<=0){
                salesorder.addError('客户没有相关联系人,状态为未启用,请及时维护和提交审批!');
            }else{ 
                if('未启用'.equals(a.Customer_status__c)){
                    salesorder.addError('客户状态为未启用,请及时提交客户进行审批!');
                }else if('待审核'.equals(a.Customer_status__c)){
                     salesorder.addError('客户状态为待审核,请等待审批完成后再新建该客户的订单!');

                }else{
                    if(contactList.size()>0){
                        if(String.isNotBlank(contactList[0].FirstName)){
                            salesorder.consignee__c = contactList[0].FirstName+contactList[0].LastName;
                        }else{
                            salesorder.consignee__c = contactList[0].LastName;
                        }

                        salesorder.consigneephone__c = contactList[0].Phone_number2__c;
                    }else{
                        salesorder.addError('客户没有相关联系人,请及时维护!');
                    }                    
                }   
            }
        }


        //salesorder.technicsrequiment__c = a.technologicalrequirements1__c;
        salesorder.industrytypes__c = a.Industry;
        salesorder.materialtype__c = a.Material_type2__c;
        if(salesorder.delivery__c=='送货上门'){
            salesorder.deliveryaddress__c = a.deliveryaddress__c;
        }else if(salesorder.delivery__c=='自提货'){
            salesorder.deliveryaddress__c = Label.CompanyAddress;
        }   
    }

    if(Trigger.isUpdate){
        if(salesorder.statue__c=='1'&&trigger.OldMap.get(salesorder.Id).statue__c=='0'){
            a  = [SELECT Id,technologicalrequirements1__c,Customer_status__c, Industry, Material_type2__c, Name, Phone__c, deliveryaddress__c FROM Account WHERE Id = : salesorder.customer__c];
            if('未启用'.equals(a.Customer_status__c)){
                    salesorder.addError('客户状态为未启用,请及时提交客户进行审批!');
            }else if('待审核'.equals(a.Customer_status__c)){
                     salesorder.addError('客户状态为待审核,请等待审批完成后再新建该客户的订单!');

            }

            List<Attachment> attachmentList = [select id from Attachment where ParentId=:salesorder.id];
            List<ContentVersion> contentVersionList = [SELECT Id FROM ContentVersion where FirstPublishLocationId=:salesorder.id];
            List<ContentDocumentLink> cdLList = [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId =:salesorder.id];
            if(attachmentList.size()<=0&&contentVersionList.size()<=0&&cdLList.size()<=0){
                salesorder.addError('销售订单提交审批时,没有上传合同、三证等信息附件,不允许提交审批!');
            }
        }
    }

    //跨区域提醒
    if(trigger.isAfter&&salesorder.statue__c=='1'){
        List<CustomObject5__c> userconfig=[SELECT id,office__c,branch__c,Business_area__c,productline__c,User__c,User__r.Name,position__c FROM CustomObject5__c WHERE User__c=:salesorder.OwnerId AND productline__c!=null];
        if(userconfig.size()>0&&salesorder.deliveryplace__c!=null){   
            Set<Id> UIds = new Set<Id>();
            String msg ='业务员 '+ userconfig[0].User__r.Name + ' 存在跨区域销售,请悉知!';
            Set<String> uline = new Set<String>();
            Boolean isfp=true;
            String salearea = salesorder.salearea__c.substring(3,7);//销售区域

            Region__c reg = [SELECT Name FROM Region__c WHERE id=:salesorder.deliveryplace__c];
            for(CustomObject5__c c5:userconfig){
                if(!c5.productline__c.contains(salearea)){//如果可售产品线不包含销售区域,则跨产品线销售(不需要发chatter)
                    isfp = false;
                }
                if(c5.position__c=='分公司经理'||c5.position__c=='内贸总监'||c5.position__c=='大客户部副经理'){
                    isfp = false;
                }else{
                    //其他职位的人跨区域则发chatter至上级
                    Set<String> bareas = new Set<String>(c5.Business_area__c.split(';'));
                    for(String s:bareas){
                        if(reg.Name.contains(s)){
                            isfp = false;//只要地址中包含业务员的一个可售区域则不是跨区域(不发chatter)
                        } 
                    }
                }         
            }
            if(isfp){ 
                List<CustomObject5__c> upper = new List<CustomObject5__c>();
                if(userconfig[0].branch__c=='精密/智能设备分公司'){
                   upper=[SELECT id,branch__c,User__c,User__r.Name,position__c FROM CustomObject5__c WHERE branch__c=:userconfig[0].branch__c AND position__c='分公司经理' AND productline__c=:salearea]; 
                }else{
                   upper=[SELECT id,branch__c,User__c,User__r.Name,position__c FROM CustomObject5__c WHERE branch__c=:userconfig[0].branch__c AND position__c='分公司经理']; 
                } 
                if(upper.size()>0){
                    ChatterSender.send(upper[0].User__c,msg,salesorder.Id);
                }    
            }    
        } 
    }   
}

转载自原文链接, 如需删除请联系管理员。

原文链接:CRM系统之Salesforce 销售订单trigger,转载请注明来源!

0