Wednesday, August 9, 2017

System.TypeException: Invalid date/time Exception in Salesforce Apex

  • When your assigning a value DateTime field using the String then we will end up with this error.
  • If your receiving your date value as part of JSON response and then assiging to DateTime value you will also end up with same error.
To avoid this issue we can use the below two procedures

Sample Error Code:


if(mapName.KeySet().contains('creation_Time') && string.isNotBlank(string.valueof(mapName.get('creation_Time'))))
{

 if(string.valueof(mapName.get('creation_Time')).contains('T'))
  {
    ObjectInsatnce.creation_Time__c = datetime.valueof(string.valueof(mapName.get('cancellation_time')).replace('T',' '));
  }
else
{
 ObjectInsatnce.Cancellation_Time__c = datetime.valueof(mapName.get('cancellation_time'));
}
}

Rectified Source Code:


if(mapName.KeySet().contains('creation_Time') && string.isNotBlank(string.valueof(mapName.get('creation_Time'))))
{

if(string.valueof(mapName.get('creation_Time')).contains('T'))
{
 ObjectInsatnce.creation_Time__c = datetime.valueof(string.valueof(mapName.get('cancellation_time')).replace('T',' '));
}
else
{
  ObjectInsatnce.Cancellation_Time__c = datetime.valueof((String)mapName.get('cancellation_time'));
}
}
 

Hope this helps you....Enjoy!

Monday, August 7, 2017

Google Service Account Private Key in JSON File

To authenticate the Google Cloud API's we need a Private Key which will be generated by Google for each Service Account Individually . This Private Key can be generated in 2 formats .


  • JSON format
  • PKCS12 format

Sample JSON file with Private Key:


{
 "type": "service_account",
 
 "project_id": "XXXX:api-project-1234",
 
 "private_key_id": "xxxxxx4360517xxxxx",
 
 "private_key": "-----BEGIN PRIVATE KEY-----\+lzqL6szpFDr5M/ow8Fa8X\nKokb6SfT+liJJboTVosoR8C1l1EnYRg+/qwv0w4JPdmLS1sY53k2EFP0EcqVYOYr\nDZx6a+TIVFB
 Xkas2IbyLsN13CCBJ5r1lhpCeP+nrCdi2MddXjTyfAIhGb5/fhTfU\nNoAaodmBuwKBgQDYzKuHLB9S+tjHKca/0fS1DLvo2vxbicDwAlDPrGfDrMHNKZBd\nKDTRJdk8R3FRNHcDHEZI969WyXcNNycM2aNcWy
 jMlCM/15pGylzDZRmP7gM20VFG\nnoNV5Y0TARJYGP2652kVsA2LuiBaA2GPHORSXo4/RvR6B+hQYJdh3GkGUQKBgQCY\nitMHu0ZkbLfze8kEYtnhX20SO5OaNgVtOcVd5RnEkU6W8fjY0ugpINbg+m7ZQh09\
 nS7z8T8DBf0CO2R4izaFZ==\n-----END PRIVATE KEY-----\n",
 
 "client_email": "xxxxxxxxxxxxxx.iam.gserviceaccount.com",
 
 "client_id": "xxxxxx4360517xxxxx",
 
 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
 
 "token_uri": "https://accounts.google.com/o/oauth2/token",
 
 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
 
 "client_x509_cert_url": "https://www.googleapis.com/xxxxxxxiam.gserviceaccount.com"
}

The one which resides between BEGIN PRIVATE KEY and END PRIVATE KEY is called Private Key.

For more information please refer this link.

Hope this helps you....Enjoy!