Update parent field using formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    Update parent field using formula

    Hey all,

    I have a parent field on lead called (salesChannel) the parent field has Account - User, I tried to update this parent field when a lead is created (if lead is created by portal user I set up the salesChannelType = "Account" - salesChannelId = createdBy.accountId - salesChannelName = createdBy.accountName but it doesn't work (I want to update the sales channel to type account and link to account of the portal user). I have an account linked to the portal user but it won't work.


    NB: when a lead is created by a normal user the salesChannel field is updated as (salesChannelType = "User" - salesChannelId = createdById - salesChannelName = createdByName ) and it works just fine, only when I a lead is created by portal user then it won't update the salesChannel parent field. I am sure that the portal user has account set.

    Can anyone help please.

    Thanks

    Update: Solution

    I have solved the problem using formula existing function, below is my code to update parent field (salesChannel) with portal user's account :

    HTML Code:
    $type = "Account";
    
    // get account id of portal user (current createdBy) using createdBy.contactId on record\findRelatedOne
    $accountId = record\findRelatedOne('Contact', createdBy.contactId, 'accounts', 'createdAt', 'desc');
    
    // grab the name attribute of the found account of the portal user using record\attribute
    $name = record\attribute('Account', $accountId, 'name');
    
    ifThen($accountId,
    entity\setAttribute('salesChannelType', $type);
    entity\setAttribute('salesChannelName', $name);
    entity\setAttribute('salesChannelId', $accountId);
    );
    I hope this will help anyone in need.

    Thanks
    ​​​​​​​Rabii
    Last edited by rabii; 07-13-2021, 01:08 AM.
    Rabii
    Web Dev
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Hi,

    I don't use formula, but seeing that your formula works for normal users and not for portal users, suggests that the portal user might not have permission to edit the salesChannelType field.
    Last edited by telecastg; 07-02-2021, 08:34 PM.

    Comment


    • rabii
      rabii commented
      Editing a comment
      I have checked and the portal user has the right to edit the salesChannel field, it is very weird. I tried even to grab just the createdBy.accountId or createdBy.accountName and doesn't get any result although the current portal has an account and is created from a contact linked to this account. Do you know any other way around ? would hooks work for this ?

      Thanks
      Rabii
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #3
    You can use hooks to do anything that formula does.

    In fact formula is an Espo custom language that executes "beforeSave" hook actions, intended mostly for SaaS (cloud) users or GUI users who can not or do not wish to write their own code.

    You might try to re-create your formula actions with a hook instead and see if it works as you want it.

    One advantage in my opinion of using your own code, is that you can use statements like this
    Code:
     $GLOBALS['log']->warning('ChatForumAdmin.php Service - createLinks() #201 created file: ',[$entityDefsFilePath]);
    to print test points in the backend log (application/data/logs) similar to using "console.log" javascript statements, so you can find out where exactly is the code not giving you the result that you expect.
    Last edited by telecastg; 07-02-2021, 09:49 PM.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #4
      telecastg Hey,

      I have tried the hooks but didn't't work either, below is my code for salesChannel parent field. The portal user is linked to an account but I couldn't get it to work :

      public function beforeSave(Entity $lead, array $options = [])
      {
      $entityManager = $this->getEntityManager();

      $createdBy = $lead->get('createdById');
      $contactId = $entityManager->getRepository('Contact')->where(['id'=>$createdBy])->findOne();
      $account = $entityManager->getRepository('Account')->where(['contactId'=>$contactId])->findOne();

      $accountName = $account->get('name');
      $accountId = $account->get('id');

      if ($lead->isNew() && !$lead->get('salesChannel')) {
      $lead->set([
      'salesChannelType' => 'Account',
      'salesChannelName' => $accountName,
      'salesChannelId' => $accountId,
      ]);
      }
      }

      It weird that the createdBy.accountId doesn't get fetched although the user has a portal and is a contact who is linked to an account.

      Any help please.

      Thanks
      Rabii
      Rabii
      Web Dev

      Comment


      • telecastg
        telecastg commented
        Editing a comment
        I'm sorry but I can't help with code debugging. What I suggest is that you inject $GLOBALS['log']->warning statements to test the logic of each step to find out where the expected value is not being fetched.
        Last edited by telecastg; 07-11-2021, 06:54 PM.
    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #5
      No worries, sort it out just using formula, few functions got the job done.

      I really appreciate your concern and support, thank you.

      Rabii
      Rabii
      Web Dev

      Comment


      • telecastg
        telecastg commented
        Editing a comment
        You're welcome
    Working...