简体   繁体   中英

Crm 2011 javascript get radio button value & set username

On a CRM 2011 form I have a radio button (two options) field called new_yearchecked and a lookup field called new_checkedby from which I can lookup CRM Users.

I want to add a piece of Javascript to the form that if new_yearchecked changes to 'Yes' new_checkedby will automatically populate with the name of the User who changed new_yearchecked.

Can anyone help? This is where I am;

function YearChecked() {
if (Xrm.Page.getAttribute("new_yearchecked") = True) {
    var userLookup = Xrm.Page.context.getUserId();
    if (userLookup != null)  {
        Xrm.Page.getAttribute("new_yeacheckedby").setValue(userLookup);

    }

}

}

function getUserName() {
    if (Xrm.Page.ui.setFormNotification !== undefined) { return Xrm.Page.context.getUserName(); }

    var serverUrl;
    if (Xrm.Page.context.getClientUrl !== undefined) {
        serverUrl = Xrm.Page.context.getClientUrl();
    } else {
        serverUrl = Xrm.Page.context.getServerUrl();
    }
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    var userRequest = new XMLHttpRequest(); 
    userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false); 
    userRequest.setRequestHeader("Accept", "application/json"); 
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    userRequest.send();
    if (userRequest.status === 200) {
        var retrievedUser = JSON.parse(userRequest.responseText).d; 
        var userFullName = retrievedUser.FullName;
        return userFullName;
    }
    else { return "error"; }
}

}
function YearChecked() {
    var yearChecked = Xrm.Page.getAttribute("new_yearchecked").getValue();
    if (yearChecked == true) {
        var userLookup = new Array();
        userLookup[0] = new Object();
        userLookup[0].id = Xrm.Page.context.getUserId();
        userLookup[0].name = getUserName();
        userLookup[0].entityType = "systemuser";
        Xrm.Page.getAttribute("new_yeacheckedby").setValue(userLookup);
    }
}

User name is not required. You can use an arbitrary / temporary name instead. The user will see the actual user name after save.

eg

function YearChecked() {
    if (Xrm.Page.getAttribute("new_yearchecked") == true) {
        Xrm.Page.getAttribute("new_yeacheckedby").setValue([{
            id : Xrm.Page.context.getUserId(),
            name : "Current User",
            entityType : "systemuser"
        }]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM