Pages

Monday, August 8, 2011

SugarCRM Customization: Default Assigned User

El Bibliomata/Flickr
One of the first customizations that I recall applying to the first SugarCRM system I was in charge of was to set the default assigned user on Cases to someone other than the logged in user. This was used as a way to simplify the need for the submitting user to have to select the same assigned user every time a new case was added. That was back in 2005, and using version 3.0 of the product.


Although the process is still not a simple matter of a couple of clicks of the mouse, major advancements to the SugarCRM architecture since that time have indeed simplified it when compared to when I first had to tackle the issue, so do not be discouraged.


Let us take a look at exactly how it is done.

Our first step is to ready the Cases module to accept a customized version of the view.edit.php file. This file controls the Edit screen on a given module, which is exactly what we need to manipulate, as we want to adjust it to set a default user when a record is created. 

To prepare the Cases module for this change, we need access to the directory where SugarCRM is installed and then proceed to follow these steps:


1. If not present, create the directory <sugar root>/custom/modules/Cases/views
2. Copy the file view.edit.php from <sugar root>/include/MVC/View/views to <sugar root>/custom/modules/Cases/views


Now that the file is in place, we are ready to apply our customization. As you will notice, it is actually a very simple modification. 


Here is what we need to do:


1. Open the file view.edit.php that was just copied into <sugar root>/custom/modules/Cases/views (using Notepad or other text editor of your liking)
2. Scroll down to the following section (around line 56):



 function display(){

$this->ev->process();
echo $this->ev->display($this->showTitle);
 }


3. Change the above to match the following (changes are bold and in red):



 function display(){

if (empty($this->bean->fetched_row['id']))
{
               //Replace 'Test User' with the 'Full Name' value of the desired user
      $this->bean->assigned_user_name = 'Test User';


               //Replace the ID value below with that corresponding to the test user
      $this->bean->assigned_user_id = 'f301e272-bdac-51a2-248b-4e1b5d2e3d95';
}

$this->ev->process();
echo $this->ev->display($this->showTitle);
 }

4. Close and save the file when prompted.

Now we are ready to try it. 

If all went well, creating a new case should automatically display the desired user as the Assigned User for the new entry. Edits to existing records will retain there already assigned user value.  

8 comments:

  1. Thank you for post, how would be possible to inherit assigned to user from parent entity ? (e.g. contact assigned to account would have same assigned to as account.)

    ReplyDelete
  2. @Robo:

    That would actually best be answered via another post. Thanks for the idea. I'll put something up and reply to this thread with the link to the post that addresses your question.

    ReplyDelete
  3. Hi, our cases are automatically assigned by stored procedure. My problem is that user is that notified of the assignment. How to trigger the notification email in this scenario?

    Thanks. Winston

    ReplyDelete
    Replies
    1. You need to assign the record within the scope of Sugar code, i.e. a logic hook, someone selecting a user via the interface, a custom script that goes through the SugarBean, etc.

      If you do it outside the scope of Sugar, such as through a stored procedure that executes at the database level, Sugar is unaware of that change and thus, it doesn't trigger the notification.

      Delete
  4. Also note that you need to change the name of the new class, change the class that we're extending, and point to a different doc in the require_once() function. If you don't do this, you'll likely encounter fatal PHP errors like 'PHP Fatal error: Cannot redeclare class ViewEdit'

    The first three lines should look like:

    <?php
    require_once('include/MVC/View/views/view.edit.php');
    class CustomViewEdit extends ViewEdit{

    ReplyDelete
    Replies
    1. Yes, good point. That was a change that was introduced in version 6.2 if not mistaken.

      Also, the main function's name has to be changed to:

      function CustomViewEdit() and the like if memory serves me.

      Delete
  5. We are on SugarCRM PRO 6.5 and this did not quite work for us. Assiging a value to this->bean->assigned_user_id did not create an error but had no effect. What DID work was the following:

    useForSubpanel = true;
    $this->useModuleQuickCreateTemplate = true;
    }

    public function display() {
    global $db;

    if (empty($this->bean->fetched_row['id']))
    {
    // first get the assigned user from Accounts (the Account Manager)
    $accountid = $_REQUEST['account_id'];
    $qry = "SELECT assigned_user_id FROM accounts WHERE id = '$accountid' AND deleted = 0";
    $results = $db->query($qry);
    $qryresult = $results->fetch_assoc();
    $am_id = $qryresult['assigned_user_id'];

    //Replace the ID value below with that corresponding to the desired user
    $this->ev->focus->assigned_user_id = $am_id;
    }
    parent::display();
    }
    }

    It should be noted that we only allow creation of Bugs from within the Account profile so all Bugs must be associated with an Account, you may have different logic to apply!

    Also, note the lines added to __construct() to allow this code to be called in the case of a subpanel/QuickCreate - otherwise this would not work for us.

    Hope this helps! :)

    ReplyDelete
    Replies
    1. Wasn't trying to be anonymous - just wasn't logged in to the right account whenI made the above post! :)

      Bob C

      Delete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.