• srashti jain 10
  • NEWBIE
  • 25 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
public with sharing class DataTableController {
    
    @AuraEnabled
    public static DataTableWrapper initRecords(String ObjectName, String fieldSetName, String Orderby, String OrderDir) {
        
        DataTableWrapper dtw = new DataTableWrapper();
        List<LabelDescriptionWrapper> labelList = new List<LabelDescriptionWrapper>();
        List<String> fieldSet = new List<String>();
        Set<String> fieldNameSet = new Set<String>(getFieldSet(ObjectName, fieldSetName).split(','));
        system.debug('fieldNameSet11'+fieldNameSet);
        
        if(Schema.getGlobalDescribe().containsKey(ObjectName) ) {
            //sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
            //system.debug('sObj+++++++++++++++++++'+sObj);
            
            //get all the labels for sObject fields and put them in a map, keyed to the field api name
            Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
            //fieldmap contains all fields name of an sObject in proper naming case.
            system.debug('fieldMap++++++++'+fieldMap);
            Map<Schema.SObjectField,String> fieldToAPIName = new Map<Schema.SObjectField,String>();
            system.debug('fieldToAPIName+++++++++'+fieldToAPIName);
            Map<String, String> apiNameToLabel = new Map<String, String>();
            Boolean loopFlag = false;
            for(String fieldName :  fieldNameSet){
                if(loopFlag == false && fieldMap.containsKey(fieldName)){
                    fieldSet.add(fieldName);
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName == 'Name' ? 'LinkName' : fieldName,  
                                                              'url',
                                                              new typeAttributesWR(new labelWR(fieldName), '_self'),
                                                              true));
                    loopFlag = true; 
                    continue;
                }
                if(fieldMap.containsKey(fieldName)) {
                    fieldSet.add(fieldName);
                    //labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), fieldName, fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), true ));
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName, 
                                                              'text', null,
                                                              //fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), 
                                                              true ));
                }
            }
            
            //call method to query
            List<sObject> sObjectRecords = getsObjectRecords(ObjectName, fieldSet, 20, '', Orderby, OrderDir);
            system.debug('labelList '+labelList);
            dtw.ldwList     = labelList;
            dtw.sobList     = sObjectRecords;
            dtw.fieldsList     = fieldSet;
            dtw.totalCount  = Database.countQuery('SELECT count() FROM '+ObjectName);
        }
        system.debug('dtw+++++++'+dtw);
        //returns wrapper datatype and fieldset fields properties.
        return dtw;
    }
    
    @AuraEnabled
    public static List<sObject> getsObjectRecords(String ObjectName, List<String> fieldNameSet, Integer LimitSize, String recId, String Orderby, String OrderDir) {
        
        OrderDir = String.isBlank(OrderDir) ? 'asc' : OrderDir;
        String query = 'SELECT '+String.join(fieldNameSet, ',')+' FROM '+ObjectName;
        if(String.isNotBlank(recId)) {
            recId = String.valueOf(recId);
            query += ' WHERE ID >: recId ';
        }
        
        query += ' ORDER BY '+Orderby+' '+OrderDir+' NULLS LAST';
        
        if(LimitSize != null && Integer.valueOf(LimitSize) > 0) {
            LimitSize = Integer.valueOf(LimitSize);
            query += ' Limit '+LimitSize;
        }
        system.debug('query++++++++++'+query);
        system.debug('query++++++++++'+Database.query(query));
        //this will return all the sObject records with all fieldset fields values.
        return Database.query(query);
    }
    
    @AuraEnabled
    //get api name of the fields of a fieldset
    public static String getFieldSet(String sObjectName, String fieldSetName) {
        String result = '';
        SObjectType objToken = Schema.getGlobalDescribe().get(sObjectName);//metadata 
        Schema.DescribeSObjectResult d = objToken.getDescribe();
        Map<String, Schema.FieldSet> FieldsetMap = d.fieldSets.getMap();//<fset name, label>
        system.debug('objToken+++++++++'+objToken);//returns the name of the sobject.
        if(FieldsetMap.containsKey(fieldSetName))
            for(Schema.FieldSetMember f : FieldsetMap.get(fieldSetName).getFields()) {
                if(string.isNotBlank(result)){
                    result += ',';
                }
                result += f.getFieldPath();
            }
        system.debug('result *****'+result);
        return result ;//returns the name of the fields in  a fieldset.
       
    }

    public class DataTableWrapper {
        @AuraEnabled
        public List<LabelDescriptionWrapper> ldwList;
        @AuraEnabled
        public List<sObject> sobList;
        @AuraEnabled
        public List<String> fieldsList;
        @AuraEnabled
        public Integer totalCount;
    }
    
    public class LabelDescriptionWrapper {
        @AuraEnabled
        public String label;
        @AuraEnabled
        public String fieldName;
        @AuraEnabled
        public String type;
        @AuraEnabled
        public typeAttributesWR typeAttributes;
        @AuraEnabled
        public boolean sortable;        
        
        public LabelDescriptionWrapper(String labelTemp, String fieldNameTemp, String typeTemp,  typeAttributesWR typeAttributesTemp, boolean sortableTemp) {
            label       = labelTemp;
            fieldName = fieldNameTemp;
            type       = typeTemp;
            typeAttributes = typeAttributesTemp;
            sortable  = sortableTemp;            
        }            
    } // end LabelDescriptionWrapper
    public class labelWR {
        @AuraEnabled
        public String fieldName;
        public labelWR(String fieldNameTemp){
            fieldName = fieldNameTemp;
        } 
    }
    
    public class typeAttributesWR {
        @AuraEnabled
        public labelWR label;
        @AuraEnabled
        public String target;
        
        public typeAttributesWR(labelWR labelTemp, String targetTemp){
            label = labelTemp;
            target = targetTemp;
        } 
    }
}
I need an urgent help..!
Create the lightning component which show the selected Accounts from the list view and on click of send button sends the email to selected Account's email id. (here a Email is a custom field on account)
use email template
for email content.

Please post the code..

Thankyou..!
there is a custom object 'Amount' with number value field 'Amnt' which is related to account by lookup. There is a field on account- 'Amt' it should show the sum of amount if the sum of amount is greater then 100 then it should not allow to insert any child record for a perticular account and should give an error- 'you have reached a maximum limit'
public with sharing class DataTableController {
    
    @AuraEnabled
    public static DataTableWrapper initRecords(String ObjectName, String fieldSetName, String Orderby, String OrderDir) {
        
        DataTableWrapper dtw = new DataTableWrapper();
        List<LabelDescriptionWrapper> labelList = new List<LabelDescriptionWrapper>();
        List<String> fieldSet = new List<String>();
        Set<String> fieldNameSet = new Set<String>(getFieldSet(ObjectName, fieldSetName).split(','));
        system.debug('fieldNameSet11'+fieldNameSet);
        
        if(Schema.getGlobalDescribe().containsKey(ObjectName) ) {
            //sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
            //system.debug('sObj+++++++++++++++++++'+sObj);
            
            //get all the labels for sObject fields and put them in a map, keyed to the field api name
            Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
            //fieldmap contains all fields name of an sObject in proper naming case.
            system.debug('fieldMap++++++++'+fieldMap);
            Map<Schema.SObjectField,String> fieldToAPIName = new Map<Schema.SObjectField,String>();
            system.debug('fieldToAPIName+++++++++'+fieldToAPIName);
            Map<String, String> apiNameToLabel = new Map<String, String>();
            Boolean loopFlag = false;
            for(String fieldName :  fieldNameSet){
                if(loopFlag == false && fieldMap.containsKey(fieldName)){
                    fieldSet.add(fieldName);
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName == 'Name' ? 'LinkName' : fieldName,  
                                                              'url',
                                                              new typeAttributesWR(new labelWR(fieldName), '_self'),
                                                              true));
                    loopFlag = true; 
                    continue;
                }
                if(fieldMap.containsKey(fieldName)) {
                    fieldSet.add(fieldName);
                    //labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), fieldName, fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), true ));
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName, 
                                                              'text', null,
                                                              //fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), 
                                                              true ));
                }
            }
            
            //call method to query
            List<sObject> sObjectRecords = getsObjectRecords(ObjectName, fieldSet, 20, '', Orderby, OrderDir);
            system.debug('labelList '+labelList);
            dtw.ldwList     = labelList;
            dtw.sobList     = sObjectRecords;
            dtw.fieldsList     = fieldSet;
            dtw.totalCount  = Database.countQuery('SELECT count() FROM '+ObjectName);
        }
        system.debug('dtw+++++++'+dtw);
        //returns wrapper datatype and fieldset fields properties.
        return dtw;
    }
    
    @AuraEnabled
    public static List<sObject> getsObjectRecords(String ObjectName, List<String> fieldNameSet, Integer LimitSize, String recId, String Orderby, String OrderDir) {
        
        OrderDir = String.isBlank(OrderDir) ? 'asc' : OrderDir;
        String query = 'SELECT '+String.join(fieldNameSet, ',')+' FROM '+ObjectName;
        if(String.isNotBlank(recId)) {
            recId = String.valueOf(recId);
            query += ' WHERE ID >: recId ';
        }
        
        query += ' ORDER BY '+Orderby+' '+OrderDir+' NULLS LAST';
        
        if(LimitSize != null && Integer.valueOf(LimitSize) > 0) {
            LimitSize = Integer.valueOf(LimitSize);
            query += ' Limit '+LimitSize;
        }
        system.debug('query++++++++++'+query);
        system.debug('query++++++++++'+Database.query(query));
        //this will return all the sObject records with all fieldset fields values.
        return Database.query(query);
    }
    
    @AuraEnabled
    //get api name of the fields of a fieldset
    public static String getFieldSet(String sObjectName, String fieldSetName) {
        String result = '';
        SObjectType objToken = Schema.getGlobalDescribe().get(sObjectName);//metadata 
        Schema.DescribeSObjectResult d = objToken.getDescribe();
        Map<String, Schema.FieldSet> FieldsetMap = d.fieldSets.getMap();//<fset name, label>
        system.debug('objToken+++++++++'+objToken);//returns the name of the sobject.
        if(FieldsetMap.containsKey(fieldSetName))
            for(Schema.FieldSetMember f : FieldsetMap.get(fieldSetName).getFields()) {
                if(string.isNotBlank(result)){
                    result += ',';
                }
                result += f.getFieldPath();
            }
        system.debug('result *****'+result);
        return result ;//returns the name of the fields in  a fieldset.
       
    }

    public class DataTableWrapper {
        @AuraEnabled
        public List<LabelDescriptionWrapper> ldwList;
        @AuraEnabled
        public List<sObject> sobList;
        @AuraEnabled
        public List<String> fieldsList;
        @AuraEnabled
        public Integer totalCount;
    }
    
    public class LabelDescriptionWrapper {
        @AuraEnabled
        public String label;
        @AuraEnabled
        public String fieldName;
        @AuraEnabled
        public String type;
        @AuraEnabled
        public typeAttributesWR typeAttributes;
        @AuraEnabled
        public boolean sortable;        
        
        public LabelDescriptionWrapper(String labelTemp, String fieldNameTemp, String typeTemp,  typeAttributesWR typeAttributesTemp, boolean sortableTemp) {
            label       = labelTemp;
            fieldName = fieldNameTemp;
            type       = typeTemp;
            typeAttributes = typeAttributesTemp;
            sortable  = sortableTemp;            
        }            
    } // end LabelDescriptionWrapper
    public class labelWR {
        @AuraEnabled
        public String fieldName;
        public labelWR(String fieldNameTemp){
            fieldName = fieldNameTemp;
        } 
    }
    
    public class typeAttributesWR {
        @AuraEnabled
        public labelWR label;
        @AuraEnabled
        public String target;
        
        public typeAttributesWR(labelWR labelTemp, String targetTemp){
            label = labelTemp;
            target = targetTemp;
        } 
    }
}
there is a custom object 'Amount' with number value field 'Amnt' which is related to account by lookup. There is a field on account- 'Amt' it should show the sum of amount if the sum of amount is greater then 100 then it should not allow to insert any child record for a perticular account and should give an error- 'you have reached a maximum limit'
Hi All,

I'm a bit confused on this. if we have a standard user profile with (CRED) on all objects and OWD set for private (let's say on Opportunity object)

Will the standard user be able to CRED on any opportunity record (owned or not-owned)? or will they have zero access to opportunity records they do not own?

I was under the impression that the user will have zero access to CRED any records they do not own. Unless permitted by role hierachy or sharing rules.

Thanks alot everyone

Pedro

I have a site for self-registration which calls Site.createPortalUser()  to create a new portal user if it is a new user. It works perfectly on site registration page. The problem now is that I have to capture facebook login and create portal user for facebook users. Current code has callout function to get facebook user's email, userid and username, etc. And then, pass those info to the same function used for site registration page. All parameters are correct, just don't know why Site.createPortalUser()  always give me null userId? Anyone has any idea about this?

  • November 29, 2011
  • Like
  • 0