diff --git a/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls b/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls new file mode 100644 index 00000000..4761ccef --- /dev/null +++ b/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls @@ -0,0 +1,162 @@ +public class RollupSummary_NewPage_Controller { + private ApexPages.StandardController controller {get; set;} + private LookupRollupSummary__c rus; + private Set numeric; + public List parentObjects {get;set;} + public List parentFields { + get{ + List retList = new List(); + retList.add(new SelectOption('','Select'+ (String.isBlank(rus.ParentObject__c)?'':' ' +rus.ParentObject__c) + ' field...')); + if(String.isNotBlank(rus.ParentObject__c)) + { + + Map parentFieldMap = gd.get(rus.ParentObject__c).getDescribe().fields.getMap(); + for(Schema.SObjectField field : parentFieldMap.values()) + { + Schema.DescribeFieldResult res = field.getDescribe(); + if(res.isAccessible() && res.isUpdateable() && numeric.contains(String.valueOf(res.getType()).trim().toLowercase())) + { + String objLabel = res.getLabel(); + String objAPI = res.getName(); + retList.add(new SelectOption(objApi,objLabel)); + } + } + } + retList.sort(); + return retList; + } + set; + } + public List childObjects { + get{ + List retList = new List(); + retList.add(new SelectOption('','Select a child object.')); + if(String.isNotBlank(rus.ParentObject__c)) + { + for(Schema.ChildRelationship rel : gd.get(rus.ParentObject__c).getDescribe().getChildRelationships()) + { + Schema.DescribeSObjectResult res = rel.getChildSObject().getDescribe(); + if(res.isAccessible() && res.isUpdateable()) + { + String objLabel = res.getLabel() + ' (' + rel.getField() +')'; + String objAPI = res.getName()+'.'+rel.getField(); + retList.add(new SelectOption(objApi,objLabel)); + } + } + } + retList.sort(); + return retList; + } + set; + } + public List childFields { + get{ + List retList = new List(); + retList.add(new SelectOption('','Select child field...')); + if(String.isNotBlank(rus.ChildObject__c)) + { + String strChildObject = rus.ChildObject__c.substringBefore('.'); + + Map parentFieldMap = gd.get(strChildObject).getDescribe().fields.getMap(); + for(Schema.SObjectField field : parentFieldMap.values()) + { + Schema.DescribeFieldResult res = field.getDescribe(); + if(res.isAccessible() && res.isUpdateable() && numeric.contains(String.valueOf(res.getType()).trim().toLowercase())) + { + String objLabel = res.getLabel(); + String objAPI = res.getName(); + retList.add(new SelectOption(objApi,objLabel)); + } + } + } + retList.sort(); + return retList; + } + set; + } + private Map gd; + public RollupSummary_NewPage_Controller(ApexPages.StandardController stdController) { + this.controller = stdController; + this.rus = (LookupRollupSummary__c)controller.getRecord(); + gd = Schema.getGlobalDescribe(); + numeric = new Set(); + numeric.add('currency'); + numeric.add('date'); + numeric.add('double'); + numeric.add('integer'); + numeric.add('percent'); + + // Get the list of creatable,updateable objects. + parentObjects = new List(); + parentObjects.add(new SelectOption('','Select Parent Object')); + for(String obj : gd.keySet()) + { + Schema.DescribeSObjectResult res = gd.get(obj).getDescribe(); + if(res.isCreateable() && res.isUpdateable()) + { + String objLabel = res.getLabel(); + String objAPI = res.getName(); + parentObjects.add(new SelectOption(objApi,objLabel)); + } + } + parentObjects.sort(); + } + public void setRelationshipField() + { + rus.RelationshipField__c = (rus!=null && String.isNotBlank(rus.ChildObject__c)?rus.ChildObject__c.substringAfter('.'):''); + } + + public PageReference save() + { + rus.ChildObject__c = rus.ChildObject__c.substringBefore('.'); + Set childTrigger = new Set(); + childTrigger.add(rus.ChildObject__c); + Map apexTriggers = new ApexTriggersSelector().selectByName(childTrigger); + String triggerName = RollupSummaries.makeTriggerName(rus); + Database.SaveResult sr; + if((rus.CalculationMode__c == RollupSummaries.CalculationMode.Realtime.name() || + rus.CalculationMode__c == RollupSummaries.CalculationMode.Scheduled.name()) && + !apexTriggers.containsKey(triggerName)) + { + //we need the trigger to be created so let's create it, + // and then let's set rus.Active__c = true; + //and then insert the rus record. + Boolean triggerWasDeployedSuccessfully = true; // TODO if the trigger deployment was successful + if(triggerWasDeployedSuccessfully) + { + //rus.Active__c = true; //Uncomment once the trigger deploying works. + sr = Database.insert(rus); + } + } + else + { + //rus.Active__c = true; //Uncomment once the trigger deploying works. + sr = Database.insert(rus); + } + PageReference pageRef; + if(sr.isSuccess()) + { + pageRef = new PageReference('/' + rus.Id); + pageRef.setRedirect(true); + } + else + { + for(Database.Error err : sr.getErrors()) + { + ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,err.getMessage())); + } + } + return pageRef; + } + public void getChildFields(String parentObjectName) + { + Map> gdFields = new Map>(); + SObjectType parentObjectType = gd.get(rus.ParentObject__c); + SObjectType childObjectType = gd.get(rus.ChildObject__c); + + if(parentObjectType!=null && !gdFields.containsKey(parentObjectType)) + gdFields.put(parentObjectType, parentObjectType.getDescribe().fields.getMap()); + if(childObjectType!=null && !gdFields.containsKey(childObjectType)) + gdFields.put(childObjectType, childObjectType.getDescribe().fields.getMap()); + } +} \ No newline at end of file diff --git a/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls-meta.xml b/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls-meta.xml new file mode 100644 index 00000000..62b0775a --- /dev/null +++ b/rolluptool/src/classes/RollupSummary_NewPage_Controller.cls-meta.xml @@ -0,0 +1,4 @@ + + + 28.0 + \ No newline at end of file diff --git a/rolluptool/src/pages/RollupSummary_NewPage.page b/rolluptool/src/pages/RollupSummary_NewPage.page new file mode 100644 index 00000000..2f195e8d --- /dev/null +++ b/rolluptool/src/pages/RollupSummary_NewPage.page @@ -0,0 +1,88 @@ + + +