function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JasonGonzalesJasonGonzales 

Prevent creation of Opportunity based on Account field value

Hi - very new to writing Apex and I'm stumped on this requirement: display an message when user clicks on the New Opportunity button in the Contact record if the Account where it belongs has the Prohibited field set to "Embargoed Country". Below is what I've written so far:
 
trigger ProhibitedOpp1 on Opportunity (before insert) {
    Map<Id,Opportunity> opps = new Map<Id,Opportunity>([Select Id,Account.Prohibited__c From Opportunity Where Id in : Trigger.new]);
    for (Opportunity opp: trigger.new)
    { if (opp.account.Prohibited__c=='Embargoed Country') {
        opp.adderror('You cannot create an Opportunity from an Account located in a restricted or embargoed country');
    }
   
    }
}

Can somebody please tell me what I need to change/update in my code? Thanks in advance! :)
Terence_ChiuTerence_Chiu
Hi Jason, try the following:

You had queried the account.prohibited__c field and stored the records in Map called opps, however, your were iterating over the records in trigger.new which would not have the account field value.
 
trigger ProhibitedOpp1 on Opportunity (before insert) {
   List<Opportunity> opps = [Select Id,Account.Prohibited__c From Opportunity Where Id in : Trigger.new];
    for (Opportunity opp: opps)
    { if (opp.account.Prohibited__c=='Embargoed Country') {
        opp.adderror('You cannot create an Opportunity from an Account located in a restricted or embargoed country');
    }
   
    }
}

Let me know if that helps.
 
Srinath R 9Srinath R 9
Hi Jason,

You could achieve this using a simple validation rule. I am not sure why you went the apex way.

The validation rule should check ISNEW() on opportunity and also check the related account field value.

Its very simple one. Try it and let me know if it works this way.

Thanks
Srinath R
JasonGonzalesJasonGonzales
@srinath - Yes I am aware of that. However, I would like the error message to pop up the moment they click on the button and not upon saving.

@terrence_chiu - thanks for the suggested code but it didn't work. I was still able to save an Opp even if the Account is tagged as embargoed.
Terence_ChiuTerence_Chiu
Jason, if the goal is to display an error the moment someone clicks on the New Opportunity button, then perhaps you should consider creating a custom button that executes Javascript to replace any standard new opportunity buttons.  For instance create a List button from the Opportunity object where the behaviror setting is to Execute Javascript. The code would look something like the following:
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

//store contact record Id for query
var cId = '{!Contact.Id}'; 

//query string to pull the account custom field related to the contact
var ctQueryStr = "SELECT Id, AccountId, Account.Prohibited__c, LeadSource FROM Contact WHERE Id = '" + cId + "'"; 

//query call
var ctRes = sforce.connection.query(ctQueryStr); 

//get records from query results
var ctRecs = ctRes.getArray("records"); 

//if the Contact record's account is an Embargo country alert the user
if(ctRecs[0].Account.Prohibited__c == 'Embargo Country'){ 
alert("You cannot create an Opportunity from an Account located in a restricted or embargoed country"); 
} 

//otherwise re-direct user to the Opportunity edit page passing the values that would have been passed as url parameters in the query string as the standard list button

else{ 
window.top.location = "/006/e?lookupcmpgn=1&retURL=%2F" + cId + "&accid=" + ctRecs[0].AccountId + "&conid=" + cId + "&opp6=" + ctRecs[0].LeadSource; 
}

The user would not even get to the edit page via this method. Adding an error via a trigger would still require the user to save the record first before the error occurs. Let me know if this helps.