SalesLogix 8 – Validating 1:M Data from an Editable Grid before insert
Posted By: Alex.Cottner on October 16th, 2013 in General, Saleslogix
No Gravatar

I ran into a problem recently where I needed to have a 1:M editable grid (contacts) on an insert page (opportunity). The data in that grid needed to go through validation along with the base entity before any records could be inserted. However, this isn’t the way insert pages in SalesLogix 8 normally work. Normally, the base entity is saved first. Once it is saved the 1:M records in the editable grid are then saved all at once (via the editable grid’s “InsertAssociationAction” method).

This wouldn’t work for me. If one of the validation rules for the 1:M data threw an exception, then the user was stuck on the insert page with partially saved data. The base entity already existed in the database but the 1:M grid data hasn’t been saved yet.

The easiest way to resolve this is to take the code from the grid’s “InsertAssociationAction” and move it into your insert form’s save action. You will have to either hard code or search for an ID in the postback, but that is the only hard thing. See code example below.

var current = BindingSource.Current as Sage.Entity.Interfaces.IOpportunity;
var contactsText = Request.Form.Get("ctl00$MainContent$OpportunityContactGrid$OpportunityContactgrdOpportunityContacts_DataCarrier");
if (contactsText.Length > 0)
{
    var feed = new Sage.Integration.Entity.Feeds.OpportunityContactFeed();

    using (var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(contactsText)))
    {
        new Sage.Common.Syndication.JsonSerializer().LoadFromStream<Sage.Integration.Entity.Feeds.OpportunityContactEntry>(feed, stream);
    }

    if (feed == null) return;
    var transformer =
        Sage.Platform.Application.ApplicationContext.Current.Services.Get<Sage.Platform.SData.IAtomEntryToEntityTransformationService>();
    current.Contacts.Clear(); // always clear this on insert
    if (transformer != null && current != null)
    {
        foreach (Sage.Integration.Entity.Feeds.OpportunityContactEntry entry in feed.Entries)
        {
            var item = transformer.ConvertEntry(entry) as Sage.Entity.Interfaces.IOpportunityContact;
            if (item != null)
            {
                item.Opportunity = current;
                current.Contacts.Add(item);
            }
        }
    }
}    
current.Save();
Response.Redirect("Opportunity.aspx?modeid=detail&entityid=" + current.Id);


Leave a Reply