Tuesday, January 21, 2014

How to Update salesstage with javascript based on "processbar" in MS CRM 2013

Hi all, it's been a loooong time since i last updated this blog.. But here's a new nice tip :)

Microsoft have not given us the easiest way of updating fields on FORMS based on the new processbars feature on FORMS, but here's one way to do it With Javscript.. Supported!

Should work on opportunities.. AFTER YOU INSERT YOUR GUID's FOR
1. Field "processid"
2. Field "stageid"
3. Change name of "salesstage"

(Needed in setSalesStage function)

/* Script Entry Point */
function OnCrmPageLoad() {
    var FormTypes =
    {
        Undefined: 0,
        Create: 1,
        Update: 2,
        ReadOnly: 3,
        Disabled: 4,
        QuickCreate: 5,
        BulkEdit: 6
    }
    runAlways();
    switch (Xrm.Page.ui.getFormType()) {
        case FormTypes.Create: OnNewFormLoad(); break;
        case FormTypes.Update: OnUpdateFormLoad(); break;
        case FormTypes.ReadOnly: OnReadOnlyFormLoad(); break;
        case FormTypes.Disabled: OnDisabledFormLoad(); break;
        case FormTypes.QuickCreate: OnQuickCreateFormLoad(); break;
        case FormTypes.BulkEdit: OnBulkEditFormLoad(); break;
        case FormTypes.Undefined: alert("Error"); break;
    }
}
/* Implement each Form Type you wish to address */
function runAlways() {
    onchange();
    Xrm.Page.data.entity.addOnSave(addToOnSave);
}
function OnNewFormLoad() {
    setSalesStage('new');
}
function OnUpdateFormLoad() {}
function OnReadOnlyFormLoad() { }
function OnDisabledFormLoad() { }
function OnQuickCreateFormLoad() { }
function OnBulkEditFormLoad() { }
/******************** OnChange and OnSave functions ***************************/
function onchange() {
    //Add all fields who should have OnChange triggers
    Xrm.Page.data.entity.attributes.get("prefix_fieldname").addOnChange(examplefunction);
}
function addToOnSave() {
 //Check to see if stageid has changed
    var attributes = Xrm.Page.data.entity.attributes.get();
    for (var i in attributes) {
        var attribute = attributes[i];
        if (attribute.getIsDirty()) {
            if (attribute.getName() == 'stageid') {
                //Update Probability field - send a "dummy parameter"
                setSalesStage('dummy');
            }
        }
    }
}
/*************** End OnChange and OnSave functions ***************************/
/* --------------- Private functions ---------------------*/
function examplefunction() {
//No Operation
}
function setSalesStage(check) {
 /*
 UNIKE FOR EACH PROSESS AND CRM ORGANIZATION
 Notes of GUID's steps in Prosess ID = 'bfc01b1c-9741-43b2-a016-3a41d91c234a' Display: "Solution Selling" on entity: opportunity
 10% - 85bdfa9d-f067-42d4-82c3-0e1fd09f2b19
 20% - 6a2020c4-7d10-28e3-1898-138b6c075259
 40% - ae0c9ae4-55eb-cd1c-44ab-872d4aca4338
 60% - 16b99584-143d-04ad-61c9-2b6a809e5608
 80% - 93e08965-2e2a-4674-0e57-ec88633a0940
 90% - 3a516491-d7cd-6cb1-a3e0-61be68c1857c
 */
    var prosessId = Xrm.Page.getAttribute("processid");
    var stageid = Xrm.Page.getAttribute("stageid");
    var prob = Xrm.Page.getAttribute("salesstage");

 //Check if new record
    if (check == 'new') {
  //sets first process name
        prob.setValue('10% - Qualification');
    }
 //Not a new record, then it's an existing record
    else {
        if (prosessId.getValue() == 'bfc01b1c-9741-43b2-a016-3a41d91c234a') {
            switch (stageid.getValue()) {
                case '85bdfa9d-f067-42d4-82c3-0e1fd09f2b19':
                    //setter sannsynlighet
                    prob.setValue('10% - Qualification');
                    break;
                case '6a2020c4-7d10-28e3-1898-138b6c075259':
                    prob.setValue('20% - Pain and Need');
                    break;
                case 'ae0c9ae4-55eb-cd1c-44ab-872d4aca4338':
                    prob.setValue('40% - Proof of Concept');
                    break;
                case '16b99584-143d-04ad-61c9-2b6a809e5608':
                    prob.setValue('60% - Closing');
                    break;
                case '93e08965-2e2a-4674-0e57-ec88633a0940':
                    prob.setValue('80% - Offering accepted');
                    break;
                case '3a516491-d7cd-6cb1-a3e0-61be68c1857c':
                    prob.setValue('90% - Contract signing');
                    break;
                default:
                    alert("An error has occured!");
            }
        }
    }
}
/* ------------ End Private functions------------------*/