Monday, 15 February 2016

Manual Sharing for Partner community using Apex



Salesforce

This topic is related to the sharing in communities. I need to setup the sharing on partner communities and there is no point and click option for sharing any object for partner communities object. First you should setup the communities in the ORG than go to the setupàcommunity settings and add a new sharing sets, see the below image 1


In above red highlighted box, you would not able to see the partner community profiles, so this is really a main point for which I used manual sharing by apex code. We can add a sharing rule for customer community user license. So if we are making customer community than this would be pretty mush simple to configure. Now moving forward, my task was to share a newly created case in the community only for same account, Means let’s say if I have created a Case1 and adding this case against any contact, obviously Account is there for that contact, so this Case1 should be share among all those community user which have that same Account Id and if another account user login to the community than that Case1 should not be seen for that User. So this is the user case which I need to follow here. So I have made a trigger on case and on that trigger, newly created case should be share with all those user which are of same account. Please keep this in mind if let’s say we update that case and link that case with another Account than we must un-share the case with previous users and that case would be share with new community user. So, I have just written a sample here which is perfect for the created case if we change the Account on updating than few more lines needs to be added here, please see the below trigger code here.

trigger trShareCase on Case (after insert, after update) 
{
    map<Id,Id> mCase_Account = new map<Id,Id>();
    map<Id,Id> mUser_Account = new map<Id,Id>();
    map<Id,List<Id>> mCase1 = new map<Id,List<Id>>();
    Map<Id,List<User>> mUser = new Map<Id,List<User>>();
            for(Case cs:trigger.new)
            {
                        if (cs.accountid != null)
                        {
                                    mCase_Account.put(cs.Id,cs.accountid);
                        }
            }

            for (user u : [select Id,AccountId from user where IsActive= true And AccountId in :mCase_Account.values()])
            {
              mUser_Account.put(u.Id,u.AccountId);
            }
           
            List<Id> liUser ;
            for(Id caseId: mCase_Account.keyset())
            {
                         for(Id userId: mUser_Account.keyset())
                         {
                                    if(mCase_Account.get(caseId) == mUser_Account.get(userId))
                                    {
                                        if (!mCase1.containsKey(caseId))
                                                {
                                                    liUser = new List<Id>();
                                                    liUser.add(userId);
                                                            mCase1.put(caseId,liUser);
                                                }
                                                else
                                                {
                                                    liUser = new List<Id>();
                                                            liUser = mCase1.get(caseId);
                                                            liUser.add(userId);
                                                            mCase1.put(caseId,liUser);
                                                }
                                    }
                         }

            }
            List<CaseShare> csShareList = new List<CaseShare>();
            Id TestId ;
            for(Case cs:trigger.new)
            {
              
               for (Id caseId: mCase1.keyset())
               {
                 if (cs.Id == caseId)
                         {
                            liUser = mCase1.get(caseId);
                                    for (Id UserId: liUser)
                                    {
                                                CaseShare csShare = new CaseShare();
                                                // Give Read write access to that user for this particular case record.
                                                csShare.CaseAccessLevel = 'Read';
                                                // Assign case Id of case record.
                                                csShare.CaseId = cs.id;
                                                // Assign user id to grant read write access to this particular case record.
                                                TestId = UserId;
                                                csShare.UserOrGroupId = TestId;
                                                //csShare.RowCause = Schema.Project__Share.RowCause.Manual;
                                                csShareList.add( csShare );
                                    }
                         
                         }

               }

              
            }
           
            insert csShareList;
}
In above code, first yellow highlighted code is showing a map which consist Case Id and Account Id, second green highlighted code showing map in which it is saving User Id and Account Id, and I used SOQL to fetched all those user which have Account Ids from first map value. In third gray highlighted code, it is than maintaining very important map. This map contains a signature of Case Id and List of user Ids. So I am using 2 for loops which actually maintain User list with single case, if the map do not contain any key than it would first add the value in the map with Case Id as key and List of User Id and in second if condition, if the same key exists than it is altering the map value against same key that is case id. Last but not least in the later part the case sharing object comes into play and adding the entries into the list having 1 to many relation means 1 case might contains one or multiple users.
The Community plus license also needs the same way of manual sharing but if you want to play the same game against customer community user license than go to setpàcommunity settings and define the sharing set and add the same rule means Case1 should be share against all those user which are under the umbrella of same Account, see below image for your help

So this is the case of adding any new case, let’s say if we are making any new partner community user than what should we do? Because we have earlier worked on Case object. So, for this we need to add a trigger on User as well but the important punch over here is that if we add a trigger on user and on that trigger if we are manually share the object like Case, so if User is added and then hit that trigger, it would give an exception of MIXED_DML_OPEARATION, reason being we are getting the exception because the same time we are using the System object that is user for DML operation and on the other way we are using non-system object which is CaseSharing object, so it would create an issue. So to handle this we should need some time difference between these 2 operation, so I have made a custom formula date time field in user and set the time 55 minutes before the created date. See below image


Why I added this field because I want to add a time base workflow and in time base workflow, it need minimum an hours to execute. I have also added a custom field in user object, a checkbox “Show in Community” , so if the workflow executes after 5 min than it would update this checkbox field and on update the user record than the trigger would execute on user. So the purpose of this so to give some time elapse between user object creation and case sharing object. See the below time dependent workflow snapshot.



The rule criteria is mentioned in red box which show that if the custom field is not checked than it would enter into the workflow. Secondly, the red box showing 1 hour after the custom date time formula field, we do not have any option to execute the workflow under an hour. Third one the field update, it would mark check on the custom checkbox field. Immediately if it mark the checkbox than it would hit the trigger and trigger will share the object with the newly created user. I am not writing the User trigger here as this trigger would be prettly kuch similar to the above trigger, only the difference would be that in this trigger, we should maintain the final map with User Id and list of case, the opposite logic to the above trigger, so happy coding guys J

Wednesday, 13 January 2016

Removing "unsaved changes" from MS dynamics online forms after saving



MS Dynamics CRM online

This topic is related to the dirty fields in MS dynamics CRM online forms, if user save any changes and after saving he would see the “Unsaved Changes” at the left bottom part of the page even if all the fields are saved, in my case I have used Javascript on both the load and save event also I have workflow activated behind every entity so the option in Administration area for Auto save is set to NO in my configuration. Because if we have workflows and JS behind the entity that is why we don’t allow Auto save functionality.
Now, it was real irritating for me to find the solution for this thing. At first stage, I tried to disable all the Javascript on my entity because I found this solution in most of the blogs and post. So I did this but that was useless for me. So, I found that there are some dirty fields in the entities which are causing the issue and due to these dirty fields, the form could not save and showing “Unsaved Changes” like below image


So, I have set the setSubmitMode to “never” in this case on page load Javascript. But the thing is that, if we set these fields as “never” submit mode than these fields won’t save into the database on saving event. And in my case, there are few fields which needs to be save. So what is observed during the task is that, if we set the submit mode to “never” on page load javascript and again just before saving through javascript if we again set the submit mode to “always” than it solve my issue because on page loading, MS dynamics page identify the dirty fields and if we set these fields to “never” than it won’t  affect the “unsaved changes” issue. I know this is trick but this is the only way I found the solution. So I have made a dynamic javascript which accept the submit mode and on load event, I set it to “never” and on saving it accept the “always” mode. See the below Javascript
function ClearDirtyFields(mode)
{
   var attributes = Xrm.Page.data.entity.attributes.get();
   for (var i=0;i<attributes.length ;i++)
   {
       if (attributes[i].getName() == "new_guid")
       {
          attributes[i].setSubmitMode(mode);
       }
               if (attributes[i].getName() == "custom_Membership")
       {
          attributes[i].setSubmitMode(mode);
       }
    }          
}

In above Javascript,, I explicitly set the field’s submit mode using for loop. Remember, set the “never” submit mode on page load javascript and “always” on save event javascript.

Monday, 28 December 2015

Month wise data distribution using Google Chart

Salesforce

This topic is related to the charting on Account, I need to show the chart in which the scenario is to show the Order total against account on monthly basis. For example, 10 Orders are made in December 2015, 15 Order in November and 30 Orders in October. So if user go to any Account than there would be Chart which would show these statistics. So I have a made a custom VF page in which I showed this chart using Google Chart. So Let’s start.

I have a made a custom button in Account page layout and if user click this custom button than it would redirect to the custom VF page taking Account Id and current Year in query string. If you see below image, you would that custom button on Account


Picture A



If you click this button on Account detail page than it would open this below custom VF Page

Picture B


This above picture show the Order Total sum month wise. I have made a 3 custom field in Order

1)    Order Month (Formula fieldà Month(EffectiveDate))   [Integer Type]
2)    Order Year    (Formula fieldà Year(EffectiveDate))     [Integer Type]
3)    Order Month String  (Text Field).

1st two fields are the formula field which returns Number of Month and Year. The 3rd field is tricky one, in this field we need the Month Name like “December”, so this field contains the data using Workflow, in workflow, I have set this string field using formula, if the Order Month (formula field) contains 12 than set the “December” text in this field, see below formula image taken from workflow


Picture 3



So this way, I have reached to that position in which SOQL will return that data which will pass into the Google chart to show Chart distribution. Please see the below apex page code.
<apex:page controller="GoogleChartsController" sidebar="false">
    <!-- Google API inclusion -->
<apex:form >
    <apex:includeScript id="a" value="https://www.google.com/jsapi" />

 <span>Select Year:</span><apex:selectList value="{!selectedYear}" multiselect="false" size="1">
                <apex:selectOption itemValue="2015" itemLabel="2015"/>
                <apex:selectOption itemValue="2014" itemLabel="2014"/>
                <apex:selectOption itemValue="2013" itemLabel="2013"/>
                <apex:selectOption itemValue="2012" itemLabel="2012"/>
                <apex:selectOption itemValue="2011" itemLabel="2011"/>
     </apex:selectList>
           <apex:commandButton action="{!Generate}" value="Generate Report" id="theButton"/>
 <div id="chartBlock" />
    
    <script type="text/javascript">
   
    function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});
      
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(initCharts);
  
        function initCharts() {        
          // Following the usual Remoting syntax
          // [<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {...}
          // controller : GoogleChartsController
          // method : loadOpps
          var Id = getUrlVars()["Id"];
          var Year = getUrlVars()["Year"];
          GoogleChartsController.loadOpps(Id,Year,
                 function(result, event){ 
                     // load Column chart
                     var visualization = new google.visualization.ColumnChart(document.getElementById('chartBlock'));
                     // Prepare table model for chart with columns
                     var data = new google.visualization.DataTable();
                     data.addColumn('string', 'Order');
                     data.addColumn('number', 'Order Amount');
//                     data.addColumn('number', 'Amount');   
                     // add rows from the remoting results
                     for(var i =0; i<result.length;i++){
                        var r = result[i];
                        data.addRow([r.OMonth__c, r.OrderTotal]);
                      }
                    // all done, lets draw the chart with some options to make it look nice.
                    visualization.draw(data, {legend : {position: 'top', textStyle: {color: 'blue', fontSize: 10}}, width:window.innerWidth,vAxis:{textStyle:{fontSize: 10}},hAxis:{textStyle:{fontSize: 10},showTextEvery:1,slantedText:false}});
              }, {escape:true});
          }
    </script>

</apex:form>
</apex:page>

In above yellow highlighted area, I am fetching the Query strings in javascript and pass these query string values in GoogleChartsController.loadOpps in above yellow highlighted area. This LoadOpps is actually is a remote action defined in controller of this apex page. Now moving towards the controller, see the controller code below.

global with sharing class GoogleChartsController {
public String selectedYear{get;set;}

public GoogleChartsController()
{
    selectedYear = ApexPages.currentPage().getParameters().get('Year');
}
public PageReference Generate()
{
    PageReference nextPage = new PageReference('/apex/GoogleChartsController?Id=' + ApexPages.currentPage().getParameters().get('Id') + '&Year=' + selectedYear);
    nextPage.setRedirect(true);
    return nextPage;
   

}
    /**
      Loads Orders
    */
    @RemoteAction  
    global static List<AggregateResult> loadOpps(String Id,decimal Year) {
        //return [select Id, Name, ExpectedRevenue, Amount from Opportunity order by CreatedDate DESC limit 10];
        List<AggregateResult> li= [SELECT OMonth__c, sum(TotalAmount) OrderTotal FROM Order where AccountId = :Id and Order_Year__c = :Year GROUP BY OMonth__c];
        return li;
    }  

}

The query I am using in above controller code is actually returning that data which I need and this SOQL based on some filter criteria. This way this page show the Order Statistics using google chart.