Changing the template variables to reflect sale owner, and not the document creator

Hi Guys, a customer of mine asked me the following question:

 

"How can i specify the signature of a document to be the sale owner, and not the signature of the user creating the document?"

 

I immediately answered, that there probably was template variables that addressed that, but after some research we found out that there actually wasn't a way to get sale owner properties (such as title, phone number and email) into a document, the only available template variable is saau, which prints the full name of the sale owner, but no other details are exposed as template variables.

So i though that i might be able to change the associate to the sale owner associate before document generation via a "before save" trigger.

So i tried to write one, but it seems like the document is generated before the "Before save" trigger is called.

This is my code:

#setLanguageLevel 3;

EventData ed = getEventData();
Integer saleId = ed.getInputValue('CurrentEntity.Sale.SaleId').toInteger();

if(saleId > 0){
  NSSaleAgent sAgent;
  NSSaleEntity sale = sAgent.GetSaleEntity(saleId);
  NSAssociate associate = sale.GetAssociate();
  ed.setOutputValue('CurrentEntity.Associate.AssociateId', associate.GetAssociateId().toString());
  ed.setOutputValue('CurrentEntity.Associate.Deleted', associate.GetDeleted().toString());
  ed.setOutputValue('CurrentEntity.Associate.EjUserId', associate.GetEjUserId().toString());
  ed.setOutputValue('CurrentEntity.Associate.GroupIdx', associate.GetGroupIdx().toString());
  ed.setOutputValue('CurrentEntity.Associate.Name', associate.GetName());
  ed.setOutputValue('CurrentEntity.Associate.PersonId', associate.GetPersonId().toString());
  ed.setOutputValue('CurrentEntity.Associate.Type', associate.GetType().toString());
  ed.setOutputValue('DocumentEntity.Associate.AssociateId', associate.GetAssociateId().toString());
  ed.setOutputValue('DocumentEntity.Associate.Deleted', associate.GetDeleted().toString());
  ed.setOutputValue('DocumentEntity.Associate.EjUserId', associate.GetEjUserId().toString());
  ed.setOutputValue('DocumentEntity.Associate.GroupIdx', associate.GetGroupIdx().toString());
  ed.setOutputValue('DocumentEntity.Associate.Name', associate.GetName());
  ed.setOutputValue('DocumentEntity.Associate.PersonId', associate.GetPersonId().toString());
  ed.setOutputValue('DocumentEntity.Associate.Type', associate.GetType().toString());

}

I am overwriting both CurrentEntity and DocumentEntity - as i was unsure which one i needed to overwrite in order to achieve the desired outcome. It did not change anything though :(

So can anyone of you confirm the document is generated before the "Before save" trigger is called, and could you perhaps accept a wish with new sale template variables containing information about the sale owner?

 

Thanks :) 

 

RE: Changing the template variables to reflect sale owner, and not the document creator

Hi Dennis,

Perhaps you could consider using extra document UDEF's which are prefilled during the before save trigger with the Sales data that you would like to use? That should definately work.

Af: Bas Kreijveld 21. jun 2021

RE: Changing the template variables to reflect sale owner, and not the document creator

Thanks for the suggestion - a fine work around :)

I've create fields on the more page, corresponding to the fields i would like to change

updated the document:

 

and edited the trigger:

#setLanguageLevel 3;

EventData ed = getEventData();
Integer saleId = ed.getInputValue('CurrentEntity.Sale.SaleId').toInteger();
NSSaleAgent sAgent;
NSAssociateAgent aAgent;
NSAssociate associate;
NSPersonAgent pAgent;
NSPersonEntity person;

//If there's a sale on the document, use the sale owner details. otherwise, get the associate creating the document.
if(saleId > 0){

  NSSaleEntity sale = sAgent.GetSaleEntity(saleId);
  associate = sale.GetAssociate();
  person = pAgent.GetPersonEntity(associate.GetPersonId());

}
else{
  User current = getActiveUser();
  associate = aAgent.GetAssociate(current.getValue('associateId').toInteger());
  person = pAgent.GetPersonEntity(associate.GetPersonId());

}

//Set the output values for the userdefined fields:
ed.setOutputValue('CurrentEntity.UserDefinedFields.auth', associate.GetFullName());
ed.setOutputValue('CurrentEntity.UserDefinedFields.atit', person.GetTitle());


//Including phone and email:
NSEntityElement[] mobilePhones = person.GetMobilePhones();
if(mobilePhones.length() > 0)
	ed.setOutputValue('CurrentEntity.UserDefinedFields.aupm', mobilePhones[0].GetStrippedValue());

NSEntityElement[] emails = person.GetEmails();
if(emails.length() > 0)
	ed.setOutputValue('CurrentEntity.UserDefinedFields.auem', emails[0].GetStrippedValue());

And that did the trick - but i think the wish about making standard template variables would still make good sense :) 

Af: Dennis Mortensgaard 21. jun 2021

RE: Changing the template variables to reflect sale owner, and not the document creator

If you don't want those UDEFs visible on the sale, you can put it to be at like X = -200 so it ends up being outside the visible area.

Af: Patrik Larsson 21. jun 2021

RE: Changing the template variables to reflect sale owner, and not the document creator

Sure, i know :)

 

Sidenote: They are not visible on the sale, just on the document itself - and they are not in the way there, which is why i didn't hide them :)

Af: Dennis Mortensgaard 21. jun 2021