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 "https://theothersystem.com/?id={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.

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

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?

Thanks!

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

    <?php

    $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.

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

    <?php

    $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.

Children
No Data