Calculate field with line breaks

Hi, I'm trying to create a calculated/concatenated field that pulls fields from related records and would like to have line breaks between the field values, not spaces? As an example, I'm trying to pull the contacts company address fields and the contacts phone numbers into a text area field, I'd like line breaks between each line of the address and the phone numbers to be on seperate lines

I have tried using \n\r and \\n\\r in my calculated concat field but it treats them as a string. If it is not possible in a TextArea field, is it possible in an HTML  field and last resort I suppose would be an aftersave logichook

Thanks for any assistance you can give me

  • Hi Rob,

    Unfortunately, there is not currently a way to use Sugar Logic to add a newline into a field, and an HTML field cannot use calculated formulas. A logic hook is going to be your best bet. Here is how I got it working in a logic hook:

    // custom/modules/Accounts/logic_hooks.php
    <?php
    $hook_version = 1;
    $hook_array = Array();
    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(1, 'Store values', 'custom/modules/Accounts/My_Logic_Hooks.php', 'My_Logic_Hooks', 'before_save_method' );
    
    // custom/modules/Accounts/My_Logic_Hooks.php
    <?php 
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class My_Logic_Hooks {
      function before_save_method($bean, $event, $arguments) {
      $bean->test_calc_newline_c = $bean->annual_revenue . "\r" . $bean->name . "\n" . $bean->account_type; // both of these worked
      }
    }
    

    I hope that helps!

    -Alan

  • Hey Alan, thanks for the response, got me started , heading in the right direction...

    The record/module that I'm running the logic in is a custom module, I have the following code which works but.... it doesn't allow me to get at the contacts_cstm table OR the OR the accounts tables (related to the Contact) OR email address tables. I've read some stuff online about using the bean to load related records, is that applicable here? and if so, any chance of giving me a pointer to resources detailing how to use it? Any 'further' assistance, greatly appreciated (the variable $mba_detail_id in the SQL is the id of the bean currently loaded, the record I'm working with)

    Thanks much

    <?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class copyProfileDetails {

    function before_save_method($bean, $event, $arguments) {

    global $db;

    $mba_detail_id = $bean->id;

    $query = "select * from contacts inner join contacts_mba_mba_detail_1_c on contacts_mba_mba_detail_1_c.contacts_mba_mba_detail_1contacts_ida = contacts.id and contacts_mba_mba_detail_1_c.contacts_mba_mba_detail_1mba_mba_detail_idb = '$mba_detail_id' and contacts_mba_mba_detail_1_c.deleted = 0; ";

            $result_contact = $db->query($query);

            if($row_contact = $db->fetchByAssoc($result_contact)){

            // Update contact details in here

                $c_name = "Name: ".$row_contact['first_name'].' '.$row_contact['last_name'];

                $c_phone = "Phone: ".$row_contact['phone_mobile'];

                $c_email = "Testing: ".$row_contact['first_name'].' '.$row_contact['last_name'];

              

                $bean->contact_profile = $c_name . "\r" . $c_phone . "\n" . $c_email;

            }

      // Update employer details

            //$bean->employer_profile = "Hello\r" . $bean->name . "Whats up\n" . $bean->account_type;

      }

    }

  • He Adam, so I did some more work on it, code below, appears to be working OK... is there any way using related records to drop the SQL out of the function entirely?

    Thanks for your assistance...

    <?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class copyProfileDetails {

      function before_save_method($bean, $event, $arguments) {

      // Updated contact details

      //

      global $db;

      $mba_detail_id = $bean->id;

      $query = "select contacts.id from contacts inner join contacts_mba_mba_detail_1_c on contacts_mba_mba_detail_1_c.contacts_mba_mba_detail_1contacts_ida = contacts.id and contacts_mba_mba_detail_1_c.contacts_mba_mba_detail_1mba_mba_detail_idb = '$mba_detail_id' and contacts_mba_mba_detail_1_c.deleted = 0; ";

            $result_contact = $db->query($query);

            if($row_contact = $db->fetchByAssoc($result_contact)){

            // Update contact details in here

                $c_id = $row_contact['id'];

                $focus = new Contact();

                $focus->retrieve($c_id);

                $c_name = "Name: ".$focus->name;

                $c_phone = "Phone: ".$focus->phone_mobile;

                $c_studno = "Student Number: ".$focus->stud_id_c;

                $c_graduated = "Year Graduated: ".$focus->yog_c;

                $c_address = "Personal Address Details"."\r".$focus->personal_address_street_c."\r".$focus->personal_address_city." ".$focus->personal_address_postalcode." ".$focus->personal_address_state."\r".$focus->personal_address_country;

               

                $bean->contact_profile = $c_name . "\r" . $c_phone . "\r" . $c_studno . "\r" . $c_graduated . "\r\r" . $c_address;

            }

      // Update employer details

            //$bean->employer_profile = "Hello\r" . $bean->name . "Whats up\n" . $bean->account_type;

      }

    }

  • Hi Rob,

    You can check out our documentation on Fetching Relationships with SugarBean objects. For your example, I would do something like this:

    if($bean->load_relationship('contacts_mba_mba_detail_1_c')) {
      $relatedBeans = $bean->$link->getBeans();
      foreach($relatedBeans as $relatedBean) {
        $c_id = $relatedBean->id;
        // The rest of your code can go here
      }
    }
    

    The exact name of the relationship may vary, so you will have to verify that in order for this to work. Let me know if you have any questions!

    -Alan

  • Hey Alan, thanks for all you help and the link to your online resources, once I found the proper relationship name and swapped out the $link variable, all good

    Cheers