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