Skip to content

Instantly share code, notes, and snippets.

@oximenvn
Created August 20, 2019 09:20
Show Gist options
  • Save oximenvn/9be51014f103e04f420161927e413609 to your computer and use it in GitHub Desktop.
Save oximenvn/9be51014f103e04f420161927e413609 to your computer and use it in GitHub Desktop.
Syncing for Outbound transfer to Salesforce
@RestResource(urlMapping='/Teams/*')
global with sharing class MyRestResource {
@HttpPut
global static String putCustomer(){
RestRequest req = Restcontext.request;
List<SObject> sbjectList = new List<SObject>();
// Deserialize
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(req.requestBody.tostring());
System.debug(params);
for(String tableName : params.keySet()){
if (tableName == 'f_user'){
// find sync Obj
List<Mapping__mdt> objList = [SELECT Id, SObject__c, Field__c, Column__c
FROM Mapping__mdt
WHERE Table__c =: tableName];
if (objList.size()==0) return 'list =0';
Mapping__mdt obj = objList[0];
if (obj == null) return 'null';
if (obj.SObject__c.length()<=0) return 'obj null';
// get Object by Object name
Schema.SObjectType objType = Schema.getGlobalDescribe().get(obj.SObject__c);
SObject newObj = objType.newSObject();
// put value to sfield
if (params.get(tableName) instanceof Map<String, Object> ){
Map<String, Object> columns = (Map<String, Object>)params.get(tableName);
for(String column : columns.keySet()){
String sfield = searchSfield(objList, column);
System.debug(sfield);
if (sfield =='') continue;
newObj.put(sfield, columns.get(column));
}
}
upsert newObj;
//sbjectList.add(newObj);
System.debug(newObj);
}
//thisTeam.put(field, params.get(field));
}
// insert or update
//List<SObject> has = [SELECT id FROM :];
//upsert sbjectList;
return '200';
}
// find field name for mapping sync
private static String searchSfield(List<Mapping__mdt> objList, String column ){
for(Mapping__mdt record: objList){
if(column == record.Column__c ){
String obj_field = record.Field__c;
// get field
return obj_field.substring(obj_field.lastIndexOf('.')+1);
}
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment