web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

map data between two custom fields

(0) ShareShare
ReportReport
Posted on by

I want to create a map between two custom fields that are lookup fields in CRM 2011

We have a type of account called Physicians Assistants that work with different clinics

On the account form we have a field called PA filtered to PA type

On the Opportunity field we have a field called PA that is a look up field to accounts filtered to PA type

My requirement is to have the PA field from account when filled in to flow down to the PA field in opportunity.

When I create the lookup field it creates a 1:N relationship on the account tying the Accountid to the field. This then brings the parent account down to the field and we want the actual value set in the PA field on the Account type. How can I accomplish this. I do not see a way outside of a workflow to handle this however we want it to happen when a new opportunity is started from an account so a workflow will not suffice.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    Re: map data between two custom fields

    This code worked great. One outstanding issue I have is where this all started. If the account fields do not have anything populated the parent company still populates in the account related fields. I know the code run above is only activated if the fields are not null or empty. Is there a way to set a paramter that if the account fields are null or empty that the opportunity fields are "blanked out" or made null? I am trying to outsmart the microsoft code that sets these fields through mappings that can not be removed because the fields are setup as lookup fields.

    I also wanted to thank you for not just handing me the answer. I like to try to figure these out as much as possible on my own so that I can learn from the mistakes I have made. I admit it took me a good 6 tries to get this one right because we have two forms under opportunity and I didnt change one of them but that is how I learn best by correcting my mistakes. Your guidance is very much appreciated.

  • Verified answer
    Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    Ok. so its bascially the same script, in the comments is an example of how to use it.

    //useage (these will be case sensitive, so use exactly what is displayed in the customizations section of crm, normally lower case).

    //GetLookup('account', 'new_paaccount','new_pa2,'customerid');

    in the above GetLookup....

    'account' is the entity you are wanting to get the value from.

    'new_paaccount' is the name of the field on the above entity to grab.

    'new_pa2' is the name of the field you want to put the above value into.

    'customerid' is the name of the field on this entity that holds a lookup to the entity you want to grab the value from.

    Sorry if thats confusing, but yeah thats basically it.

    anything i removed i just commented out so you can see the difference.

    basically, instead of the variables inside the function, i moved them to arguements for the function.

    so that you can call this function for a number of different possibilities instead of just hardcoding the lot.

    Enjoy.

    as all ways, let me know if any part of it is confusing or you need help.

  • Verified answer
    Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    function GetDetails()
    {
    //put a new one of these for each field you want done
    GetLookup('account', 'new_paaccount','new_paaccount','customerid'); //this does exactly what the origional script does
    GetLookup('account', 'new_paaccount','new_pa2','customerid'); //this puts the same field into new_pa2 in the opportunity
    GetLookup('EntityFrom', 'From_Field', 'To_Field', 'LinkField');

    }


    function GetLookup(fromEntity, fromField, toField, relationField) {

     var  formType= Xrm.Page.ui.getFormType();

    if (formType ==1)
    {

    //20120809 Changes to allow multiple calls.
    //arguments added to this function.
    // fromEntity takes the place of our assumption that we were relating against an account.
    // fromField tales the place of the var 'fieldname' from the origional script.
    // toField takes the place of the var 'oppfieldname' from the origional script.
    // relationfield takes the place of the assumption that the lookup for the account on the opportunity is named 'customerid'.

    //useage (these will be case sensitive, so use exactly what is displayed in the customizations section of crm, normally lower case).
    //GetLookup('account', 'new_paaccount','new_paaccount','customerid');

        var EntityName, EntityId
        var LookupType, LookupID, LookupName; //Note, these have been renamed
        var resultXml;
     //var fieldname = "new_paaccount"; //this is the name of the field on the accounts page - This is no longer used
     //var oppfieldname = "new_paaccount"; //this is the name of the field on the opp page - This is no longer used
     
     //This is the name of the field that contains the account id, change it if you need to.
        LookupFieldObject = Xrm.Page.data.entity.attributes.get(relationField);

     
        // If lookup field has value then the code will only run
        if (LookupFieldObject.getValue() != null) {

     
            //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables
            EntityId = LookupFieldObject.getValue()[0].id;
            EntityName = LookupFieldObject.getValue()[0].entityType;
           
      //new_patype is the name of the field that you want to get from the related account
      //i would imagine you need to change this.
            resultXml = RetrieveEntityById(EntityName, EntityId, fromField);

            
              // In retrieved XML document check if it has primarycontactid lookup attribute 
                if (resultXml != null && resultXml.selectSingleNode('//q1:' +fromField) != null) {

     
                // If XML document has primarycontactid lookup attribute then assign to local variable 
                LookupID = resultXml.selectSingleNode('//q1:' + fromField).nodeTypedValue;

     
                // If XML document has primarycontactid lookup attribute then assign to local variable 
                LookupName = resultXml.selectSingleNode('//q1:' +fromField).getAttribute("name");

     
                // If XML document has primarycontactid lookup attribute then assign to local variable 
                LookupType = resultXml.selectSingleNode('//q1:' + fromField).getAttribute("type") ;
       
       Xrm.Page.getAttribute(toField).setValue( [{id: LookupID, name: LookupName, entityType: LookupType}]);
            }       


     
        }
    }
    }
     
    // Do not make any changes to this function
    function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {

     
        var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;

     
        arrayEntityColumns = prmEntityColumns.split(",");

     
        for (var i = 0; i < arrayEntityColumns.length; i++) {
            xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
        }

     
        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>" + prmEntityName + "</entityName>" +
        "<id>" + prmEntityId + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>" +
        xmlEntityColumns +
       "</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;
            alert("Error Message : " + msg);
        }
        else {
            return resultXml;
        }
    }

     

  • Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    Sorry, i could have given you this eariler, but i wanted to see if you were going to give it a crack your self (and good job for giving it a go).

    ill paste the new script below.

  • Community Member Profile Picture
    on at
    Re: map data between two custom fields

    Mark you are correct we do not want these fields to stay in sync with the account but rather only capture what is on the account at the time the opportunity is created.

    I am able to get your code to work correctly as individual functions for each of the 5 fields I need to copy over however I am unsure of how to call a function that will run all 5 subfunctions. Any assistance you can provide would be appreciated.

    As a side note thank you for your comments in the code it helped me to desiminate what was happening in the code so that I could figure out what each area was doing. What a great learning experience. You should teach this stuff :)

  • Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    Thanks Neil,

    In this case, i believe the fact that it is cut off from the origional value (And therefor does not update) is a feature, not a bug for this user.

    the way i understand it, they only care about the value when the opp is created, not after the fact.

    otherwise mapping/relationships or a plugin could be used as you suggest.

    a workflow would work if the user was ok with it taking ~5 minutes to get the value.

  • Neil Benson Profile Picture
    7,369 User Group Leader on at
    Re: map data between two custom fields

    Mark's code is very helpful. I've used similar code on a project. The downside with this approach, as Mark has pointed out, is that if any of the values in the account are updated, then all the opportunities which have values copied from the account will have different values to the account.

    For this reason, it is more common to use plugins which fire when the opportunity is created and when the account is updated.

  • Community Member Profile Picture
    on at
    Re: map data between two custom fields

    im so sorry my day got away from me. It looks as though it is. I will try to address converting it to match my needs tomorrow. Thank you so very much for your help.

  • Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    is this what you were looking for?

  • Suggested answer
    Mark Spiers Profile Picture
    1,085 on at
    Re: map data between two custom fields

    Above is my adaptation of the script i linked you to eariler.

    it will set the value of new_paaccount on the opportunity form to the value of the new_paaccount on the accounts form.

    ONLY on new opportunity.

    ONLY if you create the new opportunity from the accounts form.

    read it above, if you need help figureing out what to change to get it work for oyu let me know (most of what you need to change should be at the top).

    if you are skillful, make a new function that calls this one (and give this function args), and run this function 5 times supplying differnt args each time to do your 5 lookup fields.

    Regards,

    Mark

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans