现在位置:
首页 > Salesforce > 正文
salesforce rest api demo
- //
- //api getPersonBasicInformation
- //by bruce he
- @RestResource(urlMapping='/user/profile/*')
- global with sharing class Api_user_profile{
- @HttpGet
- global static void getPersonBasicInformation() {
- RestResponse res = RestContext.response;
- res.addHeader('Content-Type', 'application/json');
- //return result obj
- PersonBasicInformation model = new PersonBasicInformation();
- try{
- system.debug('1111111111111');
- //get params
- RestRequest request = RestContext.request;
- String PersonID = request.params.get('PersonID');
- String PersonType = request.params.get('PersonType');
- //verify params
- if(PersonID==null || PersonID.trim().length() == 0){
- res.responseBody = model.error('99','PersonID is required');
- return;
- }
- if(PersonType==null || PersonType.trim().length() == 0){
- res.responseBody = model.error('99','PersonID is required');
- return;
- }
- String [] PersonTypes = new String[]{'C','A'};
- PersonType = PersonType.toUpperCase();
- if(!PersonTypes.contains(PersonType)){
- res.responseBody = model.error('99','PersonType is invalid');
- return;
- }
- //get result
- if(PersonType == 'A'){
- model = getAgent(PersonID);
- }else if(PersonType == 'C'){
- model = getCustomer(PersonID);
- }
- model.ContactMethods = getContactMethods(PersonID);
- system.debug('get ContactMethods ok');
- model.PolicyDetails = getPolicyDetails(PersonID);
- system.debug('get getPolicyDetails ok');
- String result = JSON.serialize(model);
- system.debug(result);
- //return result;
- res.responseBody = Blob.valueOf(result);
- }catch(Exception ex){
- res.responseBody = model.error('99',ex.getMessage());
- }
- }
- private static PersonBasicInformation getAgent(String personId){
- //return result obj
- PersonBasicInformation model = new PersonBasicInformation();
- try{
- Account account = [select Id, Status__c from account where External_Id__c =: personId and RecordType.Name ='Agent Account' limit 1];
- model.UserStatus = account.Status__c;
- }catch(Exception ex){
- throw new DmlException('Account:'+personId+' is not found');
- }
- model.AgentIndicator = getAgentIndicator(personId);
- model.StaffIndicator = getStaffIndicator(personId);
- system.debug('aaaaaaaaaaaaaa');
- return model;
- }
- private static PersonBasicInformation getCustomer(String personId){
- //return result obj
- PersonBasicInformation model = new PersonBasicInformation();
- //get status
- try{
- Account account = [select Id, Status__c from account where External_Id__c =: personId and RecordType.Name ='Person Account' ];
- model.UserStatus = account.Status__c;
- }catch(Exception ex){
- throw new DmlException('Account:'+personId+' is not found');
- }
- system.debug('22222222222222');
- //get hkid
- List<Account_Registration__c> registrations = [select Identifier__c
- from Account_Registration__c
- where account__r.External_Id__c =:personId
- and Registration_Type__c='PersonRegistration'
- and Registration_Sub_Type__c = 'IdentityCard'];
- //get AgentIndicator
- if(registrations.size()>0){
- //String HKID = registrations.get(0).Identifier__c;
- model.AgentIndicator = getAgentIndicator(personId);
- }else{
- model.AgentIndicator = 'false';
- }
- model.StaffIndicator = getStaffIndicator(personId);
- system.debug('cccccccccccccccc');
- return model;
- }
- //get AgentIndicator
- private static String getAgentIndicator(String personId){
- system.debug('333333');
- Integer role = [select count() from Account_Relationship_Role__c
- where account__r.External_Id__c =:personId
- and Account_Relationship__r.Name = 'Manulife Agent Employment'
- and Status__c ='Active'];
- //check AccountRelationshipRole
- if(role>0){
- return 'true';
- }else{
- return 'false';
- }
- }
- //get StaffIndicator
- private static String getStaffIndicator(String personId){
- system.debug('444444444');
- Integer role = [select count() from Account_Relationship_Role__c
- where account__r.External_Id__c =:personId
- and Account_Relationship__r.Name = 'Manulife Staff Employment'
- and Status__c ='Active'];
- if(role>0){
- return 'true';
- }else{
- return 'false';
- }
- }
- //getContactMethod
- private static List<ContactMethod> getContactMethods(String personId){
- system.debug('555555555555');
- List<ContactMethod> result = new List<ContactMethod>();
- 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
- from Contact_Method__c
- where account__r.External_Id__c =:personId
- and Status__c = 'Active'];
- for(Contact_Method__c item : ContactMethods){
- ContactMethod cm = new ContactMethod();
- cm.ContactMethodType = item.Contact_Method_Type__c;
- cm.ContactMethodSubType = item.Contact_Method_Subtype__c;
- cm.ValidationResult = item.Validation_Result__c;
- cm.Place = item.Place__c;
- if(item.Contact_Method_Type__c == 'Email'){
- cm.ContactData = item.Email__c;
- }else if(item.Contact_Method_Type__c == 'Phone'){
- cm.ContactData = item.Phone_Country_Code__c + item.Phone_Number__c;
- }else if(item.Contact_Method_Type__c == 'Address'){
- cm.ContactData = item.Address__c;
- }
- result.Add(cm);
- }
- return result;
- }
- //get Policy Details;
- private static List<PolicyDetail> getPolicyDetails(String personId){
- List<PolicyDetail> result = new List<PolicyDetail>();
- system.debug('666666666666666');
- //get Policy_account_role__c
- 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
- from Policy_account_role__c
- where account__r.External_Id__c =:personId];
- for(Policy_account_role__c item : roles){
- PolicyDetail pd = new PolicyDetail();
- pd.PolicyType = item.Policy__r.Policy_Type__c;
- pd.PolicyNumber = item.Policy__r.Policy_Type__c;
- //pd.SubGroupNumber = item.Policy_Type__c;
- pd.CertificateNumber = '0';
- pd.Role = item.Role__c;
- pd.BasicPlanCode = item.Policy__r.Basic_Plan_Code__c;
- result.Add(pd);
- }
- //get Policy_certificate_account_role__c
- 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
- from Policy_certificate_account_role__c
- where account__r.External_Id__c =:personId];
- for(Policy_certificate_account_role__c item : cer_roles){
- PolicyDetail pd = new PolicyDetail();
- pd.PolicyType = item.Policy__r.Policy_Type__c;
- pd.PolicyNumber = item.Policy__r.Policy_Type__c;
- //pd.SubGroupNumber = item.Policy_Type__c;
- pd.CertificateNumber = item.Policy_Certificate__r.Certificate_Number__c;
- pd.Role = item.Role__c;
- pd.BasicPlanCode = item.Policy__r.Basic_Plan_Code__c;
- result.Add(pd);
- }
- return result;
- }
- //----------------------------------------------------------------------
- //inner class model for api return
- //
- public class Error
- {
- public String ReturnCode { get; set; }
- public String ReturnMessage { get; set; }
- }
- public class ContactMethod
- {
- public String ContactMethodType { get; set; }
- public String ContactMethodSubType { get; set; }
- public String ValidationResult { get; set; }
- public String Place { get; set; }
- public String ContactData { get; set; }
- }
- public class PolicyDetail
- {
- public String PolicyType { get; set; }
- public String PolicyNumber { get; set; }
- public String SubGroupNumber { get; set; }
- public String CertificateNumber { get; set; }
- public String Role { get; set; }
- public String BasicPlanCode { get; set; }
- }
- public class PersonBasicInformation
- {
- public PersonBasicInformation(){
- this.ReturnCode = '00';
- this.ReturnMessage='';
- }
- public PersonBasicInformation(String returnCode,String returnMessage){
- this.ReturnCode = returnCode;
- this.ReturnMessage = returnMessage;
- }
- public Blob error(String returnCode,String returnMessage){
- Error m =new Error();
- m.ReturnCode = returnCode;
- m.ReturnMessage = returnMessage;
- return Blob.valueOf(JSON.serialize(m));
- }
- public String ReturnCode { get; set; }
- public String ReturnMessage { get; set; }
- public String UserStatus { get; set; }
- public String AgentIndicator { get; set; }
- public String StaffIndicator { get; set; }
- public List<ContactMethod> ContactMethods { get; set; }
- public List<PolicyDetail> PolicyDetails { get; set; }
- }
- }
POSTMAN测试:
1.获取token
2.查询数据
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · Salesforce VisualForce 使用apex:actionFunction调用后台方法
- · How to Call Salesforce API from Lightning Component
- · apex highlight userDefineLang for notepad++
- · 详解 Salesforce 15 和 18 位的ID
- · Salesforce Test class注意事项
- · salesforce Milestone clock is not running
- · salesforce apex traced the number of query
- · salesforce antscript无法拉取完整的profile
- · salesforce lightning report取URL参数
- · salesforce test class用现有数据测试
- · salesforce report get url parameters
- · salesforce display trigger error