Skip to main content

Notifications

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

  • thecharwest Profile Picture
    221 on at
    RE: How to say "do nothing" when a field is blank using web resource jscript

    Thanks for trying, but still no luck.

  • FOR365 Profile Picture
    135 on at
    RE: How to say "do nothing" when a field is blank using web resource jscript

    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
    RE: How to say "do nothing" when a field is blank using web resource jscript

    Hello Nya 

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

  • Suggested answer
    Nya Profile Picture
    29,060 on at
    RE: How to say "do nothing" when a field is blank using web resource jscript

    Hi,

    You can try to replace the

    opportunityTypeValue == null

    with

    !opportunityTypeValue.length

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 181 Super User 2025 Season 1

#2
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 124 Super User 2025 Season 1

Product updates

Dynamics 365 release plans