Record created from mobile app or web

Hi,

is there a way to know if a record has been created from the mobile app or from the web?

If not, do you think we can achieve this by adding a custom field with a logic that writes app or web depending on the entry point?

Thanks

Omar

Parents
  • Hello Omar,

    That’s a great question! 
    I explored several ways to achieve this, and I believe the best approach is to create a custom SugarLogic formula function.

    This method requires adding one custom PHP file, and once in place, you can apply it to any field using Studio — no further code changes per module needed.

    I’ve named the new function currentUserPlatform(), and it retrieves the platform used at login.

    By default, the web interface uses the base platform, the mobile app uses the mobile platform, and you can also define custom platform names for your integrations. (like postman for example)

    This function lets you automatically capture and record which platform the user is on.


    1 - Create the custom file: 

    custom/include/Expressions/Expression/Generic/CurrentUserPlatformExpression.php

    <?php
    require_once 'include/Expressions/Expression/String/StringExpression.php';
    
    class CurrentUserPlatformExpression extends StringExpression
    {
        /**
         * Returns the raw platform name used in the current request.
         * Examples: base, mobile, portal, Integration123, etc.
         */
        public function evaluate()
        {
            $platform = null;
    
            // Preferred: from REST service object
            if (isset($GLOBALS['service'])) {
                if (method_exists($GLOBALS['service'], 'getPlatform')) {
                    $platform = $GLOBALS['service']->getPlatform();
                } elseif (property_exists($GLOBALS['service'], 'platform')) {
                    $platform = $GLOBALS['service']->platform;
                }
            }
    
            // Fallbacks for non-REST contexts or integrations
            if (!$platform && !empty($_SERVER['HTTP_X_SUGAR_PLATFORM'])) {
                $platform = $_SERVER['HTTP_X_SUGAR_PLATFORM'];
            }
            if (!$platform && !empty($_SERVER['HTTP_SUGAR_PLATFORM'])) {
                $platform = $_SERVER['HTTP_SUGAR_PLATFORM'];
            }
            if (!$platform && !empty($_SERVER['HTTP_X_SUGAR_CLIENT'])) {
                $platform = $_SERVER['HTTP_X_SUGAR_CLIENT'];
            }
            if (!$platform && !empty($_SERVER['HTTP_SUGAR_CLIENT'])) {
                $platform = $_SERVER['HTTP_SUGAR_CLIENT'];
            }
    
            // As a last fallback, SugarCRM Mobile app often includes its signature in the User-Agent
            if (
                !$platform &&
                !empty($_SERVER['HTTP_USER_AGENT']) &&
                stripos($_SERVER['HTTP_USER_AGENT'], 'SugarCRM Mobile') !== false
            ) {
                $platform = 'mobile';
            }
    
            // Default to 'base' if nothing is found
            return trim($platform ?: 'base');
        }
    
        public static function getJSEvaluate()
        {
            // Client side: used only in Studio previews
            return <<<JS
                return "base";
            JS;
        }
    
        public static function getOperationName()
        {
            return ['currentUserPlatform'];
        }
    
        public static function getParameterTypes()
        {
            return [];
        }
    
        public static function getParamCount()
        {
            return 0;
        }
    }
    


    2 - Run Repairs and Clear Cache 

    Go to Admin → Repair → Quick Repair and Rebuild and Repair Sugar Logic Functions. 
    Clear the cache of your browser to make sure it loads the new function 

    3  - In Studio, you should now be able so see the new function in the formula picker 


    4 - Add the desired formula to the field that you need.

    Here’s a simple example that sets the platform only when the record is first created,
    preventing it from being overwritten on edits:


    ifElse(equal($description,""),currentUserPlatform(),$description)


    I've added in the description filed and now it's showing the platform that was used on creation: 




    I’ve done limited testing, so please validate it in your environment to ensure it behaves as expected

    Let me know if this is what you are looking for and if it works for you. 


    Cheers, 

    André 



Reply
  • Hello Omar,

    That’s a great question! 
    I explored several ways to achieve this, and I believe the best approach is to create a custom SugarLogic formula function.

    This method requires adding one custom PHP file, and once in place, you can apply it to any field using Studio — no further code changes per module needed.

    I’ve named the new function currentUserPlatform(), and it retrieves the platform used at login.

    By default, the web interface uses the base platform, the mobile app uses the mobile platform, and you can also define custom platform names for your integrations. (like postman for example)

    This function lets you automatically capture and record which platform the user is on.


    1 - Create the custom file: 

    custom/include/Expressions/Expression/Generic/CurrentUserPlatformExpression.php

    <?php
    require_once 'include/Expressions/Expression/String/StringExpression.php';
    
    class CurrentUserPlatformExpression extends StringExpression
    {
        /**
         * Returns the raw platform name used in the current request.
         * Examples: base, mobile, portal, Integration123, etc.
         */
        public function evaluate()
        {
            $platform = null;
    
            // Preferred: from REST service object
            if (isset($GLOBALS['service'])) {
                if (method_exists($GLOBALS['service'], 'getPlatform')) {
                    $platform = $GLOBALS['service']->getPlatform();
                } elseif (property_exists($GLOBALS['service'], 'platform')) {
                    $platform = $GLOBALS['service']->platform;
                }
            }
    
            // Fallbacks for non-REST contexts or integrations
            if (!$platform && !empty($_SERVER['HTTP_X_SUGAR_PLATFORM'])) {
                $platform = $_SERVER['HTTP_X_SUGAR_PLATFORM'];
            }
            if (!$platform && !empty($_SERVER['HTTP_SUGAR_PLATFORM'])) {
                $platform = $_SERVER['HTTP_SUGAR_PLATFORM'];
            }
            if (!$platform && !empty($_SERVER['HTTP_X_SUGAR_CLIENT'])) {
                $platform = $_SERVER['HTTP_X_SUGAR_CLIENT'];
            }
            if (!$platform && !empty($_SERVER['HTTP_SUGAR_CLIENT'])) {
                $platform = $_SERVER['HTTP_SUGAR_CLIENT'];
            }
    
            // As a last fallback, SugarCRM Mobile app often includes its signature in the User-Agent
            if (
                !$platform &&
                !empty($_SERVER['HTTP_USER_AGENT']) &&
                stripos($_SERVER['HTTP_USER_AGENT'], 'SugarCRM Mobile') !== false
            ) {
                $platform = 'mobile';
            }
    
            // Default to 'base' if nothing is found
            return trim($platform ?: 'base');
        }
    
        public static function getJSEvaluate()
        {
            // Client side: used only in Studio previews
            return <<<JS
                return "base";
            JS;
        }
    
        public static function getOperationName()
        {
            return ['currentUserPlatform'];
        }
    
        public static function getParameterTypes()
        {
            return [];
        }
    
        public static function getParamCount()
        {
            return 0;
        }
    }
    


    2 - Run Repairs and Clear Cache 

    Go to Admin → Repair → Quick Repair and Rebuild and Repair Sugar Logic Functions. 
    Clear the cache of your browser to make sure it loads the new function 

    3  - In Studio, you should now be able so see the new function in the formula picker 


    4 - Add the desired formula to the field that you need.

    Here’s a simple example that sets the platform only when the record is first created,
    preventing it from being overwritten on edits:


    ifElse(equal($description,""),currentUserPlatform(),$description)


    I've added in the description filed and now it's showing the platform that was used on creation: 




    I’ve done limited testing, so please validate it in your environment to ensure it behaves as expected

    Let me know if this is what you are looking for and if it works for you. 


    Cheers, 

    André 



Children