Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Update records - from lead to custom entity

Posted on by 815

Hi,

I have created a custom entity as "Study customer requirement" and on the lead entity have created a lookup to custom entity. In the custom entity there are 2 fields customer and contacts which is also there on the lead page.

Now on the lead page our regular practice is we select customer and contacts and after saving lead, clicking on Study Customer requirement lookup and select new. Now when new page comes it has to pick customer and contact from the lead page from where we start.

I tried with WF update records but it is not syncing the fields.

Pl help.

*This post is locked for comments

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Update records - from lead to custom entity

    Thanks a lot Ravi, Code works perfectly.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Also, you need to use "leadContact[0] != null" check only for lookup fields. If the field is simple then you don't need this check.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Hi,

    This should work provided the schema name on your lead record is "new_existingcustomer" and on you custom entity is "new_customer" and both are of same data type.

    Hope this helps.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Update records - from lead to custom entity

    Thanks a lot Ravi,

    Your updated code works perfectly.

    Just wanted to know if need to add 1 more field, i have modified the same code, can i use like this ?

    function getleadDetails() {

      if (parent.window.parent.opener != null) {

          var leadContact = parent.window.parent.opener.Xrm.Page.getAttribute("new_existingcontact").getValue();

          var leadCustomer = parent.window.parent.opener.Xrm.Page.getAttribute("new_existingcustomer").getValue();

          if (leadContact != null && leadContact[0] != null) {

              Xrm.Page.getAttribute("new_contact").setValue(leadContact);

          }

    if (leadCustomer != null && leadContact[0] != null) {

              Xrm.Page.getAttribute("new_customer").setValue(leadContact);

          }

      }

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Try with this:

    function getleadDetails() {

       if (parent.window.parent.opener != null) {

           var leadContact = parent.window.parent.opener.Xrm.Page.getAttribute("parentcontactid").getValue();

           if (leadContact != null && leadContact[0] != null) {

               Xrm.Page.getAttribute("new_contact").setValue(leadContact);

           }

       }

    }

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Yes, your understanding is correct. We need to put some check there so that it run's only when it is opened from the lead form.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Update records - from lead to custom entity

    Hi Ravi,

    Thanks for the code.

    First it works when opening new 'Study customer requirement" also picks contact but issue here is after saving the record when opening back getting error.

    TypeError: Cannot read property 'Xrm' of null

       at getleadDetails (crm/.../new_leadcontactsync_toappstudy)

       at eval (eval at RunHandlerInternal (crm/.../ClientApiWrapper.aspx), <anonymous>:1:1)

       at RunHandlerInternal (crm/.../ClientApiWrapper.aspx)

       at RunHandlers (crm/.../ClientApiWrapper.aspx)

       at OnScriptTagLoaded (crm/.../ClientApiWrapper.aspx)

       at crm/.../ClientApiWrapper.aspx

    I believe it is running each time when saved records get open?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Hi Syed,

    You can register the below method on load of your custom entity. This method first retrieves the contact from lead and then on next statement sets that contact to your custom entity contact lookup. You need check & update the schema name.

    =========

    function getleadDetails() {

       var leadContact = parent.window.parent.opener.Xrm.Page.getAttribute("parentcontactid").getValue();

       if (leadContact != null && leadContact[0] != null) {

           Xrm.Page.getAttribute("new_contact").setValue(leadContact);

       }

    }

    =============

    Hope this helps.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Update records - from lead to custom entity

    Hi Ravi,

    Sorry i am not good in coding, could you please share complete Js.

    Regards

    Faisal

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Update records - from lead to custom entity

    Just did a quick try. You can access lead record field using window.opener.

    window.opener.Xrm.Page.data.entity.getId()

    So, you can add a mthoed on the load of your custom entity and then retrieve the fields from lead as above and set it to the custom entity fields in the normal ways i.e. Xrm.Page.getAttribute(<SchemaName>).setValue(window.opener.Xrm.Page.getAttribute(<SchemaName>).getValue());

    Hope this helps.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans