CRM系统Salesforce 个案trigger

时间:2021-11-06 07:17:30
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<Case> caselist =trigger.new;
List<String> phoneids = new List<String>();
for(Case c1:caselist){
if(c1.contactphone__c!=null&&c1.contactname__c!=null&&c1.AccountId!=null){
phoneids.add(c1.contactphone__c);
}
}
List<Contact> 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<consignment_detail__c> 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<installationdetail__c> iDetails = new List<installationdetail__c>();//派工明细
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<RecordType> records =[SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType='Case'];
Map<String,String> recordName_Id = new Map<String,String>();
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<caseassignmentrule__c> asign = new List<caseassignmentrule__c>();
//第一层 客户名匹配
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<consignment_detail__c> 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<ProcessInstance> pInstance =[SELECT TargetObjectId, Id FROM ProcessInstance WHERE TargetObjectId=:c.Id ORDER BY CreatedDate DESC];
system.debug('pInstance'+pInstance);
if(pInstance.size()>0){
List<ProcessInstanceStep> 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<customerservicelist__c> 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<installationdetail__c> iDetail =[SELECT id,Name FROM installationdetail__c WHERE casenumber__c=:c.Id];
List<String> iids = new List<String>();
for(installationdetail__c i:iDetail){
iids.add(i.Id);
}
List<checkinglist__c> checks=[SELECT id,Name,installationdetail__c FROM checkinglist__c WHERE installationdetail__c IN :iids];

//是否每个派工明细下都有设备安装验收单
Set<String> iDetailNames = new Set<String>();
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<String> cids = new List<String>();
for(checkinglist__c c1:checks){
cids.add(c1.Id);
}
system.debug('cids:'+cids);
if(cids.size()>0){
List<Attachment> att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:cids];
List<ContentDocumentLink> documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:cids];
Set<String> NotDocNames =new Set<String>();
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<customerservicelist__c> cusSer=[SELECT id,Name FROM customerservicelist__c WHERE servicearr__c=:c.Id];
if(cusSer.size()==0){
c.addError('客户服务单为空,不允许提交审批!');
}
List<String> csids = new List<String>();
for(customerservicelist__c cs:cusSer){
csids.add(cs.Id);
}
System.debug('===================1===================');
List<Attachment> att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:csids];
System.debug('==================2===================='+csids);
if(csids.size()>0){
List<ContentDocumentLink> documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:csids];
System.debug('==================3====================');
Set<String> NotDocNames =new Set<String>();
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<CaseShare> 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<CaseShare> 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<Group> g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服话务组' limit 1];
List<String> Uids = new List<String>();
if(g.size()>0){
List<GroupMember> members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
for(GroupMember m:members){
Uids.add(m.UserOrGroupId);
}
}
List<User> users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
for(User u2:users){
SMS.doFutureSend(u2.Phone,msg);
}
}else{
List<Group> g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服话务组长' limit 1];
List<String> Uids = new List<String>();
if(g.size()>0){
List<GroupMember> members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
for(GroupMember m:members){
Uids.add(m.UserOrGroupId);
}
}
List<User> users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
for(User u2:users){
SMS.doFutureSend(u2.Phone,msg);
}
}
}
}