仅在FormType为Create时将所有者设置为当前用户

时间:2014-05-17 00:46:39

标签: javascript xml dynamics-crm user-defined-functions dynamics-crm-4

因此我被要求创建一种方法来自动将订单的所有者设置为当前用户,而不是映射的帐户的默认所有者。

此外,我必须使其仅在用户正在创建订单的实例中运行。

所以我从这开始:

try{
var xml = "" + 
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" + 
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
" <q1:EntityName>systemuser</q1:EntityName>" + 
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
" <q1:Attributes>" + 
" <q1:Attribute>businessunitid</q1:Attribute>" + 
" <q1:Attribute>firstname</q1:Attribute>" + 
" <q1:Attribute>fullname</q1:Attribute>" + 
" <q1:Attribute>lastname</q1:Attribute>" + 
" <q1:Attribute>organizationid</q1:Attribute>" + 
" <q1:Attribute>systemuserid</q1:Attribute>" + 
" </q1:Attributes>" + 
" </q1:ColumnSet>" + 
" <q1:Distinct>false</q1:Distinct>" + 
" <q1:Criteria>" + 
" <q1:FilterOperator>And</q1:FilterOperator>" + 
" <q1:Conditions>" + 
" <q1:Condition>" + 
" <q1:AttributeName>systemuserid</q1:AttributeName>" + 
" <q1:Operator>EqualUserId</q1:Operator>" + 
" </q1:Condition>" + 
" </q1:Conditions>" + 
" </q1:Criteria>" + 
" </query>" + 
" </RetrieveMultiple>" + 
" </soap:Body>" + 
"</soap:Envelope>" + 
"";
var xmlHttpRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest2.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest2 .setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest2.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest2.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest2.send(xml);
var resultXml = xmlHttpRequest2.responseXML;
var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
var firstNameNode = entityNode.selectSingleNode("q1:firstname");
var lastNameNode = entityNode.selectSingleNode("q1:lastname");
var fullNameNode = entityNode.selectSingleNode("q1:fullname");
var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");
var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid");
var organizationIdNode = entityNode.selectSingleNode("q1:organizationid");

//Create an array to set as the DataValue for the the lookup control. 
var lookupData = new Array(); 
//Create an Object add to the array. 
var lookupItem= new Object(); 
//Set the id, typename, and name properties to the object. 
lookupItem.id = systemUserIdNode.text; 
lookupItem.typename = 'systemuser'; 
lookupItem.name = fullNameNode.text; 
// Add the object to the array. 
lookupData[0] = lookupItem; 
// Set the value of the lookup field to the value of the array. 
crmForm.all.ownerid.DataValue = lookupData;
crmForm.all.ownerid.ForceSubmit = true;

}
catch(err){alert("Error on user set.")}

但这会在每个FormType中运行......所以我有点不知所措。

1 个答案:

答案 0 :(得分:0)

然后,在我的努力中,想出了许多实现这一目标的方法,我终于找到了一个有效且想要分享的方法。

这是我发现的工作:

var xml = "" +
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
        GenerateAuthenticationHeader() +
        " <soap:Body>" +
        " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
        " <q1:EntityName>systemuser</q1:EntityName>" +
        " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
        " <q1:Attributes>" +
        " <q1:Attribute>businessunitid</q1:Attribute>" +
        " <q1:Attribute>firstname</q1:Attribute>" +
        " <q1:Attribute>fullname</q1:Attribute>" +
        " <q1:Attribute>lastname</q1:Attribute>" +
        " <q1:Attribute>organizationid</q1:Attribute>" +
        " <q1:Attribute>systemuserid</q1:Attribute>" +
        " </q1:Attributes>" +
        " </q1:ColumnSet>" +
        " <q1:Distinct>false</q1:Distinct>" +
        " <q1:Criteria>" +
        " <q1:FilterOperator>And</q1:FilterOperator>" +
        " <q1:Conditions>" +
        " <q1:Condition>" +
        " <q1:AttributeName>systemuserid</q1:AttributeName>" +
        " <q1:Operator>EqualUserId</q1:Operator>" +
        " </q1:Condition>" +
        " </q1:Conditions>" +
        " </q1:Criteria>" +
        " </query>" +
        " </RetrieveMultiple>" +
        " </soap:Body>" +
        "</soap:Envelope>" +
        "";
var xmlHttpRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest2.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest2.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest2.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest2.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest2.send(xml);
var resultXml = xmlHttpRequest2.responseXML;
var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
var firstNameNode = entityNode.selectSingleNode("q1:firstname");
var lastNameNode = entityNode.selectSingleNode("q1:lastname");
var fullNameNode = entityNode.selectSingleNode("q1:fullname");
var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");
var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid");
var organizationIdNode = entityNode.selectSingleNode("q1:organizationid");

        //Create an array to set as the DataValue for the the lookup control. 
var lookupData = new Array();
        //Create an Object add to the array. 
var lookupItem = new Object();
        //Set the id, typename, and name properties to the object. 
lookupItem.id = systemUserIdNode.text;
lookupItem.typename = 'systemuser';
lookupItem.name = fullNameNode.text;
        // Add the object to the array.
lookupData[0] = lookupItem;
        // Set the value of the lookup field to the value of the array.
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
        // To carry over firstname and fullname callouts into the switch statement
var fullname = fullNameNode.text;
var firstName = firstNameNode.text;
switch (crmForm.FormType) {
case CRM_FORM_TYPE_CREATE:
    if (fullname == "Jeromie Kirchoff") {
        // alert("Hi " + firstName + '\n' + '\n' +"Let me make this easy for you!");

        crmForm.all.ownerid.DataValue = lookupData;
        crmForm.all.ownerid.ForceSubmit = true;

        // run the default order setting I had previously added in my Onload script.
        crmForm.all.new_orderstatus.DataValue = 6;
        try {crmForm.orderrstat(); } catch (err) {alert("Function Call Out - ERROR - 0001-1"); }
    }
    break;
case CRM_FORM_TYPE_UPDATE:
    break;
}

我希望这会有所帮助。 =)