Skip to content

Instantly share code, notes, and snippets.

@leehildebrand
Created February 1, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leehildebrand/69c78ef7017230a262b7 to your computer and use it in GitHub Desktop.
Save leehildebrand/69c78ef7017230a262b7 to your computer and use it in GitHub Desktop.
Force.com REST Endpoint
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Author: Lee Hildebrand (http://leehildebrand.name)
//Date: 6/2/15
//Purpose: Provide APEXREST endpoint that allows for insert / update of Registered_User__c records
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@RestResource(urlMapping='/v.1/RegisteredUsersService/*')
global with sharing class RegisteredUsersService{
global class user_to_register {
Global String active;
Global String birth_year;
Global String credentials;
Global String current_login_at;
Global String email;
Global String employer;
Global String first_name;
Global String last_login_at;
Global String last_name;
Global String last_request_at;
Global String login_count;
Global String occupation;
Global String role;
Global String specialty_info;
Global String stringfield;
Global String treatment_facility;
Global String id;
}
@HttpPost
global static ReturnClass register_users(user_to_register[] registered_users) {
try {
Registered_User__c[] parsed_users = new Registered_User__c[]{};
for (user_to_register u : registered_users){
String name = u.first_name+' '+u.last_name;
parsed_users.add(new Registered_User__c( Name = name.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('Name').getDescribe().getLength()-3),
active__c = !String.isBlank(u.active) ? u.active.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('active__c').getDescribe().getLength()-3) : '',
birth_year__c = !String.isBlank(u.birth_year) ? Integer.ValueOf(u.birth_year) : null,
credentials__c = !String.isBlank(u.credentials) ? u.credentials.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('credentials__c').getDescribe().getLength()-3) : '',
current_login_at__c = !String.isBlank(u.current_login_at) ? Datetime.valueOf(u.current_login_at.replace('T',' ')) : null,
email__c = u.email,
employer__c = !String.isBlank(u.employer) ? u.employer.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('employer__c').getDescribe().getLength()-3) : '',
first_name__c = !String.isBlank(u.first_name) ? u.first_name.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('first_name__c').getDescribe().getLength()-3) : '',
last_login_at__c = !String.isBlank(u.last_login_at) ? Datetime.valueOf(u.last_login_at.replace('T',' ')) : null,
last_name__c = !String.isBlank(u.last_name) ? u.last_name.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('last_name__c').getDescribe().getLength()-3) : '',
last_request_at__c = !String.isBlank(u.last_request_at) ? Datetime.valueOf(u.last_request_at.replace('T',' ')) : null,
login_count__c = !String.isBlank(u.login_count) ? Integer.ValueOf(u.login_count) : null,
occupation__c = !String.isBlank(u.occupation) ? u.occupation.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('occupation__c').getDescribe().getLength()-3) : '',
role__c = !String.isBlank(u.role) ? Integer.ValueOf(u.role) : null,
specialty_info__c = !String.isBlank(u.specialty_info) ? u.specialty_info.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('specialty_info__c').getDescribe().getLength()-3) : '',
string__c = !String.isBlank(u.stringfield) ? u.stringfield.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('string__c').getDescribe().getLength()-3) : '',
treatment_facility__c = !String.isBlank(u.treatment_facility) ? u.treatment_facility.abbreviate(Schema.SObjectType.Registered_User__c.fields.getMap().get('treatment_facility__c').getDescribe().getLength()-3) : '',
user_id__c = !String.isBlank(u.id) ? Integer.ValueOf(u.id) : null));
}
Registered_User__c[] existing_users = new Registered_User__c[]{};
for(Registered_User__c ru : Database.query('SELECT user_id__c FROM Registered_User__c')){
existing_users.add(ru);
}
Registered_User__c[] users_to_upsert = new Registered_User__c[]{};
if(existing_users.isEmpty()) users_to_upsert.addAll(parsed_users);
else{
for(Registered_User__c pu : parsed_users){
Registered_User__c user = pu;
for(Registered_User__c ru : existing_users)
if(user.user_id__c == ru.user_id__c)
user.Id=ru.Id;
users_to_upsert.add(user);
}
}
if(!users_to_upsert.isEmpty()) upsert users_to_upsert;
return new ReturnClass('true', 'Users registered.', users_to_upsert);
} catch (Exception e) {
return new ReturnClass('false', e.getMessage(), null);
}
}
global class ReturnClass {
global String success;
global String message;
global Registered_User__c[] records;
global ReturnClass(String success, String message, Registered_User__c[] records) {
this.success = success;
this.message = message;
this.records = records;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment