Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to update a non-mass update field on a mass update

  1. #1
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default How to update a non-mass update field on a mass update

    I'm trying to figure out if this is possible. I have a field that's a flag called "decision_sent" that I have included in our "Mass Update" section. What I want to do is if this flag is ever set to "yes", then I want to update this field. But I also want to automatically update another one called "decision_sent_date" with the current timestamp. Is this possible?
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

  2. #2
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: How to update a non-mass update field on a mass update

    This is possible through logic_hook before_save on your module.
    You must evaluate if:
    • action is MassUpdate
    • if the field decision_sent had been modified


    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  3. #3
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default Re: How to update a non-mass update field on a mass update

    Interesting. I didn't know you could use logic hooks on a "Mass Update".

    Two quick follow-ups:

    1) How do I evaluate the action being "Mass Update"? Is this passed as part of $bean in my hook?

    2) Is there a quick way to determine if this field was modified? The only way I can see accomplishing this is doing a check in a "Before Save" hook and then checking again in "After Save" to see which values have changed.

    Thanks for your help!

    -edit-
    Disregard question #1. I figured that out. It's value comes from $_REQUEST['action'].
    Last edited by hadoob024; 2010-12-14 at 04:15 PM. Reason: answered my own question
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

  4. #4
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: How to update a non-mass update field on a mass update

    Actually you are going to define a logic_hook before_save to module under which you will trigger MassUpdate.
    MassUpdate will retrieve and save each record, so all logic_hooks regarding retrieve and save are triggered.
    You must to check:

    PHP Code:
    if($_REQUEST['action'] == 'MassUpdate') {
      
    whathever you want

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  5. #5
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default Re: How to update a non-mass update field on a mass update

    Got it. Yeah, I saw where to check for the 'action'. I still do have a question about checking whether the value of "decision_sent" was changed to "1". Is there something that tells me that this field was modified? Or do I need to check it's value first in a "before_save" hook and then later in a "after_save" hook?
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

  6. #6
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: How to update a non-mass update field on a mass update

    Lets suppose the object being saved is the $bean

    PHP Code:
    if($bean->decision_sent != $bean->fetched_row['decision_sent']) {
      
    whathever you want

    fetched_row stores the values previously retrieved from database. They will be empty for a new record.

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  7. #7
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default Re: How to update a non-mass update field on a mass update

    Brilliant! That works perfectly. Here's what I have:

    PHP Code:
             if ($_REQUEST['action'] == "MassUpdate") {
                if (
    $bean->decision_sent != $bean->fetched_row['decision_sent']) {
                    if (
    $bean->decision_sent == 1) {
                        
    $d_s_query "UPDATE ipg_patient_procedure SET decision_sent_date = GETDATE() where id = '".$bean->id."'";
                        
    $db->query($d_s_query);
                    }
                    elseif (
    $bean->decision_sent == 0) {
                        
    $d_s_query "UPDATE ipg_patient_procedure SET decision_sent_date = NULL where id = '".$bean->id."'";
                        
    $db->query($d_s_query);
                    }
                }
             } 
    Just got to debug why my datetime field is not getting populated.
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

  8. #8
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default Re: How to update a non-mass update field on a mass update

    Quick follow-up to this issue. It seems that whatever I run in my query seems to fail. Not sure why. The query itself is properly formatted and it runs without any error. However, when I check my tables, I can see the query doesn't run. If I take my query and run it manually in SQL Server, then it works fine. Any thoughts on why this could be happening?
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

  9. #9
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: How to update a non-mass update field on a mass update

    Quote Originally Posted by hadoob024 View Post
    Brilliant! That works perfectly. Here's what I have:

    PHP Code:
             if ($_REQUEST['action'] == "MassUpdate") {
                if (
    $bean->decision_sent != $bean->fetched_row['decision_sent']) {
                    if (
    $bean->decision_sent == 1) {
                        
    $d_s_query "UPDATE ipg_patient_procedure SET decision_sent_date = GETDATE() where id = '".$bean->id."'";
                        
    $db->query($d_s_query);
                    }
                    elseif (
    $bean->decision_sent == 0) {
                        
    $d_s_query "UPDATE ipg_patient_procedure SET decision_sent_date = NULL where id = '".$bean->id."'";
                        
    $db->query($d_s_query);
                    }
                }
             } 
    Just got to debug why my datetime field is not getting populated.

    I don´t suggest you to directally run a sql query on database, let sugar do that.
    Additionally, I believe this logic_hook is a before_save one, so the record does not exist yet on database, event the id is populated.
    On a logic_hook context all fields are unformated, that means ready to save into database, so you can setup the datetime field just like that:

    PHP Code:
    $decision_sent_date date($GLOBALS['timedate']->dbDayFormat); 
    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  10. #10
    hadoob024's Avatar
    hadoob024 is offline Sugar Community Member
    Join Date
    Mar 2010
    Posts
    137

    Default Re: How to update a non-mass update field on a mass update

    OK. Yeah, I see what you mean. My code did in fact properly update the decision_sent_date field. The problem was though that in SugarBean.php, when it runs it's UPDATE query, it overwrites the value that I stored and replaces it with NULL. Not sure why or if this is a known Sugar issue.

    Anyway, I tried your suggestion and it does help. The only problem is that the value that gets stored for decision_sent_date on my last run was "2000-01-01 05:00:00.000". Any thoughts?

    I thought it might be a formatting issue, so I changed your command to now read:
    PHP Code:
    $bean->decision_sent_date date("Y-m-d H:i:s",$GLOBALS['timedate']->dbDayFormat); 

    But that didn't work either. I end up with a value of NULL if I write the command that way. Any thoughts on why this is happening?

    Also, is there anyway to determine what the user's datetime settings are so that I don't have to hardcode in this value? Something similar to $GLOBALS['timedate']->dbDayFormat, but for the user's setting and not the db setting?
    Last edited by hadoob024; 2010-12-15 at 04:44 PM.
    -hadoob024-


    Windows 7 SP1
    PHP 5.2.17
    SQL Server 2008 R2
    IIS 7.5
    Sugar Professional Version 5.5.1

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. mass update on checkbox field
    By aleroot87 in forum Developer Help
    Replies: 7
    Last Post: 2012-04-13, 12:05 AM
  2. Replies: 11
    Last Post: 2011-05-17, 02:14 AM
  3. Integer Field Mass Update
    By lmiller in forum Help
    Replies: 2
    Last Post: 2011-04-09, 08:04 PM
  4. How to add mass update field?
    By jim.thornton in forum Developer Help
    Replies: 10
    Last Post: 2009-02-22, 08:21 PM
  5. Add Custom Field to Mass Update
    By sullivanjc in forum Developer Help
    Replies: 10
    Last Post: 2006-07-31, 06:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •