Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript error

Posted on by

Hello All~

I'm working on a very simple script in CRM 2016 that will auto name the Subject of a Phone Call activity with the name of the Call To (or Call From based on the direction).  I have the code working as expected with the exception if I delete the Call To (for from), I get the below error messages. I tried a try/catch in my code but that isn't catching these errors.  What is the best way to handle this?  Code below

TypeError: Cannot read property 'name' of undefined

TypeError: Cannot read property '0' of null

 

function autonaming() {

var callTo = Xrm.Page.getAttribute("to").getValue()[0].name;
var callFrom = Xrm.Page.getAttribute("from").getValue()[0].name;
var taskCat = Xrm.Page.data.entity.attributes.get("vacu_taskcategorycode").getText();
var direction = Xrm.Page.getAttribute("directioncode").getValue();

if (direction) {

subject = Xrm.Page.getAttribute("subject").setValue( callTo + " - " + taskCat);
} else {
subject = Xrm.Page.getAttribute("subject").setValue( callFrom + " - " + taskCat);
}
};

 

Thank you,
Chrys

 

 

*This post is locked for comments

  • ChrysDW Profile Picture
    ChrysDW on at
    RE: Javascript error

    EXCELLENT! Thank you so very much!

  • Verified answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Javascript error

    try adding an if statement before the code:

    if (Xrm.Page.data.entity.attributes.get("to").getValue() != null && Xrm.Page.data.entity.attributes.get("from").getValue() != null)

    {

    var callTo = Xrm.Page.getAttribute("to").getValue()[0].name;
    var callFrom = Xrm.Page.getAttribute("from").getValue()[0].name;
    var taskCat = Xrm.Page.data.entity.attributes.get("vacu_taskcategorycode").getText(); 
    var direction = Xrm.Page.getAttribute("directioncode").getValue();

    if (direction) {

    subject = Xrm.Page.getAttribute("subject").setValue( callTo + " - " + taskCat);
    } else {
    subject = Xrm.Page.getAttribute("subject").setValue( callFrom + " - " + taskCat);
    }
    };

    }

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