Saturday, May 21, 2016

Custom Objects and their fields from CSV to SF via apex

Generally customers give us a excel sheet for the custom objects and the custom fields. It take too much time to create them manually.
So I have tried a code with the help of meta data api class , which can create all the custom objects and custom fields from CSV to SF in a single click.

There are two standard objects in SF which can be accessed via apex
1. CustomObject
     
MetadataService.MetadataPort service = new MetadataService.MetadataPort();    
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId(); 
        
        List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
        
        for(String s : objSet){
            MetadataService.CustomObject customObject = new MetadataService.CustomObject();
            customObject.fullName = s+'__c';
            customObject.label = s;
            customObject.pluralLabel = s+'s';
            customObject.nameField = new MetadataService.CustomField();
            customObject.nameField.type_x = 'Text';
            customObject.nameField.label = 'Name';
            customObject.deploymentStatus = 'Deployed';
            customObject.sharingModel = 'ReadWrite';
            fields.add(customobject);
        }
        List<MetadataService.SaveResult> results = service.createMetadata(fields);



2. CustomFields
      
MetadataService.MetadataPort service = new MetadataService.MetadataPort();   
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId(); 
        
        List<MetadataService.Metadata> fields1 = new List<MetadataService.Metadata>();
        
        for(String s : multField){
           MetadataService.CustomField customField = new MetadataService.CustomField(); 
           customField.fullName = s.substringBefore('-')+'__c.'+s.substringBetween('&')+'__c';
           customField.label = s.substringBetween('&');
           if(s.substringAfter('#') == 'Text'){
               customField.type_x = 'Text';
               customField.length = 42;
           }else if(s.substringAfter('#') == 'Number'){
               customField.type_x = 'Number';
               customField.precision = 18;
               customField.scale = 2;
           }else if(s.substringAfter('#') == 'Checkbox'){
               customField.type_x = 'Checkbox';
               customField.defaultvalue  = 'false';
           }else if(s.substringAfter('#') == 'Email'){
               customField.type_x = 'Email';
           }else if(s.substringAfter('#') == 'Currency'){
               customField.type_x = 'Currency';
               customField.precision = 18;
               customField.scale = 2;
           }
           fields1.add(customField);
        }  
        List<MetadataService.SaveResult> results = service.createMetadata(fields1);


So we need to do following steps.....

A. The sheet should be in a csv format because it returns a string.
   


B. Select the file which you want to create

C. Click Ok to create objects and their related fields.



So if you need the full code or demo then please let me know.


Thanks
Vikas Gaur


Saturday, May 14, 2016

Analog Clock

                                                     "Time is money"

When you are working in any salesforce org then  one thing you have in your mind that is TIME. I will say that current time of the org or current time of login user or particular time zone or GMT. So we have following OOTB in salesforce to get the time

1. System.now();
2. System.today();
3. Time zone of login user based on UserInfo.getUserId()
5. Formula field
6. Workflow
7. Some other also

What if we have a analog clock for the same at home page as following


So I tried to build to an application which will work for this requirement. For now this working based on the GMT. It can be extended for login user or particular time zone. 
We can see working demo on following link

http://vickygaur-developer-edition.ap1.force.com/Demo/AnalogClock

So when we add this link in home page component and page layout , then it will be visible at location.

As always please let me know for suggestions and sample code. 


~Vikas Gaur 
  


Saturday, April 30, 2016

Weather Report And Forecasting In Salesforce

As we know that a weather report can only be given for a past period and gives the actual weather conditions that  already occurred and calculated on the particular time.

So in salesforce it may be an application or customer requirement to get the weather report with forecasting. There may be two scenario where we may need weather report in salesforce.

1. Weather report of login user : -
                                                Salesforce org is the collections of multiple users. So when a user gets login then on home page a section of weather report can be displayed.
This is also possible in community, portal and lightning.


2. Weather report for given city:- 
                                             We can provide a input section where the user will enter the city name and will get the corresponding weather report.

 
So I have tried to implement same kinda in salesforce which has following things

 A. Platform - Salesforce
 B. API - Yahoo
 C. API Type - Rest
 D. Weather Information - Temperature, Humidity, Pressure, Rising, Visibility
 E. Forecasting - For next 10 days 

Demo - http://vickygaur-developer-edition.ap1.force.com/Demo/WeatherChecker

Please let me know for some suggestions and if need of sample code.


Thanks
Vikas Gaur
excellencegaur@gmail.com
9990540382