歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

CRM系統Salesforce 個案trigger

trigger CaseTrigger on Case (after insert,before insert,before update,before delete) {
    Case c=new Case();
    if(trigger.isDelete){
        c = trigger.old[0];
    }else{
        c = trigger.new[0];
    }

    //導數據專用
    if(trigger.isInsert&&trigger.isBefore){
        List caselist =trigger.new;
        List phoneids = new List();
        for(Case c1:caselist){
            if(c1.contactphone__c!=null&&c1.contactname__c!=null&&c1.AccountId!=null){
                phoneids.add(c1.contactphone__c);
            }
        }
        List cons=[SELECT id,Phone_number2__c FROM Contact WHERE Phone_number2__c IN :phoneids];
        for(Case c1:caselist){
            Boolean isExistCon = false;
            for(Contact c2:cons){
                if(c2.Phone_number2__c==c1.contactphone__c){
                    c1.ContactId = c2.Id;
                    isExistCon = true;
                }
            }
            if(!isExistCon){
                Contact c3 = new Contact();
                c3.LastName = c1.contactname__c;
                c3.AccountId = c1.AccountId;
                c3.Phone_number2__c = c1.contactphone__c;
                insert c3;
                c1.ContactId = c3.Id;
            }
        }
    }

    if(Trigger.isAfter&&Trigger.isInsert){
        if(c.shiporder__c!=null){
            consignment__c consignment =[SELECT id,Dispatchingorder__c FROM consignment__c WHERE id=:c.shiporder__c];
            consignment.Dispatchingorder__c = true;
            update consignment;
        }

    }

    if(c.shiporder__c!=null&&trigger.isInsert&&trigger.isAfter){
        //Case關聯的發貨單(shiporder__c)下面的所有發貨單明細
        List cDetails =[SELECT id,product__c,machineID__c,number__c,materiel_number__c,product_name__c,
                                               ERPproductmodel__c,remark__c,partSpec__c FROM consignment_detail__c WHERE consignment__c =:c.shiporder__c];
        List iDetails = new List();//派工明細
        for(consignment_detail__c cd:cDetails){
            installationdetail__c temp = new installationdetail__c();
            temp.consignment_detail__c = cd.Id;
            temp.machinecode1__c = cd.machineID__c;
            temp.product__c = cd.product__c;
            temp.casenumber__c = c.Id;
            temp.quantity1__c = cd.number__c;
            temp.materialnumber1__c = cd.materiel_number__c;
            temp.materialname1__c = cd.product_name__c;
            temp.type1__c = cd.ERPproductmodel__c;
            temp.statue1__c = cd.remark__c;
            temp.style1__c = cd.partSpec__c;
            iDetails.add(temp);
        }
        insert iDetails;
    }

    List records =[SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType='Case'];
    Map recordName_Id = new Map();
    for(RecordType r:records){
        recordName_Id.put(r.Name,r.Id);      
    }
    if(trigger.isBefore&&trigger.isInsert){
        if(c.AccountId!=null&&(recordName_Id.get('新機裝機')==c.RecordTypeId||recordName_Id.get('客戶報修')==c.RecordTypeId)){
            List asign = new List();
            //第一層 客戶名匹配
            asign = [SELECT Id,skillman__c FROM caseassignmentrule__c WHERE customername__c=:c.AccountId];
            if(asign.size()>0){
                c.firstowner__c = asign[0].skillman__c;
            }else{
                Boolean step2 = false;
                if(c.shiporder__c!=null){
                    List cDetails =[SELECT id,product__c FROM consignment_detail__c WHERE consignment__c =:c.shiporder__c];
                    //第二層 產品匹配
                    for(consignment_detail__c cd:cDetails){
                        asign =[SELECT Id,skillman__c FROM caseassignmentrule__c WHERE producttype__c=:cd.product__c];
                        if(asign.size()>0){
                            c.firstowner__c = asign[0].skillman__c;
                            step2 = true;
                        }
                    }
                }
                //第二層比對不成功再進行第三層比對
                if(!step2&&c.area__c!=null){
                    Region__c reg =[SELECT Name FROM Region__c WHERE id=:c.area__c];
                    asign =[SELECT Id,skillman__c,area__r.Name FROM caseassignmentrule__c ];
                    if(asign.size()>0){
                        for(caseassignmentrule__c cg:asign){
                            if(reg.Name.Contains(cg.area__r.Name)){
                                c.firstowner__c = asign[0].skillman__c;
                            }
                        }     
                    }
                }
            }
        }
    }

    if(trigger.isBefore&&trigger.isUpdate){
        List pInstance =[SELECT TargetObjectId, Id FROM ProcessInstance WHERE TargetObjectId=:c.Id ORDER BY CreatedDate DESC];
        system.debug('pInstance'+pInstance);
        if(pInstance.size()>0){
            List step=[SELECT Id,Comments,StepNodeId FROM ProcessInstanceStep WHERE ProcessInstanceId=:pInstance[0].Id AND StepStatus!='Started' AND Comments!=''];
            system.debug('step'+step);
            if(step.size()>0){
                for(ProcessInstanceStep ps:step){
                    ProcessInstanceNode pid=[SELECT Id, NodeStatus, ProcessNodeName FROM ProcessInstanceNode WHERE ProcessNodeId=:ps.StepNodeId limit 1];
                    if(pid.ProcessNodeName=='投訴內容核查'){
                        c.Complaintsverification__c = ps.Comments;
                    }else if(pid.ProcessNodeName=='責任部門核實'){
                        c.responsibilitydepartment__c = ps.Comments;
                    }else if(pid.ProcessNodeName=='客服管理中心處理意見及糾正預防措施'){
                        c.Customermeasures__c = ps.Comments;
                    }else if(pid.ProcessNodeName=='公司主管領導意見'){
                        c.Companymanager__c = ps.Comments;
                    }else if(pid.ProcessNodeName=='修改投訴單處理意見及預防糾正措施'){
                        c.Customermeasures__c = ps.Comments;
                    }
                }

            }
        }

        //當個案中的狀態更改為已結案,結案時間取當前系統時間
        if(trigger.old[0].Status!='已結案'&&c.Status=='已結案'){
            //c.Specialcase__c = DateTime.now();
            c.Maintenance__c = true;
            system.debug('caseTrigger-Maintenance__c:'+c.Maintenance__c);
            //當個案狀態顯示為已結案,則把所有與個案關聯的服務單狀態變更為 “已審核”
            List cusSers =[SELECT id,state1__c FROM customerservicelist__c WHERE servicearr__c=:c.Id];
            for(customerservicelist__c cu:cusSers){
                cu.state1__c ='已審核';
            }
            update cusSers;
        }
        //第一次分派時間後2小時觸發chatter
        if(recordName_Id.get('客戶報修')==c.RecordTypeId){
            if(c.firstTime2Hours__c){
                //如果第二次分派人在2小時內,則提醒第二次分派人
                if(c.secondTime2Hours__c){
                    String msg ='單號 '+c.CaseNumber+' 的派工單距離分配時間已達2小時,請電話聯系客戶';
                    ChatterSender.send(c.secondowner__c,msg,c.Id);
                    c.secondTime2Hours__c = false;
                    c.firstTime2Hours__c = false;
                    User u=[SELECT Phone FROM User WHERE id=:c.secondowner__c];
                    SMS.doFutureSend(u.Phone,msg);
                }else{//否則提醒第一次分派人
                    String msg ='單號 '+c.CaseNumber+' 的派工單距離分配時間已達2小時,請電話聯系客戶';
                    ChatterSender.send(c.firstowner__c,msg,c.Id);
                    c.firstTime2Hours__c = false;
                    User u=[SELECT Phone FROM User WHERE id=:c.firstowner__c];
                    SMS.doFutureSend(u.Phone,msg);
                }
            }
        }

        //特殊結案消息提醒:1、當個案原因不為空&狀態=已結案&父級個案為空,則每個24小時消息提醒被分派人
        //(當第一次分派人不為空,第二次為空,取第一次诶分派人;當第一次被分派人及第二次被分派人均不為空,則取第二次被分派人)
        if(recordName_Id.get('客戶報修')==c.RecordTypeId&&c.Reason=='等待配件'&&c.Status=='已結案'&&c.ParentId==null&&trigger.old[0].Status!='已結案'){
            String msg ='您的派工單號【'+c.CaseNumber+'】已進行特殊結案,請選擇特殊結案關聯派工單';
            c.SpecialcaseTime__c = DateTime.now();
            if(c.firstowner__c!=null&&c.secondowner__c!=null){
                ChatterSender.send(c.secondowner__c,msg,c.Id);
            }else if(c.firstowner__c!=null){
                ChatterSender.send(c.firstowner__c,msg,c.Id);
            }

        }

        //裝機審批邏輯:
        //派工單提交審批時,判斷派工單中的車旅費字段和派工單關聯的設備安裝驗收單,車旅費字段必須有值,
        //且派工單關聯的所有設備安裝驗收單都有附件,才能提交成功,如果檢測有一個條件不滿足,則按照檢測出的內容提示;
        //(如果是車旅費為空,提示車旅費為空,不允許提交;如果是設備安裝驗收單的附件沒有上傳,則提示設備安裝驗收單附件為空,
        //不允許推薦,並且給出相應的設備安裝驗收單單號)

        if(c.Status=='審批中'&&trigger.old[0].Status=='已接受(技術員)'&&recordName_Id.get('新機裝機')==c.RecordTypeId){
            if(c.costprice__c==null){
                c.addError('車旅費為空,不允許提交!');
            }else{
                List iDetail =[SELECT id,Name FROM installationdetail__c WHERE casenumber__c=:c.Id];
                List iids = new List();
                for(installationdetail__c i:iDetail){
                    iids.add(i.Id);
                }
                List checks=[SELECT id,Name,installationdetail__c FROM checkinglist__c WHERE installationdetail__c IN :iids];

                //是否每個派工明細下都有設備安裝驗收單
                Set iDetailNames = new Set();
                for(installationdetail__c i:iDetail){
                    Boolean haveCheck = false;
                    for(checkinglist__c c1:checks){
                        if(c1.installationdetail__c==i.Id){
                            haveCheck = true;
                        }
                    }
                    if(!haveCheck){ 
                        iDetailNames.add(i.Name);
                    }
                }
                if(iDetailNames.size()>0){
                    c.addError(iDetailNames+'的設備安裝驗收單為空,不允許提交審批!');
                }

                List cids = new List();
                for(checkinglist__c c1:checks){
                    cids.add(c1.Id);
                }
                system.debug('cids:'+cids);
                if(cids.size()>0){
                    List att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:cids];
                    List documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:cids];
                    Set NotDocNames =new Set();
                    for(checkinglist__c c2:checks){
                        Boolean thisCheckHasAtt = false;
                        Integer thisNum =0;
                        for(Attachment a:att){
                            if(a.ParentId==c2.Id){
                                thisNum ++;
                            }
                        }
                        for(ContentDocumentLink d:documents){
                            if(d.LinkedEntityId==c2.Id){
                                thisNum ++;
                            }
                        }
                        if(thisNum>=3){
                            thisCheckHasAtt = true;
                        }
                        if(!thisCheckHasAtt){
                            //c.addError('設備安裝驗收單'+c2.Name+'附件少於三個,不允許提交!');
                            NotDocNames.add(c2.Name);
                        }
                    }
                    if(NotDocNames.size()>0){
                        /*
String names='';
for(String s:NotDocNames){
if(names==''){
names = s;
}else{
names = names +'、'+s;
}
}*/
                        c.addError('設備安裝驗收單'+NotDocNames+'附件少於三個,不允許提交!');
                    }
                }
            }
        }
        //跟裝機審批判斷附件邏輯一樣,判斷的對象不一樣
        //個案提交審批時:記錄類型=客戶報修&狀態=已接收(技術員),點擊提交待審批,監測個案關聯的所有服務單是否已上傳附件,
        //如未上傳附件,則提示對應未上傳附件的服務單
        if(c.Status=='審批中'&&trigger.old[0].Status=='已接受(技術員)'&&recordName_Id.get('客戶報修')==c.RecordTypeId){
            if(c.costprice__c==null){
                c.addError('車旅費為空,不允許提交!');
            }else{
                List cusSer=[SELECT id,Name FROM customerservicelist__c WHERE servicearr__c=:c.Id];
                if(cusSer.size()==0){
                    c.addError('客戶服務單為空,不允許提交審批!');
                }
                List csids = new List();
                for(customerservicelist__c cs:cusSer){
                    csids.add(cs.Id);
                }
                System.debug('===================1===================');
                List att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:csids];
                System.debug('==================2===================='+csids);
                if(csids.size()>0){
                    List documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:csids];
                    System.debug('==================3====================');
                    Set NotDocNames =new Set();
                    for(customerservicelist__c cs2:cusSer){
                        Boolean thisCheckHasAtt = false;
                        for(Attachment a:att){
                            if(a.ParentId==cs2.Id){
                                thisCheckHasAtt = true;
                            }
                        }
                        for(ContentDocumentLink d:documents){
                            if(d.LinkedEntityId==cs2.Id){
                                thisCheckHasAtt = true;     
                            }
                        }
                        if(!thisCheckHasAtt){
                            //c.addError('客戶服務單'+cs2.Name+'附件為空,不允許提交!');
                            NotDocNames.add(cs2.Name);
                        }
                    }
                    if(NotDocNames.size()>0){
                        String names='';
                        for(String s:NotDocNames){
                            if(names==''){
                                names = s;
                            }else{
                                names = names +'、'+s;
                            }
                        }
                        c.addError('客戶服務單'+names+'附件為空,不允許提交!');
                    }
                }
            }
        }
    }

    if(trigger.isDelete){
        if(c.Status!='未派工'&&(recordName_Id.get('新機裝機')==c.RecordTypeId||recordName_Id.get('客戶報修')==c.RecordTypeId)){
            c.addError('未派工狀態下才可以刪除!');
        }
    }

    if(trigger.isBefore&&trigger.isUpdate){
        system.debug('shares:'+1);
        Case c1 = trigger.old[0];
        Case c2 = trigger.new[0];
        if(c1.firstowner__c!=c2.firstowner__c){
            system.debug('shares:'+2);
            List shares=[SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare WHERE CaseId=:c1.Id AND UserOrGroupId=:c1.firstowner__c AND RowCause='Manual'];
            system.debug('shares:'+shares);
            delete shares;
        }
        if(c1.secondowner__c!=c2.secondowner__c){
            List shares=[SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare WHERE CaseId=:c1.Id AND UserOrGroupId=:c1.secondowner__c AND RowCause='Manual'];
            system.debug('shares:'+shares);
            delete shares;
        }
    }

    //短信推送
    if(trigger.isUpdate&&(recordName_Id.get('新機裝機')==c.RecordTypeId||recordName_Id.get('客戶報修')==c.RecordTypeId)){
        //一次派工提醒技術員 
        if(c.firstowner__c!=null&&c.secondowner__c==null&&c.Status=='已派工'&&trigger.old[0].Status!='已派工'){
            String msg='單號:'+c.CaseNumber+'的派工單已分配給您,請按照派工單中的內容執行,謝謝!';
            User u=[SELECT Name,Phone FROM User WHERE id=:c.firstowner__c];
            SMS.doFutureSend(u.Phone, msg);
        }

        //一次派工拒絕提醒話務員 裝機到話務組長/報修到話務組
        if(c.Status=='未派工'&&c.refuse__c=='1'){
            User u=[SELECT Alias FROM User WHERE id=:c.firstowner__c];
            String msg='單號:'+c.CaseNumber+'的派工單已被維修人員'+u.Alias+'拒絕,請重新進行派工,謝謝!';
            sendToServer(msg);     
        }

    //裝機/報修派工2小時未響應提醒話務組   裝機到話務組長/報修到話務組
        if(c.X2HoursSms__c==true&&trigger.old[0].X2HoursSms__c!=true){
            String msg='單號:'+c.CaseNumber+'的派工截止目前已超過2小時一線維修人員還未接收,請話務人員致電維修人員詢問情況,謝謝!';
            sendToServer(msg);
        }

    //派工後預約上門時間後推24小時提醒客服技術員需到達客戶現場
        if(c.X24SendOne__c&&trigger.old[0].X24SendOne__c!=true){
            String msg='單號:'+c.CaseNumber+'的派工單距離客戶預約上門時間即將達24小時,請及時到達客戶現場,謝謝!';
            User u=[SELECT Phone FROM User WHERE id=:c.firstowner__c];
            SMS.doFutureSend(u.Phone,msg);
        }
        if(c.X24SendTwo__c&&trigger.old[0].X24SendTwo__c!=true){
            String msg='單號:'+c.CaseNumber+'的派工單距離客戶預約上門時間即將達24小時,請及時到達客戶現場,謝謝!';
            User u=[SELECT Phone FROM User WHERE id=:c.secondowner__c];
            SMS.doFutureSend(u.Phone,msg);
        }
    }

    public void sendToServer(String msg){
        if(recordName_Id.get('客戶報修')==c.RecordTypeId){
            List g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服話務組' limit 1];
            List Uids = new List();
            if(g.size()>0){
                List members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
                for(GroupMember m:members){
                    Uids.add(m.UserOrGroupId);
                }
            }
            List users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
            for(User u2:users){
                SMS.doFutureSend(u2.Phone,msg);
            }    
        }else{
            List g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服話務組長' limit 1];
            List Uids = new List();
            if(g.size()>0){
                List members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
                for(GroupMember m:members){
                    Uids.add(m.UserOrGroupId);
                }
            }
            List users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
            for(User u2:users){
                SMS.doFutureSend(u2.Phone,msg);
            }
        }
    }
}
Copyright © Linux教程網 All Rights Reserved