Monday, April 30, 2012

Chrome not displaying Listbox selected item when used inside an Ajax UpdatePanel

On a page with an ASP.NET ListBox (HTML select with a "size" attribute) that is contained within an UpdatePanel Chrome isn't scrolling the select control to display the selected item when the update panel updates the page. The selection has been made, it just can't be seen if it isn't within the first items displayed.

Firefox and Internet Explorer scroll the select to display the selected item as expected both on the initial page load and the update panels partial page refresh.

I solved this by registering a call to a Javascript function in the PageRequestManager endRequest event (this fires after the update panel does the partial page refresh). The new Javascript function loops through the options on the select and toggles the selected attribute on the one that is currently selected.

 <script type="text/javascript">

  // For callbacks in webkit force the listBox to reselect.
  if ($.browser.safari) {
   var prm = Sys.WebForms.PageRequestManager.getInstance();
   prm.add_endRequest(function () {
    FocusSelected();
   });
  }

  function FocusSelected() {
   var lb = document.getElementById("<%= listBoxControlNameHere.ClientID %>");
   if (lb != null) {
    var options = lb.options;
    for (var i = options.length - 1; i > 0; i--) {
     if (options[i].selected == true) {

      var x = options[i];

      x.selected = false;
      x.focus();
      x.selected = true;

      return;
     }
    }
   }
  }
 </script>

See Also:

Friday, April 20, 2012

Salesforce INVALID_FIELD_FOR_INSERT_UPDATE when updating Accounts and the __pc field suffix

I recently encountered the following error when updating a Salesforce Account with S4S.

Error INVALID_FIELD_FOR_INSERT_UPDATE - Account: bad field names on insert/update call: Field1__pc, Field2__pc, Field3__pc

Oddly, the fields referenced in the error were actually custom Contact fields. My first thought was an Account trigger the resulted in an update to a related Contact record.

That didn't lead anywhere, but I did notice the custom field suffix was "__pc" rather than the typical "__c".

Turns out the "__pc" suffix is used when the org is configured to use Person Accounts.

Some custom fields have __pc suffix instead of __c. Those are custom fields that have been merged from the Contacts object into the Accounts object. For example, if your Contacts object has a custom field height__c, it becomes height__pc in the Account object. All standard Contact fields are [pre]ppended with "person".
[1]

See also:

Friday, April 13, 2012

Using Saleforce Assignment Rules from G4S or S4S

S4S and G4S can both utilize the Lead Assignment Rules from Salesforce when inserting a new record.

Essentially an AssignmentRuleHeader needs to be added to the Salesforce binding with the useDefaultRule property set to true. This is equivalent to checking "Assign using active assignment rule" at the end of the Lead creation process in the UI.

After the insertion is complete the AssignmentRuleHeader should be cleared from the binding so it doesn't affect subsequent calls from that Session.

[TestMethod]
public void CreateLeadWithActiveAssignmentRule()
{
 SalesforceSession salesforceSession = SessionTest.GetActiveSession();
 Lead lead = null;

 LeadService leadService = null;

 try
 {
  lead = new Lead();
  lead.FirstName = "John";
  lead.LastName = "Brown";
  lead.Company = "ABC Corporation";
  lead.LeadSource = "Web";

  // Setting the lead source for a pre-existing lead assignment rule. This  
  // rule was created outside of this test case and will assign any new leads 
  // to a user who is not the current session owner.

  // Create the assignment rule header and add it to the proxy binding 
  AssignmentRuleHeader arh = new AssignmentRuleHeader();
  // Use the current default rule. Equivalent to checking "Assign using active assignment rule"
  arh.useDefaultRule = true;

  // Every operation that results in a new or updated lead will use the 
  // specified rule until the header is removed from the proxy binding 
  salesforceSession.Binding.AssignmentRuleHeaderValue = arh;

  leadService = new LeadService(salesforceSession);

  SaveResult saveResult = leadService.Insert(lead);
  if (!saveResult.success)
  {
   Assert.Fail(saveResult.errors[0].message);
  }

  // The lead will now have an Id
  Assert.IsTrue(leadService.ValidEntityId(lead.Id));

  // This call effectively removes the header. The next lead will be assigned 
  // to the default lead owner. 
  salesforceSession.Binding.AssignmentRuleHeaderValue = null;

  // Check that the assignment rule has been applied and the owner is not the current user.
  leadService.Refresh(lead, Lead.Fields.OwnerId);

  Assert.AreNotEqual(salesforceSession.SalesforceUserId, lead.OwnerId, "AssignmentRule should have changed the OwnerId");
    
 }
 finally
 {
  if (leadService != null && lead != null && lead.Id != null)
  {
   try { leadService.DeleteEntity(lead); }
   catch (Exception) { }
  }
 }
}

Sunday, April 8, 2012

Avoiding the Salesforce verification code activation

In the majority of cases the Salesforce verification code isn't too much of an issue. You access Salesforce from an unknown IP address, they email you a six digit code which you enter and then carry on.

However, there are cases where accessing this email can be particularly difficult. E.g.

  • Shared login credentials for development orgs (only one person gets the email)
  • After a Sandbox refresh where email addresses have a suffix
  • Apps on mobile devices that are roaming between different network connections.

In the simplest solution you add your known trusted network IP ranges and then carry on.

If that doesn't work you could try raising a case with Salesforce support to disable this feature for your org.

Alternatively, you could add every possible IP address to the Trusted Networks. This is simple in theory, but Salesforce will only allow you to add IP ranges of 33,554,432 addresses (225, a /7 CIDR block).

Doing so manually for every possible IP address will be time consuming as you will need 128 IP ranges.

I did find a package that will automate the process - Enable All Trusted IP Ranges for a Salesforce Org.
I'd probably advise against it in a production org but it was useful for a developer edition org.

Thursday, March 29, 2012

Windows Phone 7&8 - Barcode Wallet

About Barcode Wallet for Windows Phone

Save space in your wallet by digitizing the barcodes of any loyalty or memberships cards that you carry around.

Features:

  • See your most used barcodes at a glance
  • Tap a card to quickly bring up the barcode ready for scanning.
  • Pin your most used cards to the start screen.
  • Scan a barcode directly from the phone from a video feed, photo, or saved image.
  • Backup and restore barcodes to Onedrive

Usage

These are the basic steps to add a new barcode to the app by scanning it.

Add new barcode

Tap the add button in the menu bar

Start Scanning

Tap the scan button in the menu bar

Scan the barcode

Point the phones camera at the barcode.
See scanning tips for further details on using the camera to capture the barcode.

Name the barcode

Enter a name for the barcode

Add a photo

Optionally add a photo to identify the barcode using the add button next to the photo label.

Select and crop the image.

Save the barcode

Tap the save button in the menu bar

View the barcode

Tap the barcode to view it

Tips and FAQ

Scanning Barcodes into the phone

  • Ensure that the complete barcode can be seen clearly in the photo
  • Try to avoid the flash over saturating the barcode
  • Orientate the barcode to match the phones orientation
  • Check that the barcode format is supported by the app.
  • If you are having issues with a particular barcode try sending an image of it to an online reader. I've found the ClearImage Free Online Barcode Reader / Decoder to work well and provide back useful information, such as the barcode type as well as the data.

Scanning barcodes directly from the phones screen

Unfortunately not all types of barcode scanner are capable of reading barcodes directly from a cell phone screen. The major factor is the type of scanner being used.

A laser scanner sends out beams of light and uses the reflected light to read the light and dark bars. White bars reflect back the majority of the light. Black bars absorb the majority of light and reflect back very little. Therein lies the issue for scanning a barcode from a cellphone screen. Firstly, The glass and other layers of a cellphone screen are more reflective than a printed barcode. The excess light they reflect back will prevent the scanner from clearly seeing the difference between light and dark portions of the barcode. Secondly, a cellphone screen does not appear white because it is reflecting light. Rather, it is emitting light from a number of other colours to create white (typically red, green and blue pixels). These multiple pixels emitting light together won't reflect light in the way that a printed white does. For these reasons a laser scanner cannot read a barcode from a phones screen.

Another type of scanner is an imager or CCD Barcode scanner. These work more like a digital camera and will see the light emitted from the screen. CCD scanners can read a barcode from a cellphone screen.

See also:

How do I delete a barcode from the wallet?

On the first screen, try holding down on the barcode in question and then selecting edit.
On the edit screen there will be a delete link in the application bar at the bottom of the screen.

Scanning a PDF417 barcode produces the error "CP437 is not a supported encoding name. Parameter name: name"

Based on the error message it seems the barcode contains characters that aren't supported by the Windows Phone platform.

CP437 refers to Code page 437

Unable to create Onedrive folder to store barcodes

Try manually creating the folder if the Onedrive permissions don't allow the app to. The folder should be called "Barcode Wallet" and be at the top level of the files directory. E.g.

Known Issues with v2.8.x

Unfortunately there in a known bug with v2.8.78 and 2.8.80.

  1. On WP7 and WP8 handsets there is a bug with the app bar focus button on the barcode scanning page. This prevents adjusting the focus to scan a barcode.
    Workaround: Half pressing the hardware camera button will also focus the camera.

8th June 2013 - I have resolved the bug and submitted v2.9.x to the store. It may take a few days for it to pass the store certification process.

How do I sync barcodes to Onedrive from the phone?

From the "Barcode Wallet Onedrive Sync" page (accessed via the cloud button in the app bar). Press and hold on the barcode you want to sync to Onedrive. An option should appear to transfer it.

Alternatively, tap to the left of a barcode to check it. Then use the "sync" command to transfer all the checked barcodes. Pressing "sync" with no selections will give the option of transferring all the barcodes.

If you select Onedrive rather than phone the sync direction is reversed.

How do I sync barcodes to the native Wallet app?

Wallet integration is currently supported from the barcode edit page. Expand out the application bar and select "send to wallet".


Current issues and enhancement requests

Thursday, March 22, 2012

Windows Phone 7 - Sun Tracker

About Sun Tracker for WP7

Track the position of the sun throughout the day. Determine when sunrise will occur and then track the path of the sun through the sky until sunset. With tracking enabled the phone can be pointed at the sky to measure current elevation and azimuth angle.

2 of 8

New in version 2.0. Switch to the augmented reality vision mode and see the path of the sun overlayed on your environment. See exactly where the sun will go today and easily switch to the Solstice and Equinox paths.

Use the details page to calculate the Time, Elevation and Azimuth angle of:

  • Astronomical Twilight (Dawn and Dusk)
  • Nautical Twilight (Dawn and Dusk)
  • Civil Twilight (Dawn and Dusk)
  • Sunrise
  • Sunset
  • A user selected date and time.

The equation of time, Solar declination and Daylight duration are also calculated. All values are in the local timezone and adjusted for daylight savings if applicable.

Select a location on the map page or use the GPS to go to your current location. The map page also shows the direction and time of sunrise and sunset.

A useful tool for:

  • Real Estate - When will the sun come over the hill? Will a tree or building block most of the winter sun?
  • Gardeners - will the planting location suit have the right amount of sun exposure?
  • Photographers - When will twilight occur?
  • Architects - Maximise winter sun exposure and minimise summer heat gain.
  • Solar panels - What angle and inclination should the panels be placed at to be optimal for the location?

The settings page allows you to toggle access to location data and provide an optional correction to the compass bearing.

Versions:

  • 2.0
    • Adds augmented reality sun tracking via the vision button. For best results enable the use of the device location and calibrate the compass before use.
    • The last Zoom level used on the map it kept between uses and there is an option on the settings page to show zoom level buttons on the map.
  • 1.3
    • Adds Dates page that includes Solstices, Equinoxes, Perihelion, Aphelion
  • 1.1
    • Adds pin option for current location to display rise and set times plus length of day on the back of the apps tile.
    • Tapping the rise or set times in the compass view will toggle to showing the time until the event occurs.

User Guide

Tips

  • Ensure your current location is correct. Use the "location" button in the application bar to update to your current latitude and longitude. Verify this on the map page.
  • Ensure your select date is current. This can be altered with the date controls at the bottom of the page.
  • Use the "vision" button to see the projected suns path for your selected day.
  • You can jump to important dates such as the solstice or equinox on the dates page.

Compass

1 of 8

The compass shows the position of the sun through out a 24 hour period. The position of the sun is plotted according to its true north bearing (Azimuth). The points move into the circle as the elevation above the horizon increases. E.g. If the sun is directly overhead (at the Zennith) it will be in the middle of the circle.

Sunrise and Sunset times along with the Azimuth bearing are shown above the compass.

Details

3 of 8

The Details page gives times for dusk and dawn, sunset and sunrise. These will vary if you change the selected date or location.

Map

4 of 85 of 8

The Map page allows you to choose the latitude and longitude for the sun calculations. The location button will recenter the view to your current latitude and longitude.

Lines show the sunrise and sunset azimuth angles and are labeled with the applicable event times. The remaining line shows the azimuth for the selected date and time. The control on the left of the page shows the elevation.

Dates

5 of 8

The Dates page shows the Solstices, Equinoxes, Perihelion (Earth nearest to the sun), and Aphelion (Earth farthest from the Sun) for the currently selected year.

Wednesday, March 21, 2012

Salesforce - Obscure field integrity exception when upserting PricebookEntry

System.DmlException: Upsert failed. First exception on row 0 with id 01uF0000007wq56IAA; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []

I encountered this exception in an Apex batch job that I inherited (as in I didn't write the original code) and it wasn't really clear what was causing the field integrity exception.

Turns out the code was setting a specific PricebookEntry UnitPrice where the UseStandardPrice was also set to true. When setting a specific UnitPrice the UseStandardPrice should be set to false.

From the help docs:

[Unit price:] "You can specify a value only if UseStandardPrice is set to false."

[UseStandardPrice:] Indicates whether this object uses the standard price defined in the standard Pricebook2 object (true) or not (false). If set to true, then the UnitPrice field is read-only, and the value will be the same as the UnitPricevalue in the corresponding PricebookEntry in the standard price book (that is, the PricebookEntry object whose Pricebook2Id refers to the standard price book and whose Product2Id and CurrencyIsoCode are the same as this object). For PricebookEntry objects associated with the standard Pricebook2 object, this field must be set to true.