How to retrieve the generated URL value for a URL field type using the "generate" option?

The Sugar URL field type has a nice feature where it will generate a URL for you based on other fields on the record. For example, if you want to link to another system and pass the contact's Sugar record ID, you could have a custom field use the template "{id}" and when you view the record in Sugar, the link field will automatically generate the {id} part so that when you click the link it is properly filled in.

I'm wanting to retrieve this generated link value and use it in my custom code, the only issue is that when you pull the bean, the field does not give you the generated value. It will give you the template.

// This doesn't work
// say I have a field, generated_url_c and it should be{id}
$record = BeanFactory::getBean('Contacts','1234-1234-1234-1234');
echo $record->generated_url_c;
// this will return{id}
// not

How do I get the generated URL value on the server side? Is there a method we can run these URL fields through to get the generated value?


  • It looks like the apiFormatField() method is what needs to be called in includes/SugarFields/Fields/Link/SugarFieldLink.php. In that method you can see it checks the 'gen' value to see if it is flagged then runs a replace_sugar_vars() method to do the replacement.

    Any ideas how to run that method? Or is there a generic method you can run on a $bean that will run the apiFormatField() method for all fields on the bean?

    I'm guessing that is what is run whenever Sugar sends a $bean to the client for display in sidecar?

  • I went down the rabbit hole, and I think I figured it out. You have to do something like this:


    $bean = BeanFactory::getBean('Contacts','1234-1234-1234-1234');

    $sfh = new \SugarFieldHandler();
    $properties = $bean->field_defs[$fieldName];
    $type = $properties['type']; // this will be 'url'
    $data = array();
    $options = array();
    $fieldName = 'generate_url_c';

    $field = $sfh->getSugarField($type);
    $field->apiFormatField($data, $bean, $options, $fieldName, $properties);

    $data['generate_url_c'] // <--- this will hold the generated value

    This works if you want to format a single field. There are some more generic methods that will format all fields on a bean for you if you need others. Look at the formatBean() method in include/api/SugarApi.php if that's what you need.