Wednesday, April 26, 2017

Salesforce beginner learning materials

Trailhead main page - https://trailhead.salesforce.com

Force.com Fundamentals [ HTML | PDF ];
Visualforce in Practice [ PDF ];

Limits:

Useful links:

Sunday, October 19, 2014

Salesforce.com Certified Force.com Developer

Finally I have found time to pass developer certification.

In this post I will gather all useful links and docs that helped me in preparation to exam.

Books:

  • Force.com Developer Certification Handbook 2012 by Siddhesh Kabe - every person that wants to pass exam definitely needs to read this book. Here you can find answers to almost all questions about certification. Also this book will be useful for those who are starting learning Salesforce.

Materials:

Flashcards:

Tuesday, January 14, 2014

Exotic SF objects with properties.

This is list of exotic SF objects and their properties. List will be growing, as I will add new after "playing".

API Name
Prefix
Trigger
DML
SOQL
REST
Describe
CurrencyType
01L
No
No DML [1]
+
+
DatedConversionRate
04w
No
No DML [1]
+
POST / DELETE
+
Application
06P
No
No DML
No SOQL
No describe
AppManifes
0Ci
No
No DML
No SOQL
No describe
AccountTerritoryAssignmentRule
-
No
No DML [1]
No SOQL
No describe


References:

1. sObjects That Don’t Support DML Operations;

Enjoy.

How to get today's exchange rate and historical data.

To get today's exchange rate we need to go to yahoo console. Query "yahoo.finance.xchange" object with pairs of currencies we want. Link with query to get today's exchange rate between pairs EUR-USD and GBP-USD from Yahoo:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22EURUSD%22%2C%22GBPUSD%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

URL is easy to parse and to create. It will return JSON with needed rates.

But getting historical data is a little bit harder. Yahoo do not give us historical data of currency. But on Yahoo website exists page Currency Converter, that have exchange rate for any currency for any date. In the end of page there is note that data is taken from another sources: Commodity Systems and Thomson Financial Network. Analyzing network data that is sent to server we found URL:

http://finance.yahoo.com/connection/currency-converter-cache?date=20140102

This link returns us JSON object of exchange rate all currencies to USD. You can change parameter 'date' in URL and get rate for specific date.

Enjoy.

Monday, July 22, 2013

Re-connecting records through S2S connection.

How to reconnect two records that where connected with S2S connection, and it was deleted? Or, for example, we have two records in two org, how to connect them with S2S without creation new ones? This is quick quid how to do it.

We have two orgs: Org1 and Org2, and two Accounts: "Salesforce.com Inc." from Org1 and "Salesforce.com Inc." from Org 2. We need to synchronize with S2S connection. We will share from Org1 to Org2.
  1. Going to Org2 "Connections" tab:
  2. Selecting needed connection;
  3. Press "Subscribe/Unsubscribe":
  4. Uncheck "Auto-Accept Accounts":
  5. Going to Org1 to needed Account and share it:
  6. Going to Org2 Account tab;
  7. In the bottom there is block "Accounts from Connections", select "All" and press "Go":
    Accounts from Connections
  8. You will see records list that were forwarded but not accepted:
  9. Press "Accept" near Account record sent from Org1;
  10. Go to "Create New or Link to Existing One" and select "Use Existing":
  11. Search you existing Account and press "Save".
Now Account from Org1 is synchronized with Account from Org2.

P.S.: Do not forget to go back to "Connection" tab in Org2 and check again "Auto-Accept" Accounts.

Tuesday, March 19, 2013

Apex, check object by Id. Ver 3.0

This is new version of method that I mentioned in my previous topics: 'Apex, check object by Id' and 'Apex, check object by Id. Update.'. Earlier, if Id.valueOf( objectIdString ) == null we had an error 'FATAL_ERROR System.StringException: Invalid id'. To avoid this error I use 'instanceof'. So now will not have any errors, even if we do not have such object in system like 'objectTypeNameString' or it will be null.
public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString, 
                 String objectTypeNameString ){
 
 return ( ( objectIdString instanceof Id )  
             && Id.valueOf( objectIdString ).getSObjectType().getDescribe().getName().equals( objectTypeNameString ) );
}
Test class will be the same.

Enjoy.

Sunday, December 23, 2012

Check if object is shareable through the S2S connection.

Today while building the page for S2S connection and record management, I have faced with issue of the necessity to filter object types that are available for sharing. It's inconvenient that PartnerNetworkConnection object does not have information what exact objects are published/subscribed in connection. While investigating this issues, I found out that the objects that can be shared have two similar fields: ConnectionSentId and ConnectionReceivedId. So to form a list of object types that are available for sharing we need to go through all objects and check up if their fieldset contains these fields. As in the example:
Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();
List<Schema.SObjectType> objectsForConnectionList = new List<Schema.SObjectType>();

for( Schema.SObjectType objectItem : globalDescrideMap.values() ){
    if( objectItem.getDescribe().fields.getMap().get('ConnectionSentId') ) != null ){
        objectsForConnectionList.add( objectItem );
    }
}
But in our case we have more than 100 objects that are available for sharing and this code gets an error:
System.LimitException: Too many fields describes: 101
Thus we can create one example of the object and try to add these fields. If the object can be shared we will have the error that this field is not writeable:
System.SObjectException: Field ConnectionSentId is not editable
In case this object is not shareable the error will be:
System.SObjectException: Invalid field ConnectionSentId for Training_Class_Attendee__History
This is the code sample:
Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();
List<Schema.SObjectType> objectsForConnectionList = new List<Schema.SObjectType>();

for( Schema.SObjectType objectItem : globalDescrideMap.values() ){
 try{
        SObject currentObject = objectItem.newSObject();
  currentObject.put( 'ConnectionSentId', null);
    }catch( SObjectException soExpt ){
        if( soExpt.getMessage() == 'Field ConnectionSentId is not editable' )
            objectsForConnectionList.add( objectItem );
    }
}
If you have any better solution for this issue, please comment.
Enjoy.

Friday, November 23, 2012

Apex, check object by Id. Update.

This is update to my previous topic 'Apex, check object by Id'. In Apex version 26 I found a better solution than go though the List of Schema. Id now have method called getSObjectType(). We can use it to get name of the object. And compare it to existing one. getSObjectType() gives us Schema.SObjectType.
public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString,
                                                              String objectTypeNameString ){
    Boolean isEqual = false;

    if( Id.valueOf( objectIdString ).getSObjectType().getDescribe().getName().equals( objectTypeNameString ) ){
    // getSObjectType().getDescribe().getName() gives us String that we can compare to incoming object type.
        isEqual = true;
    }
    return isEqual;
}
Test class will be the same.

Enjoy.

Friday, November 9, 2012

Work with try/catch and assigning errors.

Standard try/catch functionality in Salesforce include methods that allow you to add errors to appropriate objects in lists that was used in DML operation. Why it is so important to know what object failed and why? To answer this question lets look what happens if list of objects are inserted, for example ten Projects. If one Projects fails due to some error, validation rule, required field is not filled in or something else, all list will not be inserted. It means that valid Projects too. Another example, group of Opportunities are inserted, and trigger fires on after insert creates Project for each one. And the same thing - we have different errors on insert of several Projects. What to do with this errors? We need to show each one on appropriate Opportunity. I this cases try/catch has methods that allow us to find this failed objects and to track which object failed due to which error. Let's consider the following two cases:
  1. Insert of Project from Opportunity trigger. 
  2. Update of Project from Opportunity trigger. 
They are different because in first one Project fails on insert, and it means it do not have Id. So to identify what what Project or Projects fail we will use DMLException methods such as getNumDml and getDmlIndex. getNumDml() will return us number of failed objects and getDmlIndex() - row number of failed one. Lets look into simple trigger:
trigger OpportunityUpdateProjectTrigger on Opportunity ( after insert ) {
 // Create List of Projects that we will insert.
 List<Project__c> createProjectList = new List<Project__c>();
  // Going through all Opportunities and creating Project for each one.
  for( Opportunity opportunityItem : trigger.new )
   createProjectList.add( new Project__c( Name = opportunityItem.Name,
                                          Opportunity__c = opportunityItem.Id ) );
   
if ( !createProjectList.isEmpty() ){
  try{
   // Trying to insert Projects.
   insert createProjectList;
  } catch( DMLException dmlEx ){
   // Going through all errors.
   for( Integer i = 0; i < dmlEx.getNumDml(); i++ ){
    String errorMessage = 'Error on creation of Project: ' + dmlEx.getDmlMessage( i );
    // Attaching error to appropriate Opportunity from Trigger.newMap.
    trigger.newMap.get( createProjectList[ dmlEx.getDmlIndex( i ) ].Opportunity__c ).addError(errorMessage);
   }
  }
 }
}
Lets look what we can use in second case, where we have try/catch on update. Here we have update of Project on update of Opportunity. Trigger on Opportunity after update will query from database Projects that are related to this Opportunities, make some changes and update them. On update of Projects can be errors that we need to display on appropriate Opportunity, like in first case. But here we have Project Id. For this we will use DMLException method getDmlId, that returns Id of failed objects. We can use getDmlIndex, like previously. But in this case it would be better to use DMLException method getDmlId() will return Id of failed object. Lets look in to the code:
trigger OpportunityUpdateProjectTrigger on Opportunity ( after update ) {
 // Create Map of Projects that we will updated.
 Map<id,Project__c> projectsToUpdateMap = 
     new Map<id,Project__c>( [SELECT Id, Opportunity__c
                              FROM Project__c
                              WHERE Opportunity__c IN : trigger.newMap.keySet() ] );

/*
Here we have some updates on projects.
*/
 
 if( !projectsToUpdateMap.keySet().isEmpty() ){
  try{
   // Trying to update Projects.
   update projectsToUpdateMap.values();
  } catch( DMLException dmlEx ){
   for( Integer i = 0; i < dmlEx.getNumDml(); i++ ){
    String errorMessage = 'Error creation of Project: ' + dmlEx.getDmlMessage( i );
    
    // Attaching error to appropriate Opportunity from Trigger.newMap.
    trigger.newMap.get( projectsToUpdateMap.get( dmlEx.getDmlId( i ) ).Opportunity__c ).addError( errorMessage );
   }
  }
 }
}
So now we know we know how to add errors to failed one, and can make one more thing. In both cases valid Projects were not inserted, and as they didn't get errors we can try insert them in catch. For this we need to create new list of Projects that will include only valid ones and insert it.
...
  try{
   insert createProjectList;
  } catch( DMLException dmlEx ){
   // Crete new list of Projects to insert and fill it from old one
   List<Project__c> validProjectsToInsert = createProjectList;
   
   // Going through all errors.
   for( Integer i = 0; i < dmlEx.getNumDml(); i++ ){
    String errorMessage = 'Error on creation of Project: ' + dmlEx.getDmlMessage( i );
    trigger.newMap.get ( createProjectList[ dmlEx.getDmlIndex( i ) ].Opportunity__c ).addError(errorMessage);
 
 // Remove failed one
 validProjectsToInsert.remove( dmlEx.getDmlIndex( i ) );
   }
   
   // And final insert of valid
   insert validProjectsToInsert;
  }
...
In the end want to say that we can also use getDmlType() and getDmlStatusCode() to identify what specific error was. Or getDmlFields() with getDmlFieldNames() to know if error was on fields.

Enjoy.

Tuesday, November 6, 2012

Salesforce trigger list/map values on insert/update/delete/undelete

This table shows us when Id exists in trigger:

insert
update
delete
undelete
before
null
Id
Id
-
after
Id
Id
Id
Id

And this one shows what values of Map/List we have on different stages of trigger work:

Execution
Method
insert
update
delete
undelete
before
trigger.new()
List
List
null
-
before
trigger.newMap()
null
Map
null
-
before
trigger.old()
null
List
List
-
before
trigger.oldMap()
null
Map
Map
-
after
trigger.new()
List
List
null
List
after
trigger.newMap()
Map
Map
null
Map
after
trigger.old()
null
List
List
null
after
trigger.oldMap()
null
Map
Map
null

UTD: Code.

Enjoy.