分类目录

链接

2018 年 4 月
 1
2345678
9101112131415
16171819202122
23242526272829
30  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Salesforce > 正文
salesforce rest api demo
Salesforce 暂无评论 阅读(516)
  1. //
  2. //api getPersonBasicInformation
  3. //by bruce he
  4. @RestResource(urlMapping='/user/profile/*')  
  5. global with sharing class Api_user_profile{  
  6.     
  7.     
  8.     @HttpGet  
  9.     global static void getPersonBasicInformation() {  
  10.         
  11.         RestResponse res = RestContext.response; 
  12.           res.addHeader('Content-Type', 'application/json');
  13.         
  14.         //return result obj
  15.         PersonBasicInformation model = new PersonBasicInformation();
  16.         
  17.         try{
  18.             system.debug('1111111111111');   
  19.             
  20.             //get params
  21.             RestRequest request = RestContext.request;
  22.             String PersonID =  request.params.get('PersonID');        
  23.             String PersonType = request.params.get('PersonType');
  24.             
  25.             //verify params
  26.             if(PersonID==null || PersonID.trim().length() == 0){ 
  27.                 res.responseBody = model.error('99','PersonID is required');
  28.                 return;
  29.             }
  30.             if(PersonType==null || PersonType.trim().length() == 0){                
  31.                 res.responseBody = model.error('99','PersonID is required');
  32.                 return;
  33.             }
  34.             String [] PersonTypes = new String[]{'C','A'};
  35.             PersonType = PersonType.toUpperCase();
  36.             if(!PersonTypes.contains(PersonType)){
  37.                 res.responseBody = model.error('99','PersonType is invalid');   
  38.                 return;
  39.             }
  40.             
  41.             //get result
  42.             if(PersonType == 'A'){  
  43.                 model = getAgent(PersonID);
  44.             }else if(PersonType == 'C'){
  45.                 model = getCustomer(PersonID);
  46.             }             
  47.             
  48.             
  49.             model.ContactMethods = getContactMethods(PersonID);            
  50.             system.debug('get ContactMethods ok'); 
  51.             model.PolicyDetails = getPolicyDetails(PersonID);         
  52.             system.debug('get getPolicyDetails ok'); 
  53.             
  54.             String result = JSON.serialize(model);  
  55.             system.debug(result); 
  56.             
  57.             //return result; 
  58.             res.responseBody = Blob.valueOf(result);  
  59.             
  60.         }catch(Exception ex){            
  61.             res.responseBody = model.error('99',ex.getMessage());
  62.         }
  63.          
  64.        
  65.     }
  66.     
  67.     private static PersonBasicInformation getAgent(String personId){
  68.         
  69.         //return result obj
  70.         PersonBasicInformation model = new PersonBasicInformation();
  71.         try{
  72.             Account account = [select Id, Status__c from account where External_Id__c =: personId and RecordType.Name ='Agent Account'  limit 1];
  73.             model.UserStatus = account.Status__c; 
  74.         }catch(Exception ex){
  75.             throw new DmlException('Account:'+personId+' is not found'); 
  76.         }
  77.         model.AgentIndicator  = getAgentIndicator(personId);
  78.         model.StaffIndicator = getStaffIndicator(personId);
  79.  
  80.         system.debug('aaaaaaaaaaaaaa'); 
  81.         return model;
  82.     }
  83.     
  84.     private static PersonBasicInformation getCustomer(String personId){
  85.         //return result obj
  86.         PersonBasicInformation model = new PersonBasicInformation();
  87.     
  88.         //get status
  89.         try{
  90.             Account account = [select Id, Status__c from account where External_Id__c =: personId and RecordType.Name ='Person Account' ];              
  91.             model.UserStatus = account.Status__c;
  92.         }catch(Exception ex){
  93.             throw new DmlException('Account:'+personId+' is not found'); 
  94.         }
  95.       
  96.         
  97.         system.debug('22222222222222'); 
  98.         //get hkid
  99.         List<Account_Registration__c> registrations = [select Identifier__c 
  100.                                                        from Account_Registration__c 
  101.                                                        where account__r.External_Id__c =:personId 
  102.                                                        and Registration_Type__c='PersonRegistration'  
  103.                                                        and Registration_Sub_Type__c = 'IdentityCard'];
  104.         //get AgentIndicator
  105.         if(registrations.size()>0){
  106.             //String HKID = registrations.get(0).Identifier__c;
  107.            model.AgentIndicator  = getAgentIndicator(personId);
  108.             
  109.         }else{
  110.             model.AgentIndicator  = 'false';
  111.         }
  112.         
  113.         model.StaffIndicator = getStaffIndicator(personId);
  114.        
  115.         system.debug('cccccccccccccccc'); 
  116.         
  117.         return model;
  118.     }
  119.     
  120.     //get AgentIndicator
  121.     private static String getAgentIndicator(String personId){
  122.         
  123.          system.debug('333333'); 
  124.          Integer role = [select count() from Account_Relationship_Role__c 
  125.                                                  where account__r.External_Id__c =:personId 
  126.                                                  and Account_Relationship__r.Name = 'Manulife Agent Employment' 
  127.                                                  and Status__c ='Active'];
  128.             //check AccountRelationshipRole 
  129.             if(role>0){
  130.                return 'true';
  131.             }else{
  132.                return 'false';
  133.             }
  134.     }
  135.     
  136.     
  137.     //get StaffIndicator
  138.     private static String getStaffIndicator(String personId){
  139.           system.debug('444444444'); 
  140.         
  141.           Integer role = [select count() from Account_Relationship_Role__c 
  142.                                                  where account__r.External_Id__c =:personId 
  143.                                                  and Account_Relationship__r.Name = 'Manulife Staff Employment' 
  144.                                                  and Status__c ='Active'];
  145.         if(role>0){
  146.             return 'true';
  147.         }else{
  148.             return 'false';
  149.         }
  150.     }
  151.  
  152.     //getContactMethod
  153.     private static List<ContactMethod> getContactMethods(String personId){
  154.         
  155.         system.debug('555555555555'); 
  156.         List<ContactMethod> result = new List<ContactMethod>();
  157.         
  158.         List<Contact_Method__c> ContactMethods = [select Contact_Method_Type__c,Contact_Method_Subtype__c,Validation_Result__c,Place__c,Phone_Country_Code__c,Phone_Number__c,Email__c,Address__c 
  159.                                                    from Contact_Method__c 
  160.                                                    where account__r.External_Id__c =:personId
  161.                                                    and Status__c = 'Active'];
  162.         
  163.         for(Contact_Method__c item : ContactMethods){
  164.             
  165.             ContactMethod cm = new ContactMethod();
  166.             cm.ContactMethodType = item.Contact_Method_Type__c;
  167.             cm.ContactMethodSubType = item.Contact_Method_Subtype__c;
  168.             cm.ValidationResult = item.Validation_Result__c;
  169.             cm.Place = item.Place__c;
  170.             
  171.             if(item.Contact_Method_Type__c == 'Email'){
  172.                 cm.ContactData = item.Email__c;
  173.             }else if(item.Contact_Method_Type__c == 'Phone'){
  174.                 cm.ContactData = item.Phone_Country_Code__c + item.Phone_Number__c;
  175.             }else if(item.Contact_Method_Type__c == 'Address'){
  176.                 cm.ContactData = item.Address__c;
  177.             }
  178.             result.Add(cm);
  179.         }
  180.         return result;
  181.     }
  182.     
  183.     //get  Policy Details;
  184.     private static List<PolicyDetail> getPolicyDetails(String personId){
  185.         List<PolicyDetail> result = new List<PolicyDetail>();
  186.         
  187.         system.debug('666666666666666'); 
  188.         //get Policy_account_role__c
  189.         List<Policy_account_role__c> roles = [select Policy__r.Policy_Number__c, Role__c,Policy__r.Policy_Type__c,Policy__r.Basic_Plan_Code__c
  190.                                               from Policy_account_role__c
  191.                                               where account__r.External_Id__c =:personId];
  192.         for(Policy_account_role__c item : roles){
  193.             PolicyDetail pd = new PolicyDetail();
  194.             pd.PolicyType = item.Policy__r.Policy_Type__c;
  195.             pd.PolicyNumber = item.Policy__r.Policy_Type__c;
  196.             //pd.SubGroupNumber = item.Policy_Type__c;
  197.             pd.CertificateNumber = '0';
  198.             pd.Role = item.Role__c;
  199.             pd.BasicPlanCode = item.Policy__r.Basic_Plan_Code__c;
  200.             result.Add(pd);
  201.         }
  202.                
  203.         //get Policy_certificate_account_role__c
  204.         List<Policy_certificate_account_role__c> cer_roles = [select Policy__r.Policy_Number__c, Role__c,Policy__r.Policy_Type__c,Policy__r.Basic_Plan_Code__c,Policy_Certificate__r.Certificate_Number__c
  205.                                                               from Policy_certificate_account_role__c
  206.                                                               where account__r.External_Id__c =:personId];
  207.          
  208.         for(Policy_certificate_account_role__c item : cer_roles){
  209.             PolicyDetail pd = new PolicyDetail();
  210.             pd.PolicyType = item.Policy__r.Policy_Type__c;
  211.             pd.PolicyNumber = item.Policy__r.Policy_Type__c;
  212.             //pd.SubGroupNumber = item.Policy_Type__c;
  213.             pd.CertificateNumber = item.Policy_Certificate__r.Certificate_Number__c;
  214.             pd.Role = item.Role__c;
  215.             pd.BasicPlanCode = item.Policy__r.Basic_Plan_Code__c;
  216.             result.Add(pd);
  217.         }
  218.         
  219.         return result;
  220.     }
  221.     
  222.     //----------------------------------------------------------------------
  223.     //inner class model for api return
  224.     //
  225.     public class Error
  226.     {
  227.         public String ReturnCode { get; set; } 
  228.         public String ReturnMessage { get; set; }         
  229.     }
  230.     
  231.     public class ContactMethod 
  232.     {
  233.         public String ContactMethodType { get; set; } 
  234.         public String ContactMethodSubType { get; set; } 
  235.         public String ValidationResult { get; set; } 
  236.         public String Place { get; set; } 
  237.         public String ContactData { get; set; }     
  238.     }
  239.     
  240.     public class PolicyDetail 
  241.     {
  242.         public String PolicyType { get; set; }     
  243.         public String PolicyNumber { get; set; } 
  244.         public String SubGroupNumber { get; set; } 
  245.         public String CertificateNumber { get; set; } 
  246.         public String Role { get; set; } 
  247.         public String BasicPlanCode { get; set; } 
  248.     
  249.     }
  250.     
  251.     public class PersonBasicInformation
  252.     {
  253.         public PersonBasicInformation(){            
  254.             this.ReturnCode = '00';
  255.             this.ReturnMessage='';
  256.         }
  257.         public PersonBasicInformation(String returnCode,String returnMessage){
  258.             this.ReturnCode = returnCode;
  259.             this.ReturnMessage = returnMessage;
  260.         }
  261.         
  262.         public Blob error(String returnCode,String returnMessage){
  263.             Error m =new Error();
  264.             m.ReturnCode = returnCode;
  265.             m.ReturnMessage = returnMessage;
  266.             
  267.             return Blob.valueOf(JSON.serialize(m));
  268.         }
  269.         
  270.         public String ReturnCode { get; set; } 
  271.         public String ReturnMessage { get; set; } 
  272.         public String UserStatus { get; set; } 
  273.         public String AgentIndicator { get; set; } 
  274.         public String StaffIndicator { get; set; } 
  275.         public List<ContactMethod> ContactMethods  { get; set; } 
  276.         public List<PolicyDetail> PolicyDetails  { get; set; } 
  277.     }
  278. }

POSTMAN测试:

1.获取token

2.查询数据

 

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:salesforce rest api demo | Bruce's Blog

发表评论

留言无头像?