Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create URL from a number of fields using javascript

Posted on by 30

Hi!

I am trying to create a URL field based on a number of fields within any given company page. The main problem i am encountering is when trying to add the primary contact of a company to the string. It resolves to object Object instead of the name of the contact. Is thier anyway to pull the name of the contact that is recorded within a company? Below is my current JS

function CreateURL() {

var employeeEmail = Xrm.Page.data.entity.attributes.get("mc_nutraceuticalsowner").getValue();
var distributor = Xrm.Page.data.entity.attributes.get("mc_distributorforms").getValue();
var pharmacyName = Xrm.Page.data.entity.attributes.get("name").getValue();
var pharmacyAccountNumber = Xrm.Page.data.entity.attributes.get("mc_membershipnumber").getValue();
var state = Xrm.Page.data.entity.attributes.get("mc_stateforms").getValue();
var customerContactName = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue();

var accountUrl = "https://forms.example.com/view.php?element_279="+employeeEmail+"&element_5="+distributor+"&element_34="+pharmacyName+"&element_33="+pharmacyAccountNumber+"&element_51="+state+"&element_280="+customerContactName+"&id=20314";

Xrm.Page.data.entity.attributes.get("mc_accounturl").setValue(accountUrl);

}

The current URL being given from the script is: 

https://forms.example.com/view.php?element_279=%5bobject%20Object%5d&element_280=%5bobject%20Object%5d&element_33=1111111&element_34=Medlab%20Clinical%20LTD&element_5=3&element_51=1

*This post is locked for comments

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Create URL from a number of fields using javascript

    change this line

    var customerContactName = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue();

    to

    var customerContactName = "";

    var customerValue = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue();

    if (customerValue != null) { customerContactName = customerValue[0].name; }

  • HarryZang Profile Picture
    HarryZang 240 on at
    RE: Create URL from a number of fields using javascript

    The "primarycontactid" is a lookup field, it is actually a lookup array,

    so you need to get the value by using the following code

    var customerContactName = Xrm.Page.data.entity.attributes.get("primarycontactid")[0].getValue();  //try access the property of the object, you should able to get the fullname

    Please note, add null check when necessary, otherwise the code may broke,

    In addition, you may need to encode your URL as well.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans