locked
Validation - Copy data from one entity to another RRS feed

  • Question

  • Right now, I have two entities I am dealing with:  Contact (default that comes with CRM) and another entity which I will call Entity B from here on out.  When a contact is selected (from a lookup field) on Entity B, it populates the contact's 'Job Title' from the Contact entity using CrmService.Retrieve method using Javascript.  It puts it in 'Entity B Job Title' which has code is its OnChange event :

    if (crmForm.all.my_lookup_field.DataValue != null)
    {
    	var contactid = crmForm.all.my_lookup_field.DataValue[0].id;
    
    	var authenticationHeader = GenerateAuthenticationHeader();
    
      //Prepare the SOAP message
    	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>contact</entityName>"+ 
    	"<id>" + contactid + "</id>"+ 
    	"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    	"<q1:Attributes>"+ 
    	"<q1:Attribute>jobtitle</q1:Attribute>"+ 
    	"</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);
    	}
    	else
    	{
    		crmForm.all.entity_b_job_title.DataValue = resultXml.selectSingleNode("//q1:jobtitle").nodeTypedValue;
    	}	
    }
    
    This works great BUT I need to validate that a person's Job Title (from Contact) is not null, otherwise I get an 'Object required' JavaScript error.  How can I check the Job Title field from Contact if I am working in another entity?  Thanks!
    http://radiateeverything.blogspot.com/
    Thursday, March 17, 2011 8:44 PM

Answers

  • if the Job Title in contact entity is null than it will be catched by catch statement. If it is not null than it will execute try statment. if you replace end portion of your code with the below you will see an alert whenever the Job Title field is empty on contact:-

     

     //Check for errors
    	var errorCount = resultXml.selectNodes('//error').length;
    	if (errorCount != 0)
    	{
    		var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    		alert(msg);
    	}
    	else
    	{
    try
    {
    crmForm.all.entity_b_job_title.DataValue = resultXml.selectSingleNode("//q1:jobtitle").nodeTypedValue;
    }
    catch(err)
    {
    alert("Job Title field is empty");
    crmForm.all.entity_b_job_title.DataValue = null;
    }
    }	
    }
    
    

     


    Regards Faisal
    • Marked as answer by wikky1 Friday, March 18, 2011 7:04 PM
    Friday, March 18, 2011 5:24 PM

All replies

  • Hello,

    Please read this 2 articles:

    - CRM SDK soap entity creationhttp://www.bizforward.cws-international.com/2011/01/26/creating-records-in-crm-2011-using-javascript/.

    - Full CRM SDK soap library for CRM 2011: http://www.bizforward.cws-international.com/2011/03/09/useful-javascript-functions-and-methods-for-crm-2011-cws-crm-utils-js-library/.

    Let's say you'll get a contact object. To validate if the Job Title is null, try the following code:

    var jobTitle = contact.GetValue("new_jobtitle");
    
    if(jobTitle == null)
    <span style="white-space:pre">	</span>alert("The contact job title is null")
    


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

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

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Thursday, March 17, 2011 10:58 PM
  • Put in try catch:-

    try
    
    {
    
    crmForm.all.entity_b_job_title.DataValue = resultXml.selectSingleNode("//q1:jobtitle").nodeTypedValue;
    
    }
    
    catch(err)
    
    {
    
    //alert("Job Title field is empty");
    
    }

    Regards Faisal
    Thursday, March 17, 2011 11:50 PM
  • Faisal--I need to check the Job Title from the Contact entity (this would only work for my entity B).  From the OnChange event in entity B, how can I check Job Title from the Contact entity to see if it is null?
    http://radiateeverything.blogspot.com/
    Friday, March 18, 2011 4:22 PM
  • if the Job Title in contact entity is null than it will be catched by catch statement. If it is not null than it will execute try statment. if you replace end portion of your code with the below you will see an alert whenever the Job Title field is empty on contact:-

     

     //Check for errors
    	var errorCount = resultXml.selectNodes('//error').length;
    	if (errorCount != 0)
    	{
    		var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    		alert(msg);
    	}
    	else
    	{
    try
    {
    crmForm.all.entity_b_job_title.DataValue = resultXml.selectSingleNode("//q1:jobtitle").nodeTypedValue;
    }
    catch(err)
    {
    alert("Job Title field is empty");
    crmForm.all.entity_b_job_title.DataValue = null;
    }
    }	
    }
    
    

     


    Regards Faisal
    • Marked as answer by wikky1 Friday, March 18, 2011 7:04 PM
    Friday, March 18, 2011 5:24 PM
  • If you haven't noticed by now, I'm a novice haha...Thanks a lot!


    http://radiateeverything.blogspot.com/
    Friday, March 18, 2011 7:04 PM