Pradyush Das
Pradyush Das

Reputation: 11

MSCRM 4.0 : Getting Error when saving and clicking on the Item in the field value after auto populating look up though Javascript

I am getting an error after auto populating a Look-UP though javascript. My code is below. It is working and I am able to autopopulate. But when I click on the autopopulated filed I am getting an error. Also an error while saving the record.Errors are :

1) 'ryan_leadengagementprincipalassignment with id = ******* does not exist' (When I click on the populated Lookup value)

2) The requested record is not found or you do not have sufficient privilege to view it. (While saving the record)

Code:

 var LEPAccountLookup= crmForm.all.customerid.DataValue;

    if (LEPAccountLookup!= null && LEPAccountLookup!= 'undefined')
    {
    var LEPAccountID= LEPAccountLookup[0].id;

    var LEPAccountxml = '' +

    '<?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>ryan_leadengagementprincipalassignment</q1:EntityName>' +

    ' <q1:ColumnSet xsi:type=\'q1:AllColumns\' />' +

    ' <q1:Distinct>false</q1:Distinct>' +

    ' <q1:Criteria>' +

    ' <q1:FilterOperator>And</q1:FilterOperator>' +

    ' <q1:Conditions>' +

    ' <q1:Condition>' +

    ' <q1:AttributeName>ryan_accountnameid</q1:AttributeName>' +

    ' <q1:Operator>Like</q1:Operator>' +

    ' <q1:Values>' +

    ' <q1:Value xsi:type=\'xsd:string\'>' + LEPAccountID + '</q1:Value>' +

    ' </q1:Values>' +

    ' </q1:Condition>' +

    ' </q1:Conditions>' +

    ' </q1:Criteria>' +

    ' </query>' +

    ' </RetrieveMultiple>' +

    ' </soap:Body>' +

    '</soap:Envelope>' +

    '';

    //alert(LEPAccountxml);

    var xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');

    xmlHttpRequest.Open('POST', '/mscrmservices/2007/CrmService.asmx', false);

    xmlHttpRequest.setRequestHeader('SOAPAction','http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple');

    xmlHttpRequest.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');

    xmlHttpRequest.setRequestHeader('Content-Length', LEPAccountxml.length);

    xmlHttpRequest.send(LEPAccountxml);

    var resultLEPAccountxml = xmlHttpRequest.responseXML;

    //alert(resultLEPAccountxml);

    //----------------------------------------------------------------------------------------------------------------------

    var entityNodes = resultLEPAccountxml.selectNodes('//RetrieveMultipleResult/BusinessEntities/BusinessEntity');

    for (var i = 0; i < entityNodes.length; i++) {

    var entityNode = entityNodes[i];

    var LEPAccountNode = entityNode.selectSingleNode("q1:ryan_accountnameid");
    var LEPAccountNodename = entityNode.selectSingleNode('./q1:ryan_accountnameid/@name');
    var LEPParentPracAreaNode = entityNode.selectSingleNode("q1:ryan_parentpracticeareaid");
    var LEPParentPracAreaNodename = entityNode.selectSingleNode('./q1:ryan_parentpracticeareaid/@name');
    var LEPPracAreaNode = entityNode.selectSingleNode("q1:ryan_practiceareaid");
    var LEPPracAreaNodename = entityNode.selectSingleNode('./q1:ryan_practiceareaid/@name');
    var LEPLegalEntityNode= entityNode.selectSingleNode("q1:ryan_legalid");
    var LEPLegalEntityNodename = entityNode.selectSingleNode('./q1:ryan_legalid/@name');
    var LEPNode= entityNode.selectSingleNode("q1:ryan_leadengagementid");
    var LEPNodename = entityNode.selectSingleNode('./q1:ryan_leadengagementid/@name')


    var LEPAccountid= (LEPAccountNode == null) ? null : LEPAccountNode.text;
    var LEPAccountname= (LEPAccountNodename == null) ? null : LEPAccountNodename.text;

    var LEPPareantPracAreaid= (LEPParentPracAreaNode == null) ? null : LEPParentPracAreaNode.text;
    var LEPPareantPracAreaname= (LEPParentPracAreaNodename == null) ? null : LEPParentPracAreaNodename.text;

    var LEPPracAreaid= (LEPPracAreaNode == null) ? null : LEPPracAreaNode.text;
    var LEPPracAreaname= (LEPPracAreaNodename == null) ? null : LEPPracAreaNodename.text;

    var LEPLegalEntityid= (LEPLegalEntityNode == null) ? null : LEPLegalEntityNode.text;
    var LEPLegalEntityname= (LEPLegalEntityNodename == null) ? null : LEPLegalEntityNodename.text;

    var LEPEntityid= (LEPNode == null) ? null : LEPNode.text;
    var LEPEntityname= (LEPNodename == null) ? null : LEPNodename.text;



    //alert(LEPAccountid);
    //(LEPAccountname);
    var LEPAccount = crmForm.all.customerid.DataValue;
    var LEPLE = crmForm.all.ryan_ryanlegalentityid.DataValue;
    var LEPPPA = crmForm.all.ryan_parentpracticearea2id.DataValue;
    var LEPPA = crmForm.all.ryan_practiceareaid.DataValue;

    if((LEPAccount != null) &&(LEPLE != null) && (LEPPPA != null) && (LEPPA != null))
    {

    if ((LEPAccountid== LEPAccount[0].id) && (LEPLegalEntityid== LEPLE[0].id) && (LEPPareantPracAreaid== LEPPPA[0].id) && (LEPPracAreaid== LEPPA[0].id))

    {


     var lookup = [];
     var lookupValue = new Object();

       lookupValue.id = LEPEntityid;
       lookupValue.typename = 'ryan_leadengagementprincipalassignment';
       lookupValue.name = LEPEntityname;

       lookup[0] = lookupValue;

       crmForm.all.ryan_leadengagementprincipalid.DataValue = lookup;
       crmForm.all.ryan_leadengagementprincipalid.ForceSubmit = true;


    }
    }

    }
    }

Upvotes: 1

Views: 134

Answers (1)

Joseph Duty
Joseph Duty

Reputation: 795

There is likely something wrong with your SOAP call. When you debug it, does resultLEPAccountXml have an xml response string? or is it just returning an error/empty set? (it appears you were alerting that and have commented it out)

Unfortunately I'm not familiar enough with your particular data model to verify that you are querying for the same table that you are attempting to populate. Is the ryan_leadengagementid field on the ryan_leadengagementprincipalassignment table indeed the correct field to use to set the lookup on whatever form you are executing this script (ie is ryan_leadengagementprincipalid a lookup to the ryan_leadengagementprincipalassignment table)? If you are setting the lookup for the wrong table, then that will cause an issue that looks exactly like this.

Also (as a side note), it appears you are doing a RetrieveMultiple, and looping through and setting the target field multiple times potentially - is this the intended functionality (setting that lookup based on all the leadengagementprincipalassignment results)? The setting of the lookup field looks fine, so I suspect it is the code to retrieve the values which should be used to set the lookup that is the culprit here.

Upvotes: 0

Related Questions