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

Notifications

Announcements

No record found.

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)
  • Suggested answer
    Mark Spiers Profile Picture
    1,085 on at

    Hi Jennifer,

    if i understand correctly, your saying the workflow is 'too slow' at doing this (IE: you want this to happen BEFORE the user saves the opp for the first time?).

    if On save is ok, then a plugin running in sync mode would do it 'just before' the user saves, otherwise there is jscript.

    You will need to perform a lookup and set the value of the PA field accordingly.

    just to be clear, you do NOT want this PA field to be updated? (ie the accounts PA field gets updated, you want all historic opportunities to stay at the old value?).

    check out

    worldofdynamics.blogspot.co.nz/.../microsoft-dynamics-crm-2011-retrieve.html

    for ideas.

    you should be able to change that to meet your needs.

    Regards,

    Mark

  • Community Member Profile Picture
    on at

    Thats correct Mark. A workflow would be to slow. I want the fields to populate on create of a new opportunity only not when the opportunity form is opened to review a current opportunity. I am struggling on how to A. only allow the code to run on form create and B. Get the values of the 5 lookup values I need from the account entity and set those 5 values on the opportunity entity. Any help you can provide on creating this code would be helpful. I have created a few javascripts for CRM but am by no means a developer.

    I should state that I have looked at the link you sent and made a stab at the code but am not making any progress what soever.

  • GL-23071537-0 Profile Picture
    on at

    Hi Jennifer,

    I wonder if you did an Onload but then before poplutiung the PA field, you checked for a certain criteria that would indicate this is a new record (e.g. Last modified is blank or older than X?).  Just a thought.

  • Mark Spiers Profile Picture
    1,085 on at

    to find out if its a create form or an update form do... (from crmbusiness.wordpress.com/.../crm-2011-using-javascript-to-identifying-if-a-form-is-in-create-or-update-contect)

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

    If the return value is 1 –  Form context is Create

    If the return value is 2 –  Form context is Update

    do your two lookup fields use the same option list (global option list, so all of the values are the same?).

    or do you update the option lists independantly?

    i have a meeting shortly, but ill try write something up quickly for you.

    Regards,

    Mark

  • Community Member Profile Picture
    on at

    They all pull values from the account lookup view. We have different account types that are popluated in the 5 fields based on the type they are.

    Below is the code I have started working on thus far. I continue to get an error on load: Unable to get value of the property 'getValue': object is null or undefined.

    //using shortcut method of Xrm.Page.data.entity.attributes method
    function SetOpportunityConnection()
    {
    if (crmForm.FormType ==1)
    {
    //To get an attribute value;
    var lookup1= window.parent.opener.Xrm.Page.getAttribute("new_lookup1").getValue();   
    var lookup2= window.parent.opener.Xrm.Page.getAttribute("new_lookup2").getValue();   
    var lookup3= window.parent.opener.Xrm.Page.getAttribute("new_lookup3").getValue();
    var lookup4= window.parent.opener.Xrm.Page.getAttribute("new_lookup4").getValue();
    var lookup5= window.parent.opener.Xrm.Page.getAttribute("new_lookup5").getValue();
    {
    // Set the form control value to the lookupItem just created.
    Xrm.Page.getAttribute("new_lookup1").setValue(lookup1);
    Xrm.Page.getAttribute("new_lookup2").setValue(lookup2);
    Xrm.Page.getAttribute("new_lookup3").setValue(lookup3); 
    Xrm.Page.getAttribute("new_lookup4").setValue(lookup4);
    Xrm.Page.getAttribute("new_lookup5").setValue(lookup5);
    Xrm.Page.getAttribute("new_6").setValue(null);  //not used on both forms but it does carry over the parent account value so i want to set to null
      }
     }

  • Mark Spiers Profile Picture
    1,085 on at

    function GetDetails() {

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

    if (formType ==1)
    {
        var EntityName, EntityId
        var PaType, PaID, PaName;
        var resultXml;
        var fieldname = "new_paaccount"; //this is the name of the field on the accounts page
        var oppfieldname = "new_paaccount"; //this is the name of the field on the opp page
       
        //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('customerid');

     
        // 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, fieldname);

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

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

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

     
                // If XML document has primarycontactid lookup attribute then assign to local variable 
                PaType = resultXml.selectSingleNode('//q1:' + fieldname).getAttribute("type") ;
               
    Xrm.Page.getAttribute(oppfieldname).setValue( [{id: PaID, name: PaName, entityType: PaType}]);
            }       


     
        }
    }
    }
     
    // 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;
        }
    }

  • Suggested answer
    Mark Spiers Profile Picture
    1,085 on at

    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

  • Mark Spiers Profile Picture
    1,085 on at

    is this what you were looking for?

  • Community Member Profile Picture
    on at

    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.

  • Neil Benson Profile Picture
    7,369 User Group Leader on at

    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.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans