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 365 | Integration, Dataverse...
Suggested Answer

How to say "do nothing" when a field is blank using web resource jscript

(0) ShareShare
ReportReport
Posted on by 221

Hello - I have a jscript triggering onchange of a lookup field on Opportunities which is meant to create an alert if the wrong record is selected. The problem is that if the field is empty (so if someone selects the record which triggers the error, and then wants to remove it to replace it with the appropriate record), the jscript throws up an error "cannot read properties of null (reading '0')". How can I change my jscript to say "if the field is blank, do nothing"? I already tried adding in a row but unfortunately I am not trained in writing code so evidently what I've done is not correct! Any help would be greatly appreciated.

Here is my script:

//This function gives an error message if the Opp Type is New Business but there is an Originating Lead.
function newbusopperror(executionContext) {
formContext = executionContext.getFormContext();

// Get Opportunity Type
var opportunityTypeValue = formContext.getAttribute("safe_opportunitytype").getValue();
var record_id = opportunityTypeValue[0].id;
var Lead = formContext.getAttribute("originatingleadid").getValue();
{
if (opportunityTypeValue != null &&
record_id.toUpperCase() == "{8815B22C-80E0-EA11-A813-000D3A4B2C9E}" && Lead == null) {
alert("New Business Sales Should Always Start as a Lead");
} else if (opportunityTypeValue == null) {
null
}
};
}

And here is the error text in full just in case it's helpful:

TypeError: Cannot read properties of null (reading '0')
Error Details:
Event Name: onchange
Function Name: newbusopperror
Web Resource Name: samar_/NewBusinessOppError
Solution Name: Active
Publisher Name: DefaultPublisherorg36a1d10f

I have the same question (0)
  • Suggested answer
    Nya Profile Picture
    29,060 on at

    Hi,

    You can try to replace the

    opportunityTypeValue == null

    with

    !opportunityTypeValue.length

  • thecharwest Profile Picture
    221 on at

    Hello Nya 

    I'm afraid that didn't work and I'm still stuck with the same error message.

  • FOR365 Profile Picture
    135 on at

    Changed your code to:

    //This function gives an error message if the Opp Type is New Business but there is an Originating Lead.

    function newbusopperror(executionContext) {

    formContext = executionContext.getFormContext();

    // Get Opportunity Type

    var opportunityTypeValue = formContext.getAttribute("safe_opportunitytype").getValue();

    var record_id = opportunityTypeValue[0].id;

    var Lead = formContext.getAttribute("originatingleadid").getValue();

    if (opportunityTypeValue != null &&

    record_id.toUpperCase() == "{8815B22C-80E0-EA11-A813-000D3A4B2C9E}" && Lead == null) {

    alert("New Business Sales Should Always Start as a Lead");

    } else if (opportunityTypeValue == null) {

    null

    }

    }

    Please try again.

  • thecharwest Profile Picture
    221 on at

    Thanks for trying, but still no luck.

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 51 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans