Improve your workflow with ajax: one click activity status changing

Publicat
2010-10-01 01:53
Written by

Hi all,

 

For the training in london, we wanted a simple example that illustrates how to customise and improve civicrm for specific usages using the ajax interface. I'm sharing the result with you, hoping you will find it useful.

One common workflow we have is to change the status of an activity from "scheduled" to "complete". The default way is to click on edit, go to the full form, change the status, save, and go back to the list of activities

We are going to improve it with a "one click click complete": on the list of activities, we transform the status column into an action (when "scheduled"), and when I click on it, it changes it to Completed, without changing screen. For that, we are using the ajax interface and the activity api. Copy the template templates/CRM/Activity/Selector/Activity.tpl into your override directory, and add a few lines of jQuery at the top:

{literal}
<script>
  jQuery(function ($){
    $("td.crm-activity-status_1")
    .attr("title","Change the status to completed")
    .css("cursor","pointer")
    .click (function () {
      aid=$(this).parent("tr").attr('id');
      var activity_id = aid.replace("crm-activity_", "");
      cj().crmAPI ('activity','update',{id:activity_id,status_id:2}, 
       {callBack: function(result,settings){
          if (result.is_error == 1) {
            $(settings.msgbox)
            .addClass('msgnok')
            .html(result.error_message);
            return false;
          } else {
            $('#crm-activity_'+activity_id)
            .removeClass('status-overdue')
            .find('td.crm-activity-status_1')
            .removeClass('crm-activity-status_1')
            .addClass('crm-activity-status_2')
            .html("Completed"); 
          }
        }
       });
    });
  });
</script>
{/literal}
Oh, it's magic! What does it do in detail ? Well, you should attend a developer training, you will discover it, and way more ;) X+

Comments

Good work guys. So does this become an alternate approach to eg needing to change the Membership status on lots of records in a simpler manner - since Batch Update isn't an option yet?

You'll need to check if the api is there (pretty sure it is). As for the code adaptation, this is an exercise left to the reader ;)

X+

P.S. Unidentified members of the community do not want this to end up in the core for activities, you might be more sucessful for membership

Can these callbacks be made from anywhere on your drupal site (i.e. in a view) or do you have to be on the mysite.org/civi path where cj = $ jquery is loaded?

On a more recent version of civi (> 4.3?) use 

 CRM.api

instead of

cj.crmAPI

the rest of the code should work the same