Salesforce.com JavaScript to Override Buttons

Written by ShamrockCRM on July 23, 2009 – 7:20 pm -

This little snippet of code is written in JavaScript.  This piece of code is simply an example of what you can do by embedding JavaScript into a custom Salesforce.com button.

In this example, we have Opportunities and a related custom object called Shipments.  You sell a Product and you ship it, get it?

This button would be displayed on the “Shipments” related list on an Opportunity.  The button would be titled “New Shipment” and would replace the default button.

This button would check if a shipment for this Opportunity is currently “In Progress” and not “Shipped”.  If so, it will not allow the user to create a new shipment.  They should combine this new shipment into the existing one to save on transportation costs.

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 

var result = sforce.connection.query("Select Id From Shipment__c where Status__c = 'In Progress' and Opportunity__c = '{!Opportunity.Id}' ");
var records = result.getArray("records"); 

//If there are no Shipments In Progress, redirect the User to the New Shipment page
if (records.length == 0){
window.parent.location.href="{! urlFor( $Action.Shipment__c.New, $ObjectType.Job__c,null,true)}&CF00N20000001B5jC_lkid={!Opportunity.Id}&CF00N20000001B5jC={!Opportunity.Name}";
}
//If there are Shipments In Progress for this Opportunity
else{
alert("There is a current job on the system - please complete this job prior to creating a new job");
}

Tags: , , , ,
Posted in S-Controls, Salesforce.com, Web Development | No Comments »

Populate Custom Lookup Fields from an S-Control

Written by ShamrockCRM on April 4, 2009 – 12:00 pm -

Probably the most common request I receive from clients is to automatically populate a new record with fields.  This is a simple process, usually involving an S-Control and some URL parameters.  It gets a little more complicated, however, when you start wanting to populate lookup fields on the new records.

Warning:  This isn’t documented or supported by Salesforce, and could change at any time.

First, you need the ID of the field in question.  If you’ve already created S-Controls to populate new fields, this probably isn’t new to you.  However, if you haven’t, the easiest way is to navigate to the field in question in Setup. (Create > Objects > Object Name > Field Name, for custom objects).  Then, take the ID from the URL of the custom field, for example:

http://ssl.salesforce.com/00N50000001xxxx

You’ll want to pass this as a URL parameter when creating your new object, with “CF” preceding the ID and “_lkid” appended.  You’ll also want to provide a value for the textbox, as the ID isn’t enough.  So you’ll want to pass something like this for a Contact:

?CF00N50000001xxxx_lkid=00N50000001yyyy&CF00N50000001xxxx=John%20Doe

Basically, it just comes down to knowing to append “_lkid” to the ID of the field you’re passing in with the Salesforce ID, as well as providing a value to display in the textbox on the creation screen.  Hopefully this helps you out!


Tags: , ,
Posted in S-Controls, Salesforce.com | 3 Comments »

Modifying Salesforce.com Dates Using Javascript

Written by ShamrockCRM on January 27, 2009 – 9:57 pm -

CAUTION:  Technical jargon!!! Non-Business Related!!! Only interesting for Salesforce Developers.  :)

I ran across this interesting little problem while creating an orchestration in Cast Iron. I was creating a custom function that would adjust dates forward and back by weeks at a time.

What I found was, my small piece of code worked flawlessly 75% of the time. The 25% of the time would return a calculated date, or error, of “NaN-NaN-NaN.” I could not figure out what was wrong. I tested the code in the browser and everything worked properly.

From further testing and analysis, I determined that the code only broke when the month was July, August, and September or the day was the 7th, 8th, or 9th. I thought that this was very odd.

The code was taking a standard Salesforce date string formatted like “YYYY-MM-DD”, parsing out the text for the year/month/day, and using the parseInt Javascript function to convert the year/month/day into numeric values. I then created a Date object in Javascript and then made the date modifications.

Well, it turns out that parseInt is a bit quirky. If you do parseInt(“06″), this will return the number 6. If you do parseInt(“09″), this will return 0. ZERO?!?! Yes, 0. This is because parseInt will expect the number to be in Octal format if there is a 0 in front. There is no 09 in Octal. This causes the code to return 0, instead of the expected 9. So, when I subtracted 1 from the month to put the month in proper Javascript Date format, my code was inserting -1 as the month, therefore creating an invalid date. This only happened for the 7th, 8th and 9th months.

To resolve this, it was very simple. All you need to do is add “, 10″ after the number to parse into an Integer (declaring a Radix) and it will consider this number to be of Base 10, solving your issue.  So, the correct code would be parseInt(“09″, 10).

To learn more about Javascript date modification for Salesforce.com S-Controls, take a look at this W3 Schools link.


Tags: , , ,
Posted in Programming, S-Controls, Salesforce.com, Web Development | No Comments »