salesforce接收站点传过来的json,解析并序列化json,在保存到salesforce的个案以及测试类...

@RestResource(urlMapping='/LiveChatTicketWebhook')
global without sharing class LiveChatTicketWebhook {
       
      @httpPost 
    global static void doPost() {
        //post请求
        RestRequest req = RestContext.request;
        //响应
        RestResponse res = RestContext.response;
        
        //解析请求到的body
        String requestBbody = req.requestBody.toString();
        //转义反斜杠
        String reqBbody = requestBbody.Replace('\n','\\n');
        system.debug(requestBbody);
        try{
        //序列化字符串
        TicketType tt = (TicketType)JSON.deserialize(reqBbody, TicketType.class);
        system.debug(tt);

         //查找相同的邮箱
          List cont =[select email from Contact where email =:tt.ticket.requester.mail];
          Case c = new Case();
              String s='';
              String name = '';
              String texts = '';
              String sub = '';
              String id='';
         //如果cont不为空,就关联个案与联系人
         if(cont != null && cont.size() > 0){
              c.ContactId=cont[0].Id; 
              //循环取出 Events 里面的message
              for(Events et:tt.ticket.events){
              texts  = et.message;
              }    
              id=tt.license_id;
              //获取我传进来的值,赋给LivechatSetting__c 方法
          LivechatSetting__c ls = CommonUtil.getLivechatSetting(id);
              if(ls!= null){
                  c.origin=ls.Case_Origin__c;  
              }
              name  = tt.ticket.requester.name;
              s += name+':'+texts+'\n';  
              c.Description=s; 
              Insert c; 
              
             
           } else{
              for(Events et:tt.ticket.events){
              texts  = et.message;
              }
                id=tt.license_id;
              LivechatSetting__c ls = CommonUtil.getLivechatSetting(id);
                if(ls!= null){
                  c.origin=ls.Case_Origin__c;  
              } 
              name = tt.ticket.requester.name;
              sub = tt.ticket.subject;
              s += name+':'+texts+'\n';  
              c.Description=s; 
              c.SuppliedName=tt.ticket.requester.name;
              c.SuppliedEmail=tt.ticket.requester.mail;
              Insert c;          
           
           }
       
    }catch(Exception ex){
        insert new log__c(name='ticket error:',description__c=ex.getmessage());
       }
        
    
    }
    public class TicketType{
        private String event_type;
        private String token;
        private String license_id;
        private Ticket ticket;
        
    }
    public class Ticket { 
        // private Assignee assignee;      
         private Events[] events;
         private Groups[] groups;
         private String id;
         private Requester requester;
         private String status;
         private String subject;
         private List tags;
         private Source source;

     }
   
     
    class Events{
         private Author author;
       //  private Date date;
        // private boolean is_private;
         private String message;
         private String type;
         private Source source;
     }
    class Requester{
         private String mail;
         private String name;
         private String ip;
     }
     
    class Source {
        private String type;
        private String url;
    }
    
    class Author {
        private String id;
        private String name;
        private String type;
    }
    
    class Groups {
        private Decimal id;
        private String name;
    }
   
    }

 

@isTest
public class LiveChatTicketWebhookTest{
        
    @isTest
    private static void doPostTest() {
        Contact con = new Contact( LastName = 'test',email = '[email protected]');
        insert con;
       
        
        String JSONMsg = '{"event_type":"ticket_created","token":"d157e75ddb196c7c324065e959b3afd9","license_id":"8918404",'
        +'"ticket":{"events":[{"author":{"id":"[email protected]","name":"testAngle","type":"client"},"date":"2017-11-13T08:52:10Z",'
        +'"is_private":false,"message":"Message:\ntest\n/Cart: 0 items, $0.00 total","type":"message","source":'
        +'{"url":"https://www.queenfy.com/","type":"chat-window"}}],"groups":[{"id":0,"name":"All operators"}],"id":"K01DF",'
        +'"requester":{"mail":"[email protected]","name":"testAngle","utc_offset":"Asia/Shanghai","ip":"61.152.234.182"},"status":"open",'
        +'"subject":"(no subject)","tags":[],"source":{"type":"chat-window","url":"https://www.queenfy.com/"}}}';
    
        RestRequest request = new RestRequest();
        request.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
        request.addHeader('X-Shopify-Shop-Domain', 'mydemostore2017.myshopify.com');
        request.httpMethod = 'POST';        // Perform a POST
        request.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST

        RestContext.request = request;
        // Call the method to test
        LiveChatTicketWebhook.doPost();
        
        
       String JSONMsgs = '{"event_type":"ticket_created","token":"d157e75ddb196c7c324065e959b3afd9","license_id":"8918404",'
        +'"ticket":{"events":[{"author":{"id":"[email protected]","name":"testAngle","type":"client"},"date":"2017-11-13T08:52:10Z",'
        +'"is_private":false,"message":"Message:test/Cart: 0 items, $0.00 total","type":"message","source":'
        +'{"url":"https://www.queenfy.com/","type":"chat-window"}}],"groups":[{"id":0,"name":"All operators"}],"id":"K01DF",'
        +'"requester":{"mail":"[email protected]","name":"testAngle","utc_offset":"Asia/Shanghai","ip":"61.152.234.182"},"status":"open",'
        +'"subject":"(no subject)","tags":[],"source":{"type":"chat-window","url":"https://www.queenfy.com/"}}}';
    
        RestRequest requests = new RestRequest();
        requests.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
        requests.addHeader('X-Shopify-Shop-Domain', 'mydemostore2017.myshopify.com');
        requests.httpMethod = 'POST';        // Perform a POST
        requests.requestBody = Blob.valueof(JSONMsgs); // Add JSON Message as a POST

        RestContext.request = requests;
        // Call the method to test
        LiveChatTicketWebhook.doPost();
    }
    
}

  

转载于:https://www.cnblogs.com/pipidan/p/7831656.html

你可能感兴趣的:(salesforce接收站点传过来的json,解析并序列化json,在保存到salesforce的个案以及测试类...)