locked
Invoking actions from javascript RRS feed

  • Question

  • Hi everyone,

                     I am trying to call an action through the below script:

     function CallActionFromJavaScript() {

       var projectName = "Project Created through Action from Javascript";
        var entityId = Xrm.Page.data.entity.getId();
        var entityName = "lead";
        var requestName = "new_LeadCreateProject";
        ExecuteActionCreateProject(projectName, entityId, entityName, requestName);}
    function ExecuteActionCreateProject(projectName, entityId, entityName, requestName) {
        // Creating the request XML for calling the Action
        var requestXML = ""
        requestXML += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestXML += "  <s:Body>";
        requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        requestXML += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Target</b:key>";
        requestXML += "            <b:value i:type=\"a:EntityReference\">";
        requestXML += "              <a:Id>" + entityId + "</a:Id>";
        requestXML += "              <a:LogicalName>" + entityName + "</a:LogicalName>";
        requestXML += "              <a:Name i:nil=\"true\" />";
        requestXML += "            </b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>ProjectName</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + project
    Name + "</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "        </a:Parameters>";
        requestXML += "        <a:RequestId i:nil=\"true\" />";
        requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";
        requestXML += "      </request>";
        requestXML += "    </Execute>";
        requestXML += "  </s:Body>";
        requestXML += "</s:Envelope>";
        var req = new XMLHttpRequest();
        req.open("POST", GetClientUrl(), false)
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationServic
    /Execute");
        req.send(requestXML); 
        //Get the Resonse from the CRM Execute method
        //var response = req.responseXML.xml;
    }
    function GetClientUrl() {
        if (typeof Xrm.Page.context == "object") {
            clientUrl = Xrm.Page.context.getClientUrl();
        }
        var ServicePath = "/XRMServices/2011/Organization.svc/web";
        return clientUrl + ServicePath;

     }

    I am calling this function on the onchange of industry field in lead.I need to create a project record through actions  where i am passing projectname as input parameter(string).But when changing industry it shows an error:

    Field:industrycode

    Event:onchange

    Error:" CallActionFromJavaScript" is undefined

    Please help.           

    Thursday, February 13, 2014 9:38 AM

Answers

  • Hi,

    The times I have seen these kind of errors there have been a slight syntax error somewhere. Have you tried to have just a function that does an alert and tried to call that one on the onChange of the field?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, February 13, 2014 9:49 AM
  • I think Rickard is correct about the syntax error - based on what you posted there was an errant line break in a variable name and in the where you are setting one of the request headers you are missing and "e" at the end of IOrganizationService.

    Here is the version I got working:

    function CallActionFromJavaScript() {
    
        var projectName = "Project Created through Action from Javascript";
        var entityId = Xrm.Page.data.entity.getId();
        var entityName = "lead";
        var requestName = "new_LeadCreateProject";
        ExecuteActionCreateProject(projectName, entityId, entityName, requestName);
    }
    
    function ExecuteActionCreateProject(projectName, entityId, entityName, requestName) {
        // Creating the request XML for calling the Action
        var requestXML = ""
        requestXML += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestXML += "  <s:Body>";
        requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        requestXML += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Target</b:key>";
        requestXML += "            <b:value i:type=\"a:EntityReference\">";
        requestXML += "              <a:Id>" + entityId + "</a:Id>";
        requestXML += "              <a:LogicalName>" + entityName + "</a:LogicalName>";
        requestXML += "              <a:Name i:nil=\"true\" />";
        requestXML += "            </b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>ProjectName</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + projectName + "</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "        </a:Parameters>";
        requestXML += "        <a:RequestId i:nil=\"true\" />";
        requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";
        requestXML += "      </request>";
        requestXML += "    </Execute>";
        requestXML += "  </s:Body>";
        requestXML += "</s:Envelope>";
        var req = new XMLHttpRequest();
        req.open("POST", GetClientUrl(), false)
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.send(requestXML);
        //Get the Resonse from the CRM Execute method
        //var response = req.responseXML.xml;
    }
    
    function GetClientUrl() {
        if (typeof Xrm.Page.context == "object") {
            clientUrl = Xrm.Page.context.getClientUrl();
        }
        var ServicePath = "/XRMServices/2011/Organization.svc/web";
        return clientUrl + ServicePath;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, February 13, 2014 2:11 PM
    Moderator

All replies

  • Hi,

    The times I have seen these kind of errors there have been a slight syntax error somewhere. Have you tried to have just a function that does an alert and tried to call that one on the onChange of the field?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, February 13, 2014 9:49 AM
  • I think Rickard is correct about the syntax error - based on what you posted there was an errant line break in a variable name and in the where you are setting one of the request headers you are missing and "e" at the end of IOrganizationService.

    Here is the version I got working:

    function CallActionFromJavaScript() {
    
        var projectName = "Project Created through Action from Javascript";
        var entityId = Xrm.Page.data.entity.getId();
        var entityName = "lead";
        var requestName = "new_LeadCreateProject";
        ExecuteActionCreateProject(projectName, entityId, entityName, requestName);
    }
    
    function ExecuteActionCreateProject(projectName, entityId, entityName, requestName) {
        // Creating the request XML for calling the Action
        var requestXML = ""
        requestXML += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestXML += "  <s:Body>";
        requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        requestXML += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Target</b:key>";
        requestXML += "            <b:value i:type=\"a:EntityReference\">";
        requestXML += "              <a:Id>" + entityId + "</a:Id>";
        requestXML += "              <a:LogicalName>" + entityName + "</a:LogicalName>";
        requestXML += "              <a:Name i:nil=\"true\" />";
        requestXML += "            </b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>ProjectName</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + projectName + "</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "        </a:Parameters>";
        requestXML += "        <a:RequestId i:nil=\"true\" />";
        requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";
        requestXML += "      </request>";
        requestXML += "    </Execute>";
        requestXML += "  </s:Body>";
        requestXML += "</s:Envelope>";
        var req = new XMLHttpRequest();
        req.open("POST", GetClientUrl(), false)
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.send(requestXML);
        //Get the Resonse from the CRM Execute method
        //var response = req.responseXML.xml;
    }
    
    function GetClientUrl() {
        if (typeof Xrm.Page.context == "object") {
            clientUrl = Xrm.Page.context.getClientUrl();
        }
        var ServicePath = "/XRMServices/2011/Organization.svc/web";
        return clientUrl + ServicePath;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, February 13, 2014 2:11 PM
    Moderator
  •  I tried to call the function by removing the request XML part then its working fine.I also found the IOrganizationSerivice  'e' is missing.But after adding that also its throwing the same error.Is there anything else to correct?
    Friday, February 14, 2014 4:36 AM
  • I hope you have added the .js file onload of the form. Otherwise it should not throw the error. Please check from developer tool if the .js file is getting loaded or not.

    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT

    Friday, February 14, 2014 8:34 AM
  • Hi,

    Have you tried just to copy Jason's code and see if it works? If you don't want to do that, copy that code and your code into two different files and run a compare-tool on the two (I'm used to unix and would use diff from a cygwin prompt but there are good windows tools that does the same thing and have a gui :))

    That would give you a hint of what's wrong. Personally I'm no fan of hacking soapxml so I'd use the REST-inferface.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Friday, February 14, 2014 8:50 AM