Suppose we have two forms
1. Contact
2. Transaction
In contact form we have one lookup field Broker name which will mapped to contact database (for those contact which have contact type = Broker). So I have made a custom view to do this. Please find below steps:
1. Contact
2. Transaction
In contact form we have one lookup field Broker name which will mapped to contact database (for those contact which have contact type = Broker). So I have made a custom view to do this. Please find below steps:
Step 1:
First, I have design the contact form. In contact form there is a Contact Type option set which have the following value:
There is also a broker field (which is a lookup pointed to contact database).
Step 2:
Then, to filter the lookup data depend on the contact name who are marked as broker create a new view.
Click on New Button.
Enter the require details of the view (that means view name and description). And then click on Edit Filter Criteria for fetch the contact names that are marked as Broker.
Filtered the view like (select the contact name whose Contact Type = Broker).
Click OK and then Save & Close.
Step 3:
Now, coming to the design of contact main form, double click on the Broker field to open the field properties window.
Select the newly created view (vw_Broker_Selection) as Default View.
Click on OK.
After this the broker lookup will only show the contacts name who are marked as broker.
Step 4:
Next, we have to design the transaction form (change Contract form -> Transaction)
In the transaction form I have created two fields:
1. Contact Name (Read-Only lookup, pointed to contact database)
2. Broker Name (Read-Only Textbox, here we will show the broker name automatically).
1. Contact Name (Read-Only lookup, pointed to contact database)
2. Broker Name (Read-Only Textbox, here we will show the broker name automatically).
By double click the field, from field behavior make them Read-Only.
Step 5:
Now, we have to create a new JavaScript for auto populates the broker name. For this reason we have to go to Web Resource.
Create a new Web Resource.
Give the name, Display Name and Description of the web resource. Select Script (Jscript) as the type of web resource. Then Click on the Text Editor Button.
Below is the Text Editor window for new web resource java script.
Write the below code in that text editor.//change it accordingly as yours.
function AlertText() {
var primaryContactName = Xrm.Page.data.entity.attributes.get("new_contactname").getValue()[0].name;
var primaryContactID = Xrm.Page.data.entity.attributes.get("new_contactname").getValue()[0].id;
var brokernameTEMP = RetrieveEntityById("contact", primaryContactID, "new_broker");
var brokername = RetrieveEntityById("contact", brokernameTEMP, "fullname");
Xrm.Page.data.entity.attributes.get("new_brokername").setValue(brokername);
}
function RetrieveEntityById(EmployeeEntityName, EmployeeId, NameFieldAttributeName) {
var resultXml;
var AttributeVal = null;
var errorCount;
var msg;
var xmlHttpRequest;
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//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>" + EmployeeEntityName + "</entityName>" +
"<id>" + EmployeeId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
"<q1:Attribute>" + NameFieldAttributeName + "</q1:Attribute>" +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue; //Process and display the results.
alert("Error occured: " + msg);
}
else {
if (resultXml.selectSingleNode('//q1:' + NameFieldAttributeName) != null) {
AttributeVal = resultXml.selectSingleNode('//q1:' + NameFieldAttributeName).nodeTypedValue;
}
}
return AttributeVal;
}
Click on OK
Then Save > Publish > Save & Close
Step 6:
Now, I have to call the JavaScript from the form load event of Transaction form.
Select the Form Properties from the ribbon.
In the form properties first we have to add a new Form Libraries. To do this click on Add and then select the web resource we have just created.
Then in the Event Handler section write the function name of the JavaScript and click on OK.
Then click on, Save > Publish > Save & Close.
Step 7:
Now, we click on any contact name (previously saved).
Found contact name Ayan Choudhury have the broker name as Test Contact5.
Then, from the side navigation click on Transaction.
Click on Add New Transaction
And in the New Transaction create window we can see that the Broker Name field is auto populated with broker name Test Contact5.
Thank You
No comments:
Post a Comment