Resetting Passwords

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Resetting Passwords

  • Comments 6
  • Likes

I have had a few emails from different people over the last few weeks asking about code that will allow a system administrator to quickly reset a users password. I created a little ASP page that demonstrates how this can be done.

You can develop this idea further by send an email to the user.  The code for this page is given below:

[code language="javascript"]
<!-- #include file ="accpaccrm.js"-->
<%
if (CRM.Mode==View)

{
CRM.Mode = Edit;
}

var myBlock = CRM.GetBlock("entrygroup");
var customUserEntryBlock = CRM.GetBlock("entry");
with(customUserEntryBlock)
{
  EntryType = 22
  FieldName = "custom_userid";
  Caption = "User:";
  CaptionPos = CapTop;
  Size= 1;
  NewLine = false;
}
var customTextEntryBlock = CRM.GetBlock("entry");
with (customTextEntryBlock)
{
  EntryType = 49;
  Required = true;
  DefaultType = 1
  FieldName = "Password";
  Caption = "New Password:";
  CaptionPos = CapTop;
  maxLength = 60;
}

with(myBlock)
{
Title = "Reset Password";
AddBlock(customUserEntryBlock);
AddBlock(customTextEntryBlock);
}

CRM.AddContent(myBlock.Execute());
if(CRM.Mode==Save)
{
//var myRecord = CRM.FindRecord("users","user_userid="+Request.Form("custom_userid"));
//myRecord.user_password = Request.Form("Password");
//myRecord.SaveChanges();

var strUpdatePassword = "update users set user_mustchangepassword='True', user_password = '"+Request.Form("Password")+"' where user_userid ="+Request.Form("custom_userid");
var myQuery = CRM.CreateQueryObj(strUpdatePassword,"");
myQuery.ExecSQL();

var myRecord = CRM.FindRecord("users","user_userid="+Request.Form("custom_userid"));

CRM.AddContent("<br>");
CRM.AddContent("Password Changed");
CRM.Mode = Edit;
}

Response.Write(CRM.GetPage());
%>
[/code] 

The code is quite interesting as it also shows how you can create a screen from scratch without reference to meta data.  This is a subject that has been discussed in previous articles.  https://community.sagecrm.com/blogs/hints_tips_and_tricks/archive/2007/10/06/building-a-screen-not-bound-to-meta-data-using-the-asp-com-api.aspx

NB

The code that does the 'business' of reseting the password is highlighted in red.  I have set user_mustchangepassword='True' to make sure that the next time the user logs on the password has to be changed.

The password can be set outside of CRM using an external script like this:

[code language="javascript"]
//This example is can be saved into a a file with extension .js
//Run the file from desktop by double clicking

//////////////////
var username = "Admin";
var password = "";
var CRM = new ActiveXObject("eWare.CRM62");
CRM.FastLogon = 3;  //this prevents the meta data from loading.
CRM.Logon(username,password);

var strUpdatePassword = "update users set user_mustchangepassword='True', user_password = 'fish' where user_userid =4";
var myQuery = CRM.CreateQueryObj(strUpdatePassword,"");
myQuery.ExecSQL();
[/code] 

The code above was saved into file called resetpassword.js and run from the desktop.  Of course you may not want to set just Susan Maye's password to 'fish' but it shows how the idea works.

If you want to send an email notification that the password has been reset then you can use the Message block discussed here:https://community.sagecrm.com/blogs/hints_tips_and_tricks/archive/2008/12/04/sending-emails-in-asp-com-api.aspx

What is true for the COM API is also true for the .NET API.  The .NET API has the equivalent to the CRM.CreateQueryObj() method.

This is C# Code for the Sage CRM .NET API

[code language="csharp"]
string strSQL = "update users set user_mustchangepassword='True', user_password = 'fish' where user_userid =4";
QuerySelect queryObj = new QuerySelect();
queryObj.SQLCommand = strSQL;
queryObj.ExecuteNonQuery();
[/code]

Comments