Tuesday, 9 August 2016

How to get all the RecordType Names and Id of Particular Object using SOQL

SELECT RecordType.Id, RecordType.Name  FROM <SOBJECT NAME>

OR

SELECT Id, Name FROM RecordType WHERE sobjectType = '<SOBJECT NAME>'

Thursday, 28 July 2016

A Trigger which doesn't allow Opportunity Stage to be changed if there are open task on that opportunity's related list

trigger CheckOpenActivityHasTaskInit on Opportunity (after insert,after update) 
{
   Set<String> OppoId = new Set<String>();
   List<Opportunity> oppp= new List<Opportunity>();
   for(Opportunity Opp : Trigger.New)
   {    
         List<AggregateResult> tsk =
         [SELECT Count(Id)countTask FROM Task WHERE IsClosed=false            and WhatId =:opp.Id];
         Integer tskCount = (Integer)tsk[0].get('countTask');       
         if(tskCount > 0)
         {
               opp.addError('To Procede Further Please! Close The                  Open Activities First...');
          }
     } 
}

Get The Three Digit Key of Sobjects in the Salesforce

List <String> stdObj = new List<String>();
for(Schema.SoapType typ:Schema.getGlobalDescribe().value()){
    String SObjectName = String.valueOf(typ);
    if(!SObjectName.contains('__c')){
        stdObj.add(SObjectName);
    }
}
Integer count=1;
for(String sname : stdObj){
    Schema.SoapType convertType = Schema.getGlobalDescribe().get(sname);
    Schema.DescribeSObjectResult res = convertType.getDescribe();
    String keyPrefix = res.getKeyPrefix();
    if(keyPrefix != null){
        System.debug(keyPrefix+' = '+sname);
    }
}