Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, May 4, 2016

CRM 2016: JavaScript – Retrieve records using SOAP Endpoint and XRMServiceToolkit

In my previous blogpost I described how to use XRM Service Toolkit in Dynamics CRM (2011 / 2013 / 2015 and 2016).

Today I am going to show how to retrieve parent account information onChange event of account lookup on Contact form. 

Remember! Don’t forget to add XRMServiceToolkit library in form properties. 

Now, from following code snippet you need to add getParentAccountInfo at onChange event of Parent Account field on contact field 


function getParentAccountInfo(accountLookup)
{
    try{
        var accountID = Xrm.Page.getAttribute(accountLookup).getValue()[0].id;
       
        var cols = ["emailaddress1","telephone1"];

        var retrievedAccount = XrmServiceToolkit.Soap.Retrieve("contact", contactID, cols);
       
        var emailAddress = Xrm.Page.getAttribute("emailaddress1");
        var telePhone = Xrm.Page.getAttribute("telephone1");
       
        var v_emailAddress = typeof retrievedAccount.attributes["emailaddress1"] === 'undefined'?'':
                                    retrievedAccount.attributes["emailaddress1"].value;
                                   
        var v_telephone = typeof retrievedAccount.attributes["telephone1"] === 'undefined'?'':
                                    retrievedAccount.attributes["telephone1"].value;
       
        emailAddress.setValue(v_emailAddress);
        telePhone.setValue(v_telephone);
    }
    catch(e){
        alert("Error Occured: "+e.Message);
    }
}

Enjoy!

Wednesday, July 29, 2015

How to set header field value in Dynamics CRM form?



Setting the header field values is not supported in xrm. So, we must move to unsupported ways to set the value of the fields added in header.
Following code snippet will help us to achieve the goal.

document.getElementById("header_xxx_d").childNodes[0].innerText = value;

In the above code snippet, replace xxx with your field name that exists in the header and value with your value you need to set.

But it'll not save in the database. To achieve this you should add a hidden field on  the form and set its value when you are setting value for the header field and then at  on-load event again get the value and assign to the above code snippet.

Cheers!