locked
auto populate field by using java script RRS feed

  • Question

  • My requirement is as follows...(USING JAVA SCRIPT)

    *In CONTACT entity I have created a custom field "name" and one more field "contact type".. Here contact type is option set with values= "employee" and "student"

    * I also created another entity SCHOOL in which i took custom fields "contact name" as lookup field by taking target entity as "contact" and another custom field "contact type"

    => now , once i go to school entity, n select one contact from contact name lookup field, the the type which we have given in contact entity for that specific contact should automatically fill in the "contact type" field in school.. using java script

    Thursday, January 23, 2014 6:38 AM

Answers

All replies

  • Hi Teju4,

    Have a look at this blog, its pretty much what you need: https://community.dynamics.com/crm/f/117/t/107172.aspx


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by Amy.4 Thursday, January 23, 2014 9:37 AM
    Thursday, January 23, 2014 7:35 AM
  • HI

    Try this...

    function setContactTypeofSchool()

    {

    var sLookupValue = new Array();

    sLookupValue = Xrm.Page.getAttribute("contactname").getValue();

    if(sLookupValue [0] != null)

    {

        var schoolid= sLookupValue [0].id;    

     }

    var contType = getAttributeValueFromID("contactentityname", "contacttypeofcontactentity", "contactentitynameid", schoolid);

    Xrm.Page.data.entity.attributes.get("contacttypeofschoolentity").setValue(contType);

    }

    function getAttributeValueFromID(sEntityName, sAttributeName, sID, sGUID)

    {        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>" + sEntityName + "</q1:EntityName>" +

        "        <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

        "          <q1:Attributes>" +

        "            <q1:Attribute>" + sAttributeName + "</q1:Attribute>" +

        "          </q1:Attributes>" +

        "        </q1:ColumnSet>" +

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

        "        <q1:PageInfo>" +

        "          <q1:PageNumber>1</q1:PageNumber>" +

        "          <q1:Count>1</q1:Count>" +

        "        </q1:PageInfo>" +

        "        <q1:Criteria>" +

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

        "          <q1:Conditions>" +

        "            <q1:Condition>" +

        "              <q1:AttributeName>" + sID + "</q1:AttributeName>" +

        "              <q1:Operator>Equal</q1:Operator>" +

        "              <q1:Values>" +

        "                <q1:Value xsi:type=\"xsd:string\">" + sGUID + "</q1:Value>" +

        "              </q1:Values>" +

        "            </q1:Condition>" +

        "          </q1:Conditions>" +

        "        </q1:Criteria>" +

        "      </query>" +

        "    </RetrieveMultiple>" +

        "  </soap:Body>" +

        "</soap:Envelope>" +

        "";

            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", xml.length);

            xmlHttpRequest.send(xml);

            // retrieve response and find attribute value

            var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName);

            if (result == null)

                return "";

            else

                return result.text;

        }

    Hope this helps..


    • Proposed as answer by Suresh Sorde Thursday, January 23, 2014 8:57 AM
    • Edited by Suresh Sorde Thursday, January 23, 2014 9:12 AM
    Thursday, January 23, 2014 8:56 AM
  • u can use Odata query or fetchxml for that.

    Before that get the SDK.Rest.js class file from Microsoft and add it to your Javascript library.

    you can get it from here.

    http://msdn.microsoft.com/en-us/library/gg985387.aspx

    after that write the following code in your javascript file:

    function (){

    var LookupValue = Xrm.Page.getAttribute("contactname").getValue();

    var LookupId=LookupValue[0].id;

    var options= "select=contacttype&$filter=ContactId eq guid'" + LookupId+ "'";

    SDK.REST.retrieveMultipleRecords('Contact', options, CallbackResultsForContacttype, function (error) {
                    alert(error.message);
                },
                function () { },
                true);

    }

    CallbackResultsForContacttype=function(Obj){

    if(Obj.length>0){

    var contacttypevalue=Obj[0].contacttype.Value;

    Xrm.Page.getAttribute("contacttype").setValue(contacttypevalue);

    }

    }

    Thursday, January 23, 2014 9:23 AM