Update parent field using formula
Collapse
X
-
No worries, sort it out just using formula, few functions got the job done.
I really appreciate your concern and support, thank you.
Rabii -
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
RabiiLeave a comment:
-
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
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.Code:$GLOBALS['log']->warning('ChatForumAdmin.php Service - createLinks() #201 created file: ',[$entityDefsFilePath]);Last edited by telecastg; 07-02-2021, 09:49 PM.Leave 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 -
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 :
I hope this will help anyone in need.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); );
Thanks
RabiiLast edited by rabii; 07-13-2021, 01:08 AM.Tags: None

Leave a comment: