Search This Blog

Friday, February 24, 2012

Contact Info / Label (CRM 2011)

Although the built-in Word Mail Merge can create nice personalized mailing labels, sometimes users don't want to go through all the steps Word requires to generate just one label. That is why we add a "Contact Info Summary" concatenation on most of our clients' Account and Contact forms, for a quick copy-paste option.

The code below applies to Contacts, but it can be easily adapted for the Account, Lead and Address forms.

1.    Start by creating a new memo attribute (“Multiple lines of text”) that will hold your concatenated label. In our example, we named it “new_contactinfosummary”. We recommend making it non-searchable to keep the fields list cleaner.

2.    Add the field to the form. Layout is up to you, obviously, but we recommend making it read-only, to ensure that users don’t start typing in there, thinking individual fields will update from there (true story…).


3.    Add the following 2 functions in a web resource.

function BuildContactInfoSummary() {
    //------- Load the function to build the Contact Info Summary
    var Label = "";

    if (Xrm.Page.getAttribute("salutation").getValue() != null)
    { Label = Xrm.Page.getAttribute("salutation").getValue() + " "; }

    if (Xrm.Page.getAttribute("firstname").getValue() != null)
    { Label = Label + Xrm.Page.getAttribute("firstname").getValue() + " "; }

    if (Xrm.Page.getAttribute("lastname").getValue() != null)
    { Label = Label + Xrm.Page.getAttribute("lastname").getValue(); }

    if (Xrm.Page.getAttribute("jobtitle").getValue() != null)
    { Label = Label + ", " + Xrm.Page.getAttribute("jobtitle").getValue(); }

    var OrgName = Xrm.Page.getAttribute("parentcustomerid").getValue();
    if (OrgName != null)
    { Label = Label + "\r\n" + OrgName[0].name; }

    if (Xrm.Page.getAttribute("address1_line1").getValue() != null)
    { Label = Label + "\r\n" + Xrm.Page.getAttribute("address1_line1").getValue(); }

    if (Xrm.Page.getAttribute("address1_line2").getValue() != null)
    { Label = Label + "\r\n" + Xrm.Page.getAttribute("address1_line2").getValue(); }

    //ENABLE THE FOLLOWING IF YOUR FORM INCLUDES STREET 3:
    //        if (Xrm.Page.getAttribute("address1_line3").getValue() != null)
    //        { Label = Label + "\r\n" + Xrm.Page.getAttribute("address1_line3").getValue(); }

    if (Xrm.Page.getAttribute("address1_city").getValue() != null)
    { Label = Label + "\r\n" + Xrm.Page.getAttribute("address1_city").getValue(); }

    if (Xrm.Page.getAttribute("address1_stateorprovince").getValue() != null)
    { Label = Label + ", " + Xrm.Page.getAttribute("address1_stateorprovince").getValue(); }

    if (Xrm.Page.getAttribute("address1_postalcode").getValue() != null)
    { Label = Label + "   " + Xrm.Page.getAttribute("address1_postalcode").getValue(); }

    if (Xrm.Page.getAttribute("address1_country").getValue() != null)
    { Label = Label + "    " + Xrm.Page.getAttribute("address1_country").getValue(); }

    if (Xrm.Page.getAttribute("telephone1").getValue() != null)
    { Label = Label + "\r\nBusiness Phone: " + Xrm.Page.getAttribute("telephone1").getValue(); }

    if (Xrm.Page.getAttribute("emailaddress1").getValue() != null)
    { Label = Label + "\r\n" + Xrm.Page.getAttribute("emailaddress1").getValue(); }

    Xrm.Page.getAttribute("new_contactinfosummary").setValue(Label);
    Xrm.Page.getAttribute("new_contactinfosummary").setSubmitMode("always");
}

function UpdateContactInfoSummary() {
    //------Update the Contact Info Summary on updatable records
    if (Xrm.Page.ui.getFormType() == 2) {

        //------Force a save if the Contact Info Summary was empty ** NOTE: remove this following statement if you DO NOT want to force a save -- i.e., if you have onSave code that you do not want triggered right away
        if (Xrm.Page.getAttribute("new_contactinfosummary").getValue() == null) {
            BuildContactInfoSummary();
            Xrm.Page.data.entity.save();
        }
        else {
            BuildContactInfoSummary();

            //------Disable the "do you want to save?" alert if the Contact Info Summary hasn't changed
            if (crmForm.all.new_contactinfosummary.IsDirty) crmForm.detachCloseAlert();
        }
    }
}

4.    Add UpdateContactInfoSummary as an onLoad event and BuildContactInfoSummary as an onSave event.

5.    Save and publish customizations.

No comments:

Post a Comment