2

I have a business requirement where a new Quote record cannot be generated for a couple different Opportunity types. If the user tries, they get an alert. I've overridden the standard New Quote button with a javascript button. But a new requirement came in yesterday saying that one profile can generate contracts regardless of Opportunity Type. When I added this second condition to the if statement with an && logical, it seems to abort after the if and not proceed onto the other conditionals. A slightly modified version of the code is below.

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/23.0/apex.js")} 
var cQuote = new sforce.SObject("Quote");
var QuoteRecord = sforce.connection.query("Select OpportunityId, Id From Quote where OpportunityId='{!Opportunity.Id}' "); 
var records = QuoteRecord.getArray("records");
if('{!Opportunity.Type}'=='Example Type' && '{!User.Profile}' != 'Example Profile Type') {
    alert("A quote record cannot be created for this Opportunity Type.");
    window.parent.location.href=window.parent.location.href;
}
else if(records[0] != null) { 
    alert('A Quote Entry already exists.'); 
    window.parent.location.href=window.parent.location.href;
}
else {
    cQuote.Name="Do Not Edit";
    cQuote.Status="Active";
    cQuote.Opportunityid="{!Opportunity.Id}";
    var result=sforce.connection.create([cQuote]);
    if (result[0].getBoolean("success")) {
        window.parent.location.href='/'+result[0].id;
    }
}

I checked the javascript console - the only difference between the old button without the !User.Profile condition and the new button with it is that the new button returns two error messages that say "Refused to set unsafe header "User-Agent". See below image for more detail. I would really appreciate some insight on this. Here's the console error

  • Since User is a global visualforce variable, it needs a dollar-sign, e.g. $User.Profile. – Phil Rymek Feb 26 '14 at 16:36
  • @phil Rymek Tried it like this: if('{$User.Profile}'!='System Administrator') { alert("A Q/C record cannot be created for this Opportunity Type."); I'm getting the error message even though my profile is System Administrator. – Matt Santy Feb 26 '14 at 16:42
  • All merge fields require the exclamation, and the global user variable requires the dollar sign. {!$User.Profile} – Phil Rymek Feb 26 '14 at 16:46
  • Now I get: Error: Field Profile does not exist. Check spelling. – Matt Santy Feb 26 '14 at 16:47
  • 2
    I feel like a goon this has gone back and forth so much, but this page suggests its {!$Profile.Name} – Phil Rymek Feb 26 '14 at 16:55
4

This example worked for me:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

if('{!Opportunity.Type}' == 'New Customer' && '{!$Profile.Name}' != 'System Administrator') {
    alert("A quote record cannot be created for this Opportunity Type.");
    window.parent.location.href=window.parent.location.href;
}
else {
alert('Your Sys Admin');
}

If I change the Profile check to be something else, like "System Admin1", then the IF fails, and the redirect fires.

1

The reason this is happening is because Profile is not a valid field. Please use the following:

{!$User.ProfileId}

You can test this by creating a button which executes javascript and paste this in the behavior

alert('{!$User.ProfileId}');

If you wish to go by profile name you'll have to do something like this via javascript:

{!REQUIRESCRIPT("/apex/dsfs__DocuSign_JavaScript")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 

var queryResult = sforce.connection.query("SELECT Id, Name FROM Profile WHERE Id = '{!$User.ProfileId}'"); 
var profileRecord = queryResult.getArray('records')[0];

alert(profileRecord.Name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.