SuperOfficeDocs / superoffice-docs

Contains docs about SuperOffice products.
MIT License
6 stars 18 forks source link

Saving entities best-practice #1181

Closed SuperOfficeDevNet closed 1 month ago

SuperOfficeDevNet commented 1 month ago

Is there an existing issue for this?

What kind of idea is it?

New page on SuperOfficeDocs

Who is it for?

Consultant

Which area (SuperOffice entity) is this related to?

Other

Description

Need an article that highlights the optimized way of working in CRMScripts, specifically focused on how to NOT USE AGENTS when it's unnecessary.

Looking at several resources (forums, code examples, there are several unnecessary agent calls. Example (Saving a document entity):

NSDocumentAgent docAgent;
NSDocumentEntity doc = docAgent.CreateDefaultDocumentEntity();

doc.SetName(a.getValue("name"));
doc.SetHeader(a.getValue("name"));
doc.SetDate(getCurrentDateTime());

NSListAgent listAgent;
NSDocumentTemplate docTmpl = listAgent.GetDocumentTemplate(templateId);
doc.SetDocumentTemplate(docTmpl);

NSAssociateAgent assocAgent;
doc.SetAssociate(assocAgent.GetAssociate(associateId.toInteger()));

if (personId != "") {
  NSPersonAgent persAgent;
  doc.SetPerson(persAgent.GetPerson(personId.toInteger()));
}

if (contactId != "") {
  NSContactAgent contAgent;
  doc.SetContact(contAgent.GetContact(contactId.toInteger()));
}

if (saleId != "") {
  NSSaleAgent saleAgent;
  doc.SetSale(saleAgent.GetSale(saleId.toInteger()));
}

All of this is OVERKILL...

The better way, "best-practices", is to just set the child entity ID values - since they are the only properties looked at when saving the parent entity.

Integer templateId = 5;

NSDocumentAgent docAgent;
NSDocumentEntity doc = docAgent.CreateDefaultDocumentEntity();

NSDocumentTemplate docTmpl;
docTmpl.SetDocumentTemplateId(templateId)
doc.SetDocumentTemplate(docTmpl);

NSAssociate associate;
associate.SetAssociateId(associateId.toInteger());
doc.SetAssociate(associate);

if (personId != "") {
  NSPerson person;
  person.SetPersonId(personId.toInteger())
  doc.SetPerson(person);
} 

if (contactId != "") {
  NSContact contact;
  contact.SetContactId(contactId.toInteger());
  doc.SetContact(contact);
}

if (saleId != "") {
  NSSale sale;
  sale.SetSaleId(saleId.toInteger());
  doc.SetSale(sale);
}

Need a new page that describes the entity parent-child relationship and how to use that when setting child entities on a parent entity.