Created
August 24, 2014 23:30
-
-
Save mtetlow/a9054645d37317952be4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Usage*/ | |
@RemoteAction | |
global static void archivePersonalTasksInLastColumn(){ | |
trTaskboardController.setStaticVarsForRemoteActions(); | |
//String lastListName=trController.getLastListName(); | |
List<Project_Task__c> tasksToArchive = [SELECT Archived__c FROM Project_Task__c WHERE List__c = :lastListName AND Project__c = null AND OwnerId=:UserInfo.getUserId()]; | |
for(Project_Task__c task : tasksToArchive){ | |
task.Archived__c = true; | |
} | |
//update tasksToArchive; | |
update(trUtilities.stripFLSFields('update', tasksToArchive)); | |
} | |
/*Method*/ | |
public static List<sObject> stripFLSFields(String operation, List<sObject> newRecords){ | |
List<sObject> returnList = new List<sObject>(); | |
if(newRecords.size()>0){ | |
Schema.sObjectType objectType = newRecords[0].getSObjectType(); | |
Map<String,Schema.SObjectField> objectFields = objectType.getDescribe().fields.getMap(); | |
List<Schema.describeFieldResult> flsValidFields = new List<Schema.describeFieldResult>(); | |
for(String fieldKey : objectFields.keySet()){ | |
Schema.SObjectField field = objectFields.get(fieldKey); | |
Schema.describeFieldResult fieldDescribe = field.getDescribe(); | |
if(operation == 'insert'){ | |
if(fieldDescribe.isCreateable() || fieldDescribe.getName() == 'Id'){ | |
flsValidFields.add(fieldDescribe); | |
} | |
} else if(operation == 'update' || operation =='delete'){ | |
if(fieldDescribe.isUpdateable() || fieldDescribe.getName() == 'Id'){ | |
flsValidFields.add(fieldDescribe); | |
} | |
} else if(operation == 'upsert'){ | |
if( ( fieldDescribe.isUpdateable() && fieldDescribe.isCreateable() ) || fieldDescribe.getName() == 'Id'){ | |
flsValidFields.add(fieldDescribe); | |
} | |
} | |
} | |
for(sObject newRecord : newRecords){ | |
sObject objectTemplate = objectType.newSObject(); | |
for(Schema.describeFieldResult field : flsValidFields){ | |
String fieldName = field.getName(); | |
try{ | |
if(newRecord.get(fieldName)!=null){ | |
objectTemplate.put(fieldName,newRecord.get(fieldName)); | |
} | |
} catch(Exception e){ | |
} | |
} | |
returnList.add(objectTemplate); | |
} | |
} | |
return returnList; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment