You need to sign in to do that
Don't have an account?
Sony PSP
Profile hidding field without afftecting the order of the fields
Hi guys,
can you help me on how or where could I possibly invoke that when a profile is Sales it will not display the field Lead Source, while if the Profile is Manager it display the field Lead Source without affecting the order of the field..
can you help me on how or where could I possibly invoke that when a profile is Sales it will not display the field Lead Source, while if the Profile is Manager it display the field Lead Source without affecting the order of the field..
public class OpportunityCustomCloningController { public Id oppId {get; set;} public Id recordTypeId {get; set;} public Opportunity opp {get;set;} public String cloneOption {get;set;} public String pageLayoutFields {get; set;} public List<String> addFields {get; set;} public List<LayoutComponent__c> sectionComponent {get;set;} public Map<Id, List<LayoutComponent__c>> fieldComponent {get; set;} public string OPPORTUNITY_STRING = 'Opportunity'; public string MAINTENANCE_PRODUCT_STRING = 'mpo'; public string WITH_PRODUCT_STRING = 'wip'; public string WITHOUT_PRODUCT_STRING = 'wop'; public string CLONE_OPTION_STRING = 'clo'; public string ID_STRING = 'id'; public static Boolean isCloned = false; private transient Map<String,Schema.DescribeFieldResult> fieldDescribe; private Integer dependentCount = 0; public ApexPages.StandardController controller {get; set;} public OpportunityCustomCloningController(ApexPages.StandardController controller){ this.controller = controller; if(!Test.isRunningTest() && addFields != null) { controller.addFields(addFields); } oppId = controller.getId(); opp = (Opportunity) controller.getRecord(); recordTypeId = opp.RecordTypeID; cloneOption = apexpages.currentpage().getparameters().get(CLONE_OPTION_STRING); getMetadata(); //getPageLayout(); } private void getMetadata(){ Map<String,Schema.SObjectField> fieldMap = Schema.sObjectType.Opportunity.fields.getMap(); fieldDescribe = new Map<String,Schema.DescribeFieldResult>(); for(String fieldName : fieldMap.keyset()){ fieldDescribe.put(fieldName.toLowerCase(),fieldMap.get(fieldName).getDescribe()); } } public PageReference customClone() { //String urlRecordType = pageRecordType(); String urlRecordType = 'OpportunityCustomCloneEdit'; PageReference editCloneOpportunityPage = new PageReference('/apex/'+urlRecordType); editCloneOpportunityPage.getParameters().put(ID_STRING , opp.Id); editCloneOpportunityPage.getParameters().put(CLONE_OPTION_STRING , cloneOption); editCloneOpportunityPage.setRedirect(true); return editCloneOpportunityPage; } public Map<String, SObjectField> getObjectFields(String objectName) { SObjectType objToken = Schema.getGlobalDescribe().get(objectName); DescribeSObjectResult objDef = objToken.getDescribe(); Map<String, SObjectField> fields = objDef.fields.getMap(); return fields; } public void getCloneSettings() { Map<String, Opportunity_Fields__c> allOpportunityFields = Opportunity_Fields__c.getAll(); List<Opportunity_Fields__c> opportunityFields = allOpportunityFields.values(); Map<String, SObjectField> fields = getObjectFields(OPPORTUNITY_STRING); if(fieldDescribe == null){ getMetadata(); } for(Opportunity_Fields__c ofs : opportunityFields){ System.debug('* ** ** ' + ofs.API_Name__c + ' ' + ofs.To_Be_Cloned__c); if(ofs.To_Be_Cloned__c == null || !ofs.To_Be_Cloned__c){ //SObjectField f = fields.get(ofs.API_Name__c); if(fieldDescribe.containsKey(ofs.API_Name__c)){ DescribeFieldResult fr = fieldDescribe.get(ofs.API_Name__c.toLowerCase()); //f.getDescribe(); System.debug('* ** *** fr ' + fr); if(fr.isUpdateable() && fr.isAccessible()){ opp.put(ofs.API_Name__c, NULL); } }else{ try{ opp.put(ofs.API_Name__c, NULL); }catch(Exception e){} } } } System.debug('* ** *** opp ' + opp); } public PageReference customSaveAndAddProduct(){ opp.Id = NULL; opp.OwnerId = UserInfo.getUserId(); boolean hasError = false; try{ //checks if the opportunity has already been cloned once during the transaction. if(!isCloned) { insert opp; isCloned = true; sendSuccessMessage(opp.Id); } }catch(Exception e){ hasError = true; isCloned = false; ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); sendErrorEmail(e.getMessage()); } if(hasError){ return null; }else{ PageReference opportunityProductSelect = new PageReference('/apex/OpportunityProductSelection'); opportunityProductSelect.getParameters().put(ID_STRING , opp.Id); return opportunityProductSelect; } } public PageReference customSave() { List<OpportunityLineItem> oppProdList = new List<OpportunityLineItem>(); if(cloneOption == WITH_PRODUCT_STRING || cloneOption == MAINTENANCE_PRODUCT_STRING){ Map<String, SObjectField> oltFields = getObjectFields('OpportunityLineItem'); String selectFields = ''; String dbQuery = ''; for (Schema.SObjectField ft : oltFields.values()){ Schema.DescribeFieldResult fd = ft.getDescribe(); if(fd.isCreateable()){ if(selectFields == '') { selectFields = fd.getName(); } else { selectFields = selectFields + ', ' + fd.getName(); } } } dbQuery = 'SELECT ' + selectFields + ' FROM OpportunityLineItem WHERE OpportunityId = \'' +opp.Id+ '\' AND Product2.isActive = true'; if(cloneOption == MAINTENANCE_PRODUCT_STRING) { dbQuery = dbquery + ' AND Product2.Family = \'Maintenance\''; } List<OpportunityLineItem> opportunityProducts = Database.query(dbQuery); for(OpportunityLineItem op : opportunityProducts){ oppProdList.add(op.clone(false,true)); } } opp.Id = NULL; opp.OwnerId = UserInfo.getUserId(); boolean hasError = false; try{ //checks if the opportunity has already been cloned once during the transaction. if(!isCloned) { insert opp; } }catch(Exception e){ hasError = true; isCloned = false; ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); sendErrorEmail(e.getMessage()); } if(!hasError && !isCloned){ if(!oppProdList.isEmpty()){ for(OpportunityLineItem olt : oppProdList) { olt.OpportunityID = opp.Id; if(olt.UnitPrice != NULL && olt.TotalPrice != NULL) { olt.UnitPrice = NULL; } } try{ insert oppProdList; isCloned = true; sendSuccessMessage(opp.Id); }catch(Exception e){ hasError = true; isCloned = false; ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); sendErrorEmail(e.getMessage()); } } } if(hasError){ return null; }else{ return new PageReference('/'+opp.id); } } public static Boolean runningInASandbox() { return [SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox; } public Boolean getShowSaveAndAddProducts(){ if(cloneOption == WITHOUT_PRODUCT_STRING){ return true; } else return false; } public Boolean getShowSaveButton(){ if(cloneOption == WITHOUT_PRODUCT_STRING){ return false; } else return true; } public Component.Apex.PageBlock dynBlock; public Component.Apex.PageBlock getDynamicBlock(){ if(dynBlock != null){ return dynBlock; } Component.Apex.PageBlock dynBlock = new Component.Apex.PageBlock(); dynBlock.mode = 'edit'; dynBlock.id = 'mainBlock'; dynBlock.title = 'Opportunity Clone'; Component.Apex.PageBlockButtons buttons = new Component.Apex.PageBlockButtons(); if(getShowSaveButton()){ Component.Apex.CommandButton saveButton = new Component.Apex.CommandButton(); saveButton.value = 'Save'; saveButton.Id = 'saveButton'; saveButton.onclick = 'saveButtonBehavior(true);'; saveButton.oncomplete = 'saveButtonBehavior(false);'; saveButton.expressions.action = '{!customSave}'; saveButton.rerender = new Set<String>{'msgs'}; buttons.childComponents.add(saveButton); } if(getShowSaveAndAddProducts()){ Component.Apex.CommandButton saveAddButton = new Component.Apex.CommandButton(); saveAddButton.value = 'Save & Add Product'; saveAddButton.Id = 'saveAddButton'; saveAddButton.onclick = 'saveButtonBehavior(true);'; saveAddButton.oncomplete = 'saveButtonBehavior(false);'; saveAddButton.expressions.action = '{!customSaveAndAddProduct}'; saveAddButton.rerender = new Set<String>{'msgs'}; buttons.childComponents.add(saveAddButton); } Component.Apex.CommandButton cancelButton = new Component.Apex.CommandButton(); cancelButton.value = 'Cancel'; cancelButton.Id = 'cancelButton'; cancelButton.expressions.action = '{!cancel}'; buttons.childComponents.add(cancelButton); buttons.Id = 'pageBlockButtons'; dynBlock.childComponents.add(buttons); List<LayoutComponent__c> layoutComponentList= new List<LayoutComponent__c>(); sectionComponent = new List<LayoutComponent__c>(); fieldComponent = new Map<Id, List<LayoutComponent__c>>(); pageLayoutFields = ''; List<String> fieldNames = new List<String>(); Id profileId = UserInfo.getProfileId(); List<LayoutMapping__c > layoutMapList = [SELECT Layout__c FROM LayoutMapping__c WHERE SObjectType__c = :OPPORTUNITY_STRING AND RecordTypeId__c = :recordTypeId AND ProfileId__c = :profileId ORDER BY CreatedDate DESC LIMIT 1]; if(layoutMapList.isEmpty()){ layoutMapList = [SELECT Layout__c FROM LayoutMapping__c WHERE SObjectType__c = :OPPORTUNITY_STRING AND IsDefault__c = true AND ProfileId__c = :profileId ORDER BY CreatedDate DESC LIMIT 1]; if(layoutMapList.isEmpty()){ layoutMapList = [SELECT Layout__c FROM LayoutMapping__c WHERE SObjectType__c = :OPPORTUNITY_STRING AND IsDefault__c = true ORDER BY CreatedDate DESC LIMIT 1]; } } if(!layoutMapList.isEmpty()){ getMetadata(); Component.Apex.PageBlockSection debugSection = new Component.Apex.PageBlockSection(title='Debug'); for(String fieldName : fieldDescribe.keyset()){ //debugSection.childComponents.add(new Component.Apex.OutputText(value = '"'+fieldName+'"')); } //dynBlock.childComponents.add(debugSection); Id layoutId = layoutMapList.get(0).Layout__c; List<Id> sectionIds = new List<Id>(); Map<Id,Component.Apex.PageBlockSection> componentMap = new Map<Id,Component.Apex.PageBlockSection>(); for(LayoutComponent__c lc : [ SELECT ID, Name, Columns__c, IsRequired__c, IsBlank__c, IsField__c, IsSection__c, Order__c, ParentComponent__c, FieldName__c,SectionName__c FROM LayoutComponent__c WHERE Layout__c = :layoutId AND IsSection__c = true ORDER BY Order__c ]){ System.debug('* ** *** component ' + lc); if(lc.IsSection__c) { Component.Apex.PageBlockSection sectionComponent = new Component.Apex.PageBlockSection(); sectionComponent.title = lc.SectionName__c; // + ' ' + lc.Id + ' ' + lc.Name; sectionIds.add(lc.Id); componentMap.put(lc.Id,sectionComponent); System.debug('* ** *** adding section ' + lc.SectionName__c); } }//end-for for(LayoutComponent__c lc : [ SELECT ID, Name, Columns__c, IsRequired__c, IsBlank__c, IsField__c, IsSection__c, IsEditable__c, Order__c, ParentComponent__c, FieldName__c,SectionName__c FROM LayoutComponent__c WHERE Layout__c = :layoutId AND (IsField__c = true OR IsBlank__c = true) ORDER BY Order__c ]){ if(lc.IsField__c && !String.isBlank(lc.FieldName__c) && fieldDescribe.containsKey(lc.FieldName__c.toLowerCase()) && componentMap.containsKey(lc.ParentComponent__c) ){ boolean isDependent = fieldDescribe.get(lc.FieldName__c.toLowerCase()).isDependentPicklist(); if(isDependent){ dependentCount++; } //debugSection.childComponents.add(new Component.Apex.OutputText(value = '{!opp.' + lc.FieldName__c + '}')); if(lc.IsEditable__c && (!isDependent || dependentCount <= 10) ){ Component.Apex.InputField fieldComponent = new Component.Apex.InputField(); if(lc.IsRequired__c) { fieldComponent = new Component.Apex.InputField(required = true); } fieldComponent.expressions.value = '{!opp.' + lc.FieldName__c + '}'; componentMap.get(lc.ParentComponent__c).childComponents.add(fieldComponent); }else{ Component.Apex.OutputField fieldComponent = new Component.Apex.OutputField(); fieldComponent.expressions.value = '{!opp.' + lc.FieldName__c + '}'; componentMap.get(lc.ParentComponent__c).childComponents.add(fieldComponent); } /*Component.Apex.OutputText textComponent = new Component.Apex.OutputText(); textComponent.value = lc.Order__c + ': ' + lc.FieldName__c; componentMap.get(lc.ParentComponent__c).childComponents.add(textComponent);*/ System.debug('* ** *** adding field ' + lc.FieldName__c); fieldNames.add(lc.FieldName__c); }else if(lc.IsBlank__c){ Component.Apex.PageBlockSectionItem blankComponent = new Component.Apex.PageBlockSectionItem(); componentMap.get(lc.ParentComponent__c).childComponents.add(blankComponent); } }//end-for for(Id sectionId : sectionIds){ dynBlock.childComponents.add(componentMap.get(sectionId)); } if(!fieldNames.isEmpty()){ for(String fieldName : fieldNames){ if(pageLayoutFields == '') { pageLayoutFields = fieldName; } else { pageLayoutFields += ', ' + fieldName; } } opp = Database.query('SELECT ' + pageLayoutFields + ' FROM Opportunity WHERE Id = \'' + oppId + '\' LIMIT 1'); getCloneSettings(); opp.OwnerId = UserInfo.getUserId(); } } return dynBlock; } }Thank you!