支持类

支持类允许您与支持中心常用的记录进行交互, 例如营业时间和案例。

与企业合作 小时

营业时间用于指定营业时间 您的客户支持团队运作的地方,包括多项业务 多个时区的小时数。本示例查找时间 1 从 startTime 开始的工作时间,返回本地的 Datetime 时区。它通过查询 BusinessHours 来获取默认工作时间。 此外,它还调用该方法。

BusinessHoursadd

// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Create Datetime on May 28, 2008 at 1:06:08 AM in local timezone.
Datetime startTime = Datetime.newInstance(2008, 5, 28, 1, 6, 8);

// Find the time it will be one business hour from May 28, 2008, 1:06:08 AM using the
// default business hours.  The returned Datetime will be in the local timezone.
Datetime nextTime = BusinessHours.add(bh.id, startTime, 60 * 60 * 1000L);

本示例查找距离 1 个工作小时的时间 startTime,以 GMT 为单位返回日期时间:

// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Create Datetime on May 28, 2008 at 1:06:08 AM in local timezone.
Datetime startTime = Datetime.newInstance(2008, 5, 28, 1, 6, 8);

// Find the time it will be one business hour from May 28, 2008, 1:06:08 AM using the
// default business hours.  The returned Datetime will be in GMT.
Datetime nextTimeGmt = BusinessHours.addGmt(bh.id, startTime, 60 * 60 * 1000L);

下一个示例查找 startTime 之间的差异 和 nextTime:

// Get the default business hours
BusinessHours bh = [select id from businesshours where IsDefault=true];

// Create Datetime on May 28, 2008 at 1:06:08 AM in local timezone.
Datetime startTime = Datetime.newInstance(2008, 5, 28, 1, 6, 8);

// Create Datetime on May 28, 2008 at 4:06:08 PM in local timezone.
Datetime endTime = Datetime.newInstance(2008, 5, 28, 16, 6, 8);

// Find the number of business hours milliseconds between startTime and endTime as
// defined by the default business hours.  Will return a negative value if endTime is
// before startTime, 0 if equal, positive value otherwise.
Long diff = BusinessHours.diff(bh.id, startTime, endTime);

使用案例

传入和传出 可以使用以下方法将电子邮件与其相应的案例相关联 类方法。 此方法与 Email-to-Case(一个自动化过程)一起使用 这将从客户那里收到的电子邮件转变为客户服务案例。CasesgetCaseIdFromEmailThreadId

以下示例使用电子邮件线程 ID 来检索 相关案例 ID。

public class GetCaseIdController {

   public static void getCaseIdSample() {
        // Get email thread ID 
        String emailThreadId = '_00Dxx1gEW._500xxYktg';
        // Call Apex method to retrieve case ID from email thread ID 
        ID caseId = Cases.getCaseIdFromEmailThreadId(emailThreadId);
    
    }
}

区域管理2.0

通过对 Territory2 和 UserTerritory2Association 标准对象的触发器支持, 您可以自动执行与这些区域管理中的更改相关的操作和流程 记录。

Territory2 的示例触发器

此示例触发器在创建或删除 Territory2 记录后触发。 此示例触发器假定组织具有一个名为 TerritoryCount__c 的自定义字段,该字段在 Territory2Model 对象上定义为 跟踪每个区域模型中的区域净数量。触发器代码 每次创建或删除区域时,递增或递减TerritoryCount__c字段中的值。

trigger maintainTerritoryCount on Territory2 (after insert, after delete) {
    // Track the effective delta for each model
    Map<Id, Integer> modelMap = new Map<Id, Integer>();
    for(Territory2 terr : (Trigger.isInsert ? Trigger.new : Trigger.old)) {
       Integer offset = 0;
       if(modelMap.containsKey(terr.territory2ModelId)) {
           offset = modelMap.get(terr.territory2ModelId);
       }
       offset += (Trigger.isInsert ? 1 : -1);
       modelMap.put(terr.territory2ModelId, offset);
    }
    // We have a custom field on Territory2Model called TerritoryCount__c
    List<Territory2Model> models = [SELECT Id, TerritoryCount__c FROM 
                            Territory2Model WHERE Id IN :modelMap.keySet()];
    for(Territory2Model tm : models) {
       // In case the field is not defined with a default of 0
       if(tm.TerritoryCount__c == null) {
           tm.TerritoryCount__c = 0;
       }
       tm.TerritoryCount__c += modelMap.get(tm.Id);
    }
    // Bulk update the field on all the impacted models
    update(models);
}

UserTerritory2Association 的示例触发器

此示例触发器在创建 UserTerritory2Association 记录后触发。 此示例触发器向 Sales Operations 组发送电子邮件通知 让他们知道用户已添加到区域。它标识用户 谁将用户添加到区域。然后,它标识每个添加的用户以及 用户被添加到的区域以及区域所属的区域模型 自。

trigger notifySalesOps on UserTerritory2Association (after insert) {
    // Query the details of the users and territories involved
    List<UserTerritory2Association> utaList = [SELECT Id, User.FirstName, User.LastName, 
       Territory2.Name, Territory2.Territory2Model.Name 
       FROM UserTerritory2Association WHERE Id IN :Trigger.New];
            
    // Email message to send
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[]{'salesOps@acme.com'}); 
    mail.setSubject('Users added to territories notification');     
            
    // Build the message body   
    List<String> msgBody = new List<String>();
    String addedToTerrStr = '{0}, {1} added to territory {2} in model {3} \n';    
    msgBody.add('The following users were added to territories by ' +  
        UserInfo.getFirstName() + ', ' + UserInfo.getLastName() + '\n');
    for(UserTerritory2Association uta : utaList) {
       msgBody.add(String.format(addedToTerrStr, 
           new String[]{uta.User.FirstName, uta.User.LastName,
                        uta.Territory2.Name, uta.Territory2.Territory2Model.Name}));    
    } 
    
    // Set the message body and send the email
    mail.setPlainTextBody(String.join(msgBody,''));
    Messaging.sendEmail(new Messaging.Email[] { mail });
}