Tuesday, March 26, 2013

CRM 2011: Send Email from javascript

I had to add a new button on email form that when clicked will set check a custom checkbox on email form and send the email. I have some extra checks based on email status.

The function that I use looks like this:

function SendEmail() {
 
    var formType = Xrm.Page.ui.getFormType();
    var emailStatus = Xrm.Page.getAttribute("statecode").getValue();
    var emailDirection = Xrm.Page.getAttribute("directioncode").getValue();

    //Update mode && Status= Open (Draft or failed)
    if (formType == 1 || (formType == 2 && emailStatus == "Open")) {

        //outgoing
        if (emailDirection == "1") {
            // Mark the "Is Completed" field in the email
            var completed = Xrm.Page.getAttribute("new_iscompleted");

            if (completed != null) {
                completed.setValue(true);
                if (typeof send == 'function') {
                    //send email
                    send();
                }
            }
        }
        else {
            alert('Action allowed only for "Outgoing" emails');
        }
    }
    else {
        alert("Action not supported for this email status");
    }
}

No comments:

Post a Comment