Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a variable to the workflow expression engine
#1
Since Business Maps use the workflow expression engine, I'd like to add a new expression. I need to make a business map that defaults the assigned to user to the current user when creating an Invoice from a SalesOrder. As far as I know, there is not expression to get the current user (ID). How would I add this?
Reply
#2
I did a quick test. By default the currect user is set in the assigned_to field. I converted a SO that was assigned to another user and when I arrive at the invoice the assigned_to field is set to my current user, not the one on the SO.
I created a map to change the subject and the assigned_to field was still set to the current user, so no change there.
I added a mapping to assign the Invoice to another user and that worked correctly also.
So, I'd say that you already have the behavior you are asking for.
In any case, the wiki says that you have access to the current user information using the "TEMPLATE" type:

http://corebos.org/documentation/doku.php?id=en:adminmanual:businessmappings:mapping

Code:
When creating a new record you always have access to all the fields of the current user using the FIELD or TEMPLATE types. For example:

$(assigned_user_id : (Users) first_name)


this it the mapping I created:


Code:
<map>
  <originmodule>
    <originname>SalesOrder</originname>
  </originmodule>
  <targetmodule>
    <targetname>Invoice</targetname>
  </targetmodule>
  <fields>
    <field>
      <fieldname>subject</fieldname>
      <Orgfields>
        <Orgfield>
          <OrgfieldName>subject</OrgfieldName>
        </Orgfield>
        <Orgfield>
          <OrgfieldName>sostatus</OrgfieldName>
        </Orgfield>
        <Orgfield>
          <OrgfieldName>_FromSO</OrgfieldName>
          <OrgfieldID>CONST</OrgfieldID>
        </Orgfield>
        <delimiter>;</delimiter>
      </Orgfields>
    </field>
    <field>
      <fieldname>assigned_user_id</fieldname>
      <Orgfields>
        <Orgfield>
          <OrgfieldName>5</OrgfieldName>
          <OrgfieldID>CONST</OrgfieldID>
        </Orgfield>
      </Orgfields>
    </field>
  </fields>
  </map>
Joe
TSolucio
Reply
#3
Hm, that's weird. In my install, the assigned user for the invoice defaults to the one on the SO it was created from...

I tried:

Code:
<field>
    <fieldname>assigned_user_id</fieldname>
    <Orgfields>
      <Orgfield>
        <OrgfieldName>$(assigned_user_id : (Users) id)</OrgfieldName>
<OrgfieldID>FIELD</OrgfieldID>
      </Orgfield>
     </Orgfields>
  </field>

But that gave very weird results. Any way I can add the 'id' field to the expression?
Reply
#4
This is really weird. I tested in my own install. There also an invoice will default to the user the SO is assigned to. In the coreBOS demo, the new invoice defaults to the current user. I don't understand why...


I even went as far as to set the $focus->column_fields['assigned_user_id'] in the editview.php of Invoices. But still no dice. I did find out that this only works for non-mandatory fields.
Reply
#5
I can confirm this is still not working as expected (July 2018 version)

Converting from an SO to an invoice WILL default the assigned user to the current one as far as I've tested. But when I use the following business map to control duplicates:
Code:
<map>
<originmodule>
  <originname>Invoice</originname>
</originmodule>
<targetmodule>
  <targetname>Invoice</targetname>
</targetmodule>
<fields>
  <field>
    <fieldname>assigned_user_id</fieldname>
    <Orgfields>
      <Orgfield>
        <OrgfieldName>$(assigned_user_id : (Users) id)</OrgfieldName>
        <OrgfieldID>TEMPLATE</OrgfieldID>
      </Orgfield>
     </Orgfields>
  </field>
</fields>
</map>

The invoice duplicate WILL default to another user, but not the current one, it defaults to a user that isn't even active. I also tried the above map with the EXPRESSION type.

Then I tried:
Code:
<map>
<originmodule>
  <originname>Invoice</originname>
</originmodule>
<targetmodule>
  <targetname>Invoice</targetname>
</targetmodule>
<fields>
  <field>
    <fieldname>assigned_user_id</fieldname>
    <Orgfields>
      <Orgfield>
        <OrgfieldName>$(assigned_user_id : (Users) first_name)</OrgfieldName>
        <OrgfieldID>TEMPLATE</OrgfieldID>
      </Orgfield>
      <Orgfield>
        <OrgfieldName>$(assigned_user_id : (Users) last_name)</OrgfieldName>
        <OrgfieldID>TEMPLATE</OrgfieldID>
      </Orgfield>
      <delimiter> </delimiter>
     </Orgfields>
  </field>
</fields>
</map>

With and without the delimiter. That defaulted to a group. One where the current user is not associated with.
Reply
#6
OK, I dug around a little. When I log the $user variable in this line (https://github.com/tsolucio/corebos/blob/master/modules/com_vtiger_workflow/VTEntityCache.inc#L16), I get the current user object as expected. But when I log the $data variable at the end of that __construct function, I get an array with the entity returned with the wrong assigned_user_id. My user ID is 6, but I get:

PHP Code:
[account_id] => 3x504
    
[invoicestatus] => Sent
    
[assigned_user_id] => 19x14
    
[createdtime] => 2013-01-03 08:48:48
    
[modifiedtime] => 2018-07-13 15:35:26 

I get 14 back.

And just for the heck of it I logged:

PHP Code:
    public function __construct($user$id$deleted false$queue false) {
        global 
$log;

        
$this->moduleName null;
        
$this->id $id;
        
$this->user $user;
        
$this->isDeleted=$deleted;
                
$this->isqueue=$queue;
        if (
$deleted) {
            
$data vtws_retrieve_deleted($id$user);
        } else {
            
$data vtws_retrieve($id$user);
            
$log->debug("FINDME");
            
$log->debug($user);
            
$log->debug($data);
        }
        if (!
is_null($data)) {
            foreach (
$data as $key => $value) {
                if (
is_string($value)) {
                    
$data[$key] = html_entity_decode($valueENT_QUOTES'utf-8');
                }
            }
        }
        
$this->data $data;
    } 
And again the user object ID is 6, but in data the assigned user id is 19x14, which is the user that this record originally is assigned to.
Reply
#7
I've come a BIG step closer to what is happening. It is because the difference in user ID versus webservice user id. The module users has a webservice ID of 19. No matter which approach I choose, the assigned user keeps getting set to user no. 19, because the business map doesn't take the webservice markup into account.

I've given up. I'll just revert back to my hack to default the user to the current one when duplicating records.
Reply
#8
See if this helps:

https://youtu.be/9-RlN6e31BQ
Joe
TSolucio
Reply
#9
Will do and get back to you

Thanks for the great video. This thread has - during time - gotten polluted with a lot of messed info by me. First of all, the original question was wrong. Partly because at the time I was working on an old installation (which I'm trying to upgrade now), which may have caused some effects that the more recent versions long fixed. Secondly, the question should not have been: how do I assign a user on conversion. That is working like we wanted out of the box now, and we have a lot of options to alter that behaviour now. My main problem is (and this should have been the original question): how do I get the salesorders and invoices to ALWAYS default to the currently logged in user when duplicating.

Say you have a salesorder that is assigned to user A. User B logs in and wants to duplicate this salesorder. I want to make sure that when user B hits the duplicate button and the new, to be created salesorder opens in the edit screen, that salesorder is by default assigned to user B. Not user A, who was the assigned to user in the original salesorder. To be clear: I don't want the user related to a record, I want the currently logged in user (which we would get by
PHP Code:
global $current_user->id 
when we code.
Reply
#10
Try with this little gem:

Code:
    <field>
      <fieldname>assigned_user_id</fieldname>
      <Orgfields>
        <Orgfield>
          <OrgfieldName>$(general : (__WorkflowFunction__) getCurrentUserField('id') ) </OrgfieldName>
          <OrgfieldID>TEMPLATE</OrgfieldID>
        </Orgfield>
      </Orgfields>
    </field>
Joe
TSolucio
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)