Set Lookup Value using JavaScript in MSCRM

Syntax:

Name Type Description
lookUpSchemaName String The lookup attribute logical name
entitySchemaName String The logical name of the entity being set.
recordId String A string representation of the GUID value for the record being set.
The expected format is “{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}”.
recordName String The text to be displayed in the lookup.
function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
    var lookUpObj = [];
    lookUpObj[0] = {};
    lookUpObj[0].id = recordId;
    lookUpObj[0].entityType = entitySchemaName;
    lookUpObj[0].name = recordName;
    if (Xrm.Page.getAttribute(lookUpSchemaName) != null)
        Xrm.Page.getAttribute(lookUpSchemaName).setValue(lookUpObj);
}

Alternate Method

function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
    Xrm.Page.getAttribute(lookUpSchemaName).setValue([{
        entityType: entitySchemaName,
        id: recordId,
        name: recordName
    }]);
}

Please provide your valuable comments on this article.