Another Salesforce blog!!!

Salesforce, Apex


Check to see whether the file in a url Exists or Not

You can use HttpWebRequest. Note four things 1) The webRequest will throw exception if the link doesn’t exist 2) You may like to disable auto redirect 3) You may also like to check if it’s a valid url. If not, it will throw UriFormatException. 4) Per Paige suggested , Use “Head” in request.Method so that […]

ExecuteNonQuery / ExecuteScalar

Namespaces: System.Data; System.Xml; System.Configuration; // Clear out any test records from previous test executions. using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[“DB_NAME”].ConnectionString)) { connection.Open(); string _mediaId = string.Empty; string _title = “Title”; using (System.Data.SqlClient.SqlCommand cmd = connection.CreateCommand() as System.Data.SqlClient.SqlCommand) { System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand(sql, connection); cmd1.CommandType = CommandType.Text; using (System.Data.SqlClient.SqlDataReader reader = cmd1.ExecuteReader()) { while (reader.Read()) […]

Extract substring from a string until finds a comma c#

substring = str.Split(‘,’)[0]; or You can use IndexOf() to find out where is the comma, and then extract the substring. If you are sure it will always have the comma you can skip the check. string a = “asdkjafjksdlfm,dsklfmdkslfmdkslmfksd”; int comma = a.IndexOf(‘,’); string b = a; if (comma != -1) { b = a.Substring(0, […]

Convert foreach loop to Linq code

Converting this code to Linq: string[] selections = “Men,Women,Boys”.Split(‘,’); int _chkboxId = 0; int _chkboxTextId = 1; try { string id = “lstchk_” + _chkboxId; while (!driver.FindElement(By.Id(id)).Equals(null)) { string checkboxId = String.Format(“lstchk{0}”, _chkboxTextId); string checkboxName = driver.FindElement(By.Id(checkboxId)).Text; foreach (string match in selections) { if (checkboxName == match.Trim()) { //matched… do more work here… } } […]

Refactoring horrible nested if-else statements

I wrote this at some point this week, I was looking back at it tonight and released how truly awful it looks: if(filePath.Contains(“.CSS”)) return true; else return false; else if(filePath.Contains(“.JS”)) return true; else return false; else if(filePath.Contains(“_STR”)) return true; else return false; else if(filePath.Contains(“.VBS”)) return true; else return false; else if(filePath.Contains(“.HTM”)) return true; else return […]