locked
Set Regarding field using a script in Phone Call activity RRS feed

  • Question

  • Hi

    I'm trying to produce a script for the onChange event of either the Sender or Recipient fields in a Phone Call activity that will set the Regarding field to a particular custom entity that the contact is associated with. I've got as far as using the Web Service to get the associated record but I can't get the regarding to set. I think it's becuase the Regarding field is looking for an object rather than say just the ID of the record. Is there any way I can do this? the script i have so far is...

    var representedOrg = "";
    
    if(crmForm.all.directioncode.DataValue==false&&crmForm.all.from.DataValue!=null)
    {
    		// Prepare variables for a contact to retrieve.
    		var entityId = crmForm.all.from.DataValue[0].id;
    		var entityName = crmForm.all.from.DataValue[0].typename;
    		var contactName = crmForm.all.from.DataValue[0].name;
    		var authenticationHeader = GenerateAuthenticationHeader();
    		var q1Attributes = "";
    		
    		if(entityName=="contact")
    		{
    			q1Attributes = "<q1:Attribute>fullname</q1:Attribute>"+"<q1:Attribute>new_memberid</q1:Attribute>"+"<q1:Attribute>parentcustomerid</q1:Attribute>";
    			RetrieveXML(entityId, entityName, q1Attributes, authenticationHeader);
    			crmForm.all.regardingobjectid.DataValue=representedOrg;
    		}
    
    }
    
    function RetrieveXML(entityId, entityName, q1Attributes, authenticationHeader)
    {
    	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'>"+ 
    		authenticationHeader+ 
    		"<soap:Body>"+ 
    		"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    		"<entityName>"+entityName+"</entityName>"+ 
    		"<id>"+entityId+"</id>"+ 
    		"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    		"<q1:Attributes>"+q1Attributes+"</q1:Attributes>"+ 
    		"</columnSet>"+ 
    		"</Retrieve>"+ 
    		"</soap:Body>"+ 
    		"</soap:Envelope>";
    		// Prepare the xmlHttpObject and send the request.
    		var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    		xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    		xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    		xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    		xHReq.setRequestHeader("Content-Length", xml.length);
    		xHReq.send(xml);
    		// Capture the result.
    		var resultXml = xHReq.responseXML;
    		
    		// Check for errors.
    		var errorCount = resultXml.selectNodes('//error').length;
    		if (errorCount != 0)
    		{
    			var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    			alert(msg);
    		}
    		//set orgid to the retrieved value
    		else
    		{
    			if(resultXml.selectSingleNode("//q1:new_memberid")!=null)
    			{
    				representedOrg=resultXml.selectSingleNode("//q1:new_memberid").nodeTypedValue;
    			}
    			else if(resultXml.selectSingleNode("//q1:new_memberid")==null&&resultXml.selectSingleNode("//q1:parentcustomerid")!=null)
    			{
    				representedOrg=resultXml.selectSingleNode("//q1:parentcustomerid").nodeTypedValue;
    			}
    			else
    			{
    				alert("The contact "+contactName+" is not associated with a Member or Other Organisation.");
    			}	
    		}
    }
    Monday, July 20, 2009 12:23 PM

Answers

All replies

  • Hi, you're right. The regarding field is a lookup and needs to be set up using an object rather than just the ID of the related record.


    Stefan
    • Marked as answer by Kelvin Aston Monday, July 20, 2009 2:57 PM
    Monday, July 20, 2009 12:37 PM
  • Hi Kelv,

    you would require to specify the type of regarding objectId.

    crmForm.all.regardingobjectid.type='custom_entityname';

    Replace 'custom_entityname' with you custom entity name.

    Here is the code..

    if(entityName=="contact")
    		{
    			q1Attributes = "<q1:Attribute>fullname</q1:Attribute>"+"<q1:Attribute>new_memberid</q1:Attribute>"+"<q1:Attribute>parentcustomerid</q1:Attribute>";
    
    RetrieveXML(entityId, entityName, q1Attributes, authenticationHeader);
    crmForm.all.regardingobjectid.type= 'custom_entityName';
    crmForm.all.regardingobjectid.DataValue=representedOrg;
    		}

    Jai Ho CRM http://mscrmkb.blogspot.com Skype - amol.gholap
    Monday, July 20, 2009 12:40 PM
  • Thanks Stefan - That was exactly what I needed.

    Kelv
    Monday, July 20, 2009 2:58 PM