function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fifedogfifedog 

Javascript - Multi-step operation issues

Hello,

I'm trying to two things at the same time in my javascript, Set fields on the contact record, then fire an email. 

Overview: We're going to start managing partner data, contacts and accounts, from within SFDC.  We currently have a Partner Extranet that allows the partner to log in and get access to documents and marketing stuff.  When a new partner signs up, an Account and Contact is created.  Once the Partner Manager approves this partner they want to have a "One Click" approval that does multiplet steps, Mark the contact as active, create a password, send welcome email with the contacts username (their email address) and password.

Issue: Each of the steps work independently however when I try and do them all at once, only the email step seems to work.  The screen seems to go right to the email step.  When I send the email and return to the contact record, no password is set nor is the active field.

Any Ideas, the code is below

Code


var URL = "https://na1.salesforce.com/{!Contact_ID}/e?retURL=%2F{!Contact_ID}";
var field_pswd= "&00N30000000ksPV=";
var sPassword = "";
var field_active="&00N30000000ksPa=1";
var template="00X30000000gvfF";

Main();

function Main() {
sPassword = GeneratePassword();
//Here is where I set all the values for the URL to Edit and auto save the record.
parent.frames.location.replace(URL+field_active+field_pswd+sPassword+"&save=1");

//Here is where I create an email from this record with a pre-determind template.
parent.frames.location.replace("https://na1.salesforce.com/email/author/emailauthor.jsp?retURL=%2F{!Contact_ID}+&rtype=003&p2_lkid={!Contact_ID}&template_id="+template+"&p2={!User_ID}");
}


function GeneratePassword() {
var length=4;
var noPunction = true;
var numl = 0;

for (i=0; i < length; i++) {

numI = getRandomNum();
if (noPunction) {while (checkPunc(numI)) {numI = getRandomNum(); }}

sPassword = sPassword + String.fromCharCode(numI);
}

return sPassword;
}

function getRandomNum() {

// between 0 - 1
var rndNum = Math.random();

// rndNum from 0 - 1000
rndNum = parseInt(rndNum * 1000);

// rndNum from 33 - 127
rndNum = (rndNum % 94) + 33;
return rndNum;
}

function checkPunc(num) {

if ((num >=33) && (num <=47)) { return true; }
if ((num >=58) && (num <=64)) { return true; }
if ((num >=91) && (num <=96)) { return true; }
if ((num >=123) && (num <=126)) { return true; }

return false;
}

darozdaroz
At a quick glance...

It looks as through you are trying to have the browser redirect to a URL to set the password and then redirect to the email location...

Problem is because they are happening back to back the first URL never quite loads(*) and the 2nd URL overwrites the first request firing only the email.

(* - If the first URL did load completely your JavaScript would cease execution at that point as you are now looking at a different page and the email would never be sent)

The solution is AJAX... sorta.

What you want to do is fire off an XMLHTTPRequest to the first URL, sync, NOT async. This will allow that URL to fully load and then return control to your script (since the page never really changed). You can discard the return from the URL (or parse it if you're very brave / masochistic).

Then you can do the redirect to the Email URL and you should be fine...
fifedogfifedog

Daroz,
thanks!  I guess I have to get into that xml stuff.  I'm not that great of a developer, it took me a full year just to pass my 102 Computer Science class.  I'm a Business MIS major. Oh well but what you said makes sense. 

ASmithASmith
Hi Fifedog,

Try using the code below. I haven't tested it, but it should be close. You might want to add some additional error handling as it could use some work. Enjoy!

Andrew

var xmlhttp;
var url;
var field_pswd= "00N30000000ksPV";
var sPassword = "";
var field_active="00N30000000ksPa";
var template="00X30000000gvfF";

Main();

function Main() {
sPassword = GeneratePassword();

url = '/{!Contact_ID}/e?retURL=%2F{!Contact_ID}';
url = url+"&"+field_active+"=1";
url = url+"&"+field_pswd+"="+sPassword;
url = url+"&save=1";
httpCall();

//Here is where I create an email from this record with a pre-determind template.
window.parent.parent.location.href='/email/author/emailauthor.jsp?retURL=%2F{!Contact_ID}+&rtype=003&p2_lkid={!Contact_ID}&template_id='+template+'&p2={!User_ID}';
}

function GeneratePassword() {
var length=4;
var noPunction = true;
var numl = 0;

for (i=0; i < length; i++) {

numI = getRandomNum();
if (noPunction) {while (checkPunc(numI)) {numI = getRandomNum(); }}

sPassword = sPassword + String.fromCharCode(numI);
}

return sPassword;
}

function getRandomNum() {

// between 0 - 1
var rndNum = Math.random();

// rndNum from 0 - 1000
rndNum = parseInt(rndNum * 1000);

// rndNum from 33 - 127
rndNum = (rndNum % 94) + 33;
return rndNum;
}

function checkPunc(num) {

if ((num >=33) && (num if ((num >=58) && (num if ((num >=91) && (num if ((num >=123) && (num
return false;
}

function httpCall() {

// code for Mozilla, etc.
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=state_Change;
}
// code for IE
else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP')
if (xmlhttp) {
xmlhttp.open('GET',url,true);
xmlhttp.send();
xmlhttp.onreadystatechange=state_Change;
}
}

return false;
}

function state_Change() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) {
// if "OK"
if (xmlhttp.status==200) {
// alert("XML OK");
}
else {
alert('Problem retrieving XML:' + xmlhttp.statusText);
}
}
}
darozdaroz
Andrew,

I was looking over the code and I think he's going to have essentially the same problem with this code that he had with his old code. Because you are calling the function to start the XMLHTTPRequest and then immediately redirecting the window the script will stop when the window is redirected.

xmlhttp.open('GET',url,true);


I believe that third parameter should be false, to not do an async call here. This should allow the XMLHTTPRequest to complete (force it really) and then return control to the script which will then issue the window redirect and show the final page.

Pete - If you get similiar results with the code Andrew posted try making that change.
fifedogfifedog
Hey Andrew and Daroz,
Thank you for 'writing' part of my code for me much thanks. I owe you a drink at Dreamforce. However I'll owe you a dinner if it works.

I first tried what Andrew suggested, no go same issue. Then changed the true to false for both xmlhttp.open('GET',url,true); statments. Tested in FireFox, no go. Testing in IE, no go.

Back to square one.
darozdaroz
Humm...

Is it poping any JS errors on the JS console in Firefix?

Have you tried looking at the network traffic to see if it's making the request to the server? (You'll want to use http://na1... instead of https:// for this)

Lastly you can toss in some debugging output to the JS console to trace it's execution path.

Andrew's code should work, but you might also want to compare notes with the code from here: http://blog.sforce.com/sforce/2005/06/cross_platform_.html
fifedogfifedog
YOU HAVE A DINNER!

Daroz and Andrew if you find me during Dreamforce I'll be happy to treat you to a dinner. The code does work. Andrew redid how the url string get's contatenated, (which was better coding and best practice's I have to addmit), so the URL wasn't properly formated with my addition.

Now that works I'll be trying to added 2 other steps that need to get down with this one click. Thanks Guys!
darozdaroz
Glad to hear you got it kicking.
pbreitpbreit

Is the JavaScript EmailAuthor thing documented anywhere? Like what are all the possible p2 and other parameters?

 

Salesforce should be all over this as this type of functionality is the essence of web-based services, being able to post-in URLs with parameters that either execute functions or pre-fill forms. 

jbardetjbardet

Yes, I would really like to see the parameters listed. It feels like just guess work right now.

 

I still can't auto-fill in the "To" parameter: I am forced to use the Additonal To parameter which happens to be p24.

 

Anyone have a list of these parameters?

 

Thanks,

Jeremy

jbardet at gmail dot com