Sunday, 6 September 2015

Global Describe in salesforce



Map<String, Schema.SObjectType> mapSchemaSobjectType = Schema.getGlobalDescribe();

Set<String> setSobjets = new Set<String>();

for(Schema.SObjectType d :mapSchemaSobjectType.values())
{
       Schema.DescribeSObjectResult dSobject = d.getDescribe();
       setSobjets.add(dSobject.getName());
}

setSobjets This set will contain all the SObject name.

-----------------------------------------------------------------------------------------------------

Map<String, Schema.SObjectType> mapSchemaSObjectType = Schema.getGlobalDescribe();
Set<String> standardObjects = new Set<String>();
Set<String> customObjects = new Set<String>();
for(Schema.SObjectType objSchemaSobjectType : mapSchemaSObjectType.values())
{
    Schema.DescribeSObjectResult objResult = objSchemaSobjectType.getDescribe();
    if(!objResult.isCreateable())
      continue;
    if(objResult.isCustom() == false && objResult.getRecordTypeInfos().size() > 0)
        standardObjects.add(objResult.getName());
    else if(objResult.isCustom())
        customObjects.add(objResult.getName());
}

standardObjects - Contains set of standard objects.
customObjects - Contains set of Custom objects

Override SObject list view with Visualforce page


Create a visualforce page with standard controller and recordSetVar attribute of <apex:page>

Based on your Sobject change the StandardCopntroller and recordsetvar value.


<apex:page recordsetvar="Accounts" standardcontroller="account" tabstyle="account">
  <apex:enhancedlist customizable="True" height="600" id="YourListViewId" rowsperpage="10" type="Account">
  </apex:enhancedlist>
</apex:page>


Once visualforce page is created.
Goto Sobject detail page and goto Buttons, Links, and Actions section.
Now click edit next to list now you can see like below image


Now check radio button next to Visualforce page and select your visualforce page.
final save the changes.

Now your Sobject list view is overridden.





Add a list view button for deleting records in Salesforce



Create a javascript list view button











{!requirescript("/soap/ajax/28.0/connection.js")}
{!requirescript("/soap/ajax/28.0/apex.js")}

sforce.connection.deleteIds({!GETRECORDIDS($ObjectType.Account)});
location.reload();

 
 Instead of Account use your object name and add this button in object's search layout --> list view.

Sunday, 21 June 2015

Execute Territory Assignment Rules when update or insert Accounts.


Account Assignment Rules in Salesforce.com via webservice

To run the Territory rule assignment
We need to use webservice call out

To do this, we must import the partner wsdl and generate helper apex classes.

http://developer.force.com/cookbook/recipe/calling-salesforce-web-services-using-apex


And then after insert or update of Account, pass the all accounts in fireTerritoryManagementViaSoap method.

global class TerritoryMangRule
{    
    webService static void fireTerritoryManagementViaSoap(List<Account> lstAccountToUpdate)
    {
        partnerSoapSforceCom.Soap obj = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult loginResult = obj.login('org@username.com', 'Password');  
        obj.SessionHeader = new partnerSoapSforceCom.SessionHeader_element();
        obj.endpoint_x =loginResult.ServerUrl;
        obj.Sessionheader.sessionid = loginResult.sessionid;
        List<sobjectPartnerSoapSforceCom.sObject_x> lst = new List<sobjectPartnerSoapSforceCom.sObject_x>();
        for(Account ac:lstAccountToUpdate)
        {
            sobjectPartnerSoapSforceCom.sObject_x tmpObj = new sobjectPartnerSoapSforceCom.sObject_x();
            tmpObj.type_x = 'Account';
            tmpObj.Id = ac.Id;
            lst.add(tmpObj);
        }
        partnerSoapSforceCom.AssignmentRuleHeader_element  obj1 = new partnerSoapSforceCom.AssignmentRuleHeader_element();
        obj1.useDefaultRule = true;
        obj.AssignmentRuleHeader = obj1;
        partnerSoapSforceCom.SaveResult[] lst1 =obj.update_x(lst);
    }
}