CRMScript + company address

Hi everyone

We try to set address fields in the before saving company event. Basically, we simply want to copy street address to postal address if postal address is not set by the user.

#setLanguageLevel 3;
EventData ed = getEventData();
ed.setMessage(ed.getInputValue("ContactEntity.Address.LocalizedAddress[0][0].Value")); // Read address fields: OK
ed.setOutputValue("ContactEntity.Address.LocalizedAddress[0][0].Value","Hello");  // Write address fields: issues error (object does not exist)
ed.setOutputValue("ContactEntity.UserDefinedFields.SuperOffice:2","XX");  // Write udef field: OK

Thus, we can read address fields, as well as read and write user defined fields. However, whenever we attempt to write an address field an error appears, and the company is left unchanged.

Interestingly, we are able to set address fields of a ContactEntity object as expected, e.g. in the after saving company event. But then the object is not refreshed by the browser, so the user does not see the effect.

1. Why does setOutputValue("ContactEntity.Address.LocalizedAddress…) not work?

2. Is it possible to force a refresh of the browser in an after saving event? And if so, how?

Best regards
Marc

RE: CRMScript + company address

Hi Marc,

Not sure about 1., but for 2:

You could try the following at the end

ed.setNavigateTo("soprotocol:company");

Av: Hans Wilhelmsen 16. okt 2017

RE: CRMScript + company address, SoProtocol

Hi Hans,

thanks for the hint setNavigateTo. It should read ed.setNavigateTo("soprotocol:contact...") though, see here.

ed.setNavigateTo("soprotocol:refresh");  // Works, but reloads the entire page
ed.setNavigateTo("soprotocol:contact.main?refresh");  // Does not work

You can set the context to another company by using ed.setNavigateTo("soprotocol:contact.main?contact_id=123");

If you set the context to the current company then the contact card is not refreshed, because the context does not change. Also, if you switch to the minicard or the person archive (and back to the company card) the company card is not refreshed.

Maybe someone has a better idea. The goal is to refresh the company card only.

Best regards
Marc

 

Av: Marc Eberhard 17. okt 2017

RE: CRMScript + company address

Hi Marc,

what about

ed.setNavigateTo("soprotocol:contact.main[refresh=true]"); 

 

Best,

Dennis

Av: Dennis Pabst 17. okt 2017

RE: CRMScript + company address

Hi Dennis,

works fine, thanks for the hint :-)

Marc

Av: Marc Eberhard 17. okt 2017

RE: CRMScript + company address

Hi,

Just hit the same problem as described by Marc, when setting the localizedaddress value we get an error.

 

Is there a way to set the address in the before Company Save trigger?

For now we update the data using the after save event, and reload the GUI using the soprotocol provided above.

Thanks,

David

Av: David Hollegien 26. mai 2018

RE: CRMScript + company address

Addresses are probably one of the most complicated structures to work with in the API's. Please see this article in the Customer Service documentation for more insight and assistance.

Av: Tony Yates 28. mai 2018

RE: CRMScript + company address

Here is a trigger "after save company" to copy all address elements named "Street" to the corresponding "Postal" elements, if "Postal" elements are empty.
It uses the approach suggested by Tony. If a street address element is copied to the respective postal address element then the browser is forced to display the main card of the company, so the user can see the effect.
The trigger works fine for our purposes.

#setLanguageLevel 3;

// Adapted from https://community.superoffice.com/documentation/SDK/SO.Customer.Service.Support/html/addressFormats.htm
Map validAddressTypes;
validAddressTypes.insert("PostalAddress1", "");
validAddressTypes.insert("PostalAddress2", "");
validAddressTypes.insert("PostalAddress3", "");
validAddressTypes.insert("PostalCity", "");
validAddressTypes.insert("PostalCounty", "");
validAddressTypes.insert("PostalState", "");
validAddressTypes.insert("PostalZipcode", "");
validAddressTypes.insert("StreetAddress1", "");
validAddressTypes.insert("StreetAddress2", "");
validAddressTypes.insert("StreetAddress3", "");
validAddressTypes.insert("StreetCity", "");
validAddressTypes.insert("StreetCounty", "");
validAddressTypes.insert("StreetState", "");
validAddressTypes.insert("StreetZipcode", "");

//Get the addresses of a contact as values in a map.
Map GetContactAddresses(NSContactEntity contact)
{
  NSAddress contactAddress = contact.GetAddress();
  NSLocalizedField[][] fields = contactAddress.GetLocalizedAddress();
  Map addr;
  for (Integer i=0; i < fields.length(); i++)
  {
    for (Integer j=0; j < fields[i].length(); j++)
    {
      addr.insert(fields[i][j].GetName(), fields[i][j].GetValue());
    }
  }
  return addr;
}

NSContactEntity SetContactAddresses(NSContactEntity contact, Map addresses)
{
  //For troubleshooting - detect illegal addresstypes.
  for (addresses.first(); !addresses.eof(); addresses.next())
    if (!validAddressTypes.exists(addresses.getKey()))
      exitWithMessage("Illegal address type: " + addresses.getKey());
  NSAddress contactAddress = contact.GetAddress();
  NSLocalizedField[][] fields = contactAddress.GetLocalizedAddress();
  for (Integer i = 0; i < fields.length(); i++)
  {
    for (Integer j = 0; j < fields[i].length(); j++)
    {
      if (addresses.exists(fields[i][j].GetName()))
      {
        fields[i][j].SetValue(addresses.get(fields[i][j].GetName()));
      }
    }
  }
  contactAddress.SetLocalizedAddress(fields);
  contact.SetAddress(contactAddress);
  return contact;
}

Bool copyCompanyAddress(Integer contactId, String fromType, String toType)
{
  Bool result = false;
  NSContactAgent ca;
  NSContactEntity contactEntity = ca.GetContactEntity(contactId);
  Map addresses = GetContactAddresses(contactEntity);
  Map newAddresses;
  for (addresses.first(); !addresses.eof(); addresses.next())
  {
    String key = addresses.getKey();
    if (key.beginsWith(fromType))
    {
      String value = addresses.getVal();
      newAddresses.insert(key, value);
      String newKey = key.substitute(fromType, toType);
      if (addresses.exists(newKey))
      {
        String newValue = addresses.get(newKey);
        if (newValue.isEmpty())
        {
          if (!newValue.equals(value))
          {
            newAddresses.insert(newKey, value);
            result = true;
          }
        }
      }
    }
  }
  if (result)
  {
    contactEntity = SetContactAddresses(contactEntity, newAddresses);
    ca.SaveContactEntity(contactEntity);
  }
  return result;
}

EventData ed = getEventData();
String contactId = ed.getInputValue("ContactEntity.ContactId");
if (copyCompanyAddress(contactId.toInteger(), "Street", "Postal"))
{
  ed.setNavigateTo("soprotocol:contact.main[refresh=true]");  //Refresh front end
}
Av: Marc Eberhard 31. mai 2018