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
SquirrelMasterSquirrelMaster 

Button to verify fields populated help

I'm new to coding in Apex and wanted to see if A) This is even possible and B) if someone could point me in the right direction. 

I'm trying to create a custom button located on the account. 

 

What I want it to do is verify that the billing street, city, state, and zip are populated and if so, then I want it to set another field entitled Accounting Type to Vendor. If any aren't populated, I want an error to state it needs to be populated before it will switch to vendor. 

 

sfdcfoxsfdcfox
{!RequireScript("/soap/ajax/22.0/connection.js")}
if( {! OR(ISBLANK( Account.BillingStreet ),ISBLANK( Account.BillingCity ),ISBLANK( Account.BillingState ),ISBLANK( Account.BillingPostalCode ),ISBLANK( Account.BillingCountry )) } ) {
  alert("Address must be fully populated to change the type to vendor.")
}
else {
  account = new sforce.SObject("Account");
  account["id"] = "{!Account.Id}"
  account["Type"] = "Vendor"
  result = sforce.connection.update([account])
  if(result[0].getBoolean("success")) {
    alert("Type changed to vendor.")
    window.top.location.href = window.top.location.href
  } else {
    alert("Unable to change to Vendor at this time.")
  }
}

Something like this would allow you to create a custom button. The button's type would be "Execute JavaScript". This uses the API to perform the update. To enforce the validation at the database level (for those using import files, etc), you still need a validation rule; this will only stop users from using the UI button.