Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Auto field mapping between entities

Posted on by 815

Hello,

There is 2 entities "User Entity" and "Lead Entity" and  both entity has 3 lookup fields added.

First will provide user to access user entity and they can fill up these 3 lookup field.

Now when creating a new lead --> System has to automatically sync these lookup fields.

I tried by using 1:N Relationship mapping but no luck. Is there any other way? Workflow option will not work here as it need action first. 

Please help !

**************************************************************************************************************

User Entity                                         Lead Entity

  • Business Unit                                            Business Unit
  • Market Segment                                      Market Segment
  • Region                                                      Region

- Faisal

*This post is locked for comments

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Auto field mapping between entities

    Hi Gowtam,

    On the same script which you gave to me, what if i need to 1 more fieldname to sync, where should i need to add command on the script ?

    1 more fieldname is "new_region" need to sync

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Auto field mapping between entities

    Thanks Ravi for the instruction. Issue is resolved by using Gowtham Code.

    Dear Gowtham, Now on the lead page, when new button click, Business Unit is getting sync from user. Thanks for the code.

    Regards

    Faisal

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Auto field mapping between entities

    Hi Syed,

    Try to debug your script with developer tool or by putting some more alerts in the script after retrieving the value of the business unit and find out till where the script is working fine. For example, put an alert for all your returned value _businessunitid_value etc and see if you are getting value assigned in these variable.

    Debugging your script will help you in future also if you need to change anything.

    blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

    Hope this helps.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Auto field mapping between entities

    Hi Sayed,

    Try with this -

    function setBusinessUnit() {
        var loggedInUserId = Xrm.Page.context.getUserId();
        loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
    
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + loggedInUserId + ")?$select=_businessunitid_value", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.response);
                    var _businessunitid_value = result["_businessunitid_value"];
                    var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
                    var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                    var lookupValue = new Array();
                    lookupValue[0] = new Object();
                    lookupValue[0].id = _businessunitid_value;
                    lookupValue[0].name = _businessunitid_value_formatted;
                    lookupValue[0].entityType = _businessunitid_value_lookuplogicalname;
                    Xrm.Page.getAttribute("new_businessunit").setValue(lookupValue);
    
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }


  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto field mapping between entities

    Try this code:

    function setBusinessUnit() {
    
        var loggedInUserId = Xrm.Page.context.getUserId();
    	loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
        
    	var req = new XMLHttpRequest();
    	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + loggedInUserId + ")?$select=_businessunitid_value", true);
    	req.setRequestHeader("OData-MaxVersion", "4.0");
    	req.setRequestHeader("OData-Version", "4.0");
    	req.setRequestHeader("Accept", "application/json");
    	req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    	req.onreadystatechange = function() {
    		if (this.readyState === 4) {
    			req.onreadystatechange = null;
    			if (this.status === 200) {
    				var result = JSON.parse(this.response);
    				var _businessunitid_value = result["_businessunitid_value"];
    				var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
    				var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    				
    				SetLookUp('new_businessunit', _businessunitid_value_lookuplogicalname, _businessunitid_value, _businessunitid_value_formatted);
    
    			} else {
    				Xrm.Utility.alertDialog(this.statusText);
    			}
    		}
    	};
    	req.send();
    }
    
    // *** FUNCTION: SetLookUp
    // *** PARAMS:
    // ***    fieldName = The name of the lookup field  
    // ***    fieldType = The type of field (contact, account etc)
    // ***    fieldId = The ID of the value to set (GUID)
    // ***    value = the value(name) to set
    function SetLookUp(fieldName, fieldType, fieldId, value) {
    	try {
    		var object = new Array();
    		object[0] = new Object();
    		object[0].id = fieldId;
    		object[0].name = value;
    		object[0].entityType = fieldType;
    		Xrm.Page.getAttribute(fieldName).setValue(object);
    	}
    	catch (e) {
    		alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);
    	}
    }


  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Auto field mapping between entities

    Hi,

    I have changed the parameter as advised and still it is not getting map. Running this code on form Onload Also there is no any error. When click on new lead, Business Unit Field remain empty.

    JFYI - Code

    function setBusinessUnit() {

       var loggedInUserId = Xrm.Page.context.getUserId();

    loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + loggedInUserId + ")?$select=_businessunitid_value", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

    if (this.readyState === 4) {

    req.onreadystatechange = null;

    if (this.status === 200) {

    var result = JSON.parse(this.response);

    var _businessunitid_value = result["_businessunitid_value"];

    var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];

    var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

    SetLookUp(new_businessunit, _businessunitid_value_lookuplogicalname, _businessunitid_value, _businessunitid_value_formatted);

    } else {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

    }

    // *** FUNCTION: SetLookUp

    // *** PARAMS:

    // ***    fieldName = The name of the lookup field  

    // ***    fieldType = The type of field (contact, account etc)

    // ***    fieldId = The ID of the value to set (GUID)

    // ***    value = the value(name) to set

    function SetLookUp(new_businessunit, fieldType, fieldId, value) {

    try {

    var object = new Array();

    object[0] = new Object();

    object[0].id = fieldId;

    object[0].name = value;

    object[0].entityType = fieldType;

    Xrm.Page.getAttribute(new_businessunit).setValue(object);

    }

    catch (e) {

    alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);

    }

    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto field mapping between entities

    change this parameter

    fieldNameOnlead - logical name of business unit field to be set on lead

    SetLookUp(fieldNameOnlead, _businessunitid_value_lookuplogicalname, _businessunitid_value, _businessunitid_value_formatted);

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Auto field mapping between entities

    Hi Syed,

    Assuming you have registered the method on load, can you check the debug console (f12) for any error. Also, after adding the script, publish customization and try in a different browser/ session.

    Hope this helps.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Auto field mapping between entities

    Yes, i have updated the fieldname which is there on the lead page. But still same issue.

    No error showing.

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Auto field mapping between entities

    Did you change the fieldName in this line?

    Xrm.Page.getAttribute(fieldName).setValue(object);


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans