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 are populated

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. 

 

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. 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

just pu this in the end of that script code.

 

location.href = '/'+"{!Account.Id}";

All Answers

Shashikant SharmaShashikant Sharma

 

Yes it is possible,

 

Create a Detail Page Button

 

Execute as java script

 

Use this sort of syantax

 

var accName = "{!Account.Name}";
alert(accName);

 Similarly you can access your field that you want to verify they are populated or not.

 

Let me know if it works for you to validate , then we can procedd to set  Accounting Type to Vendor

SquirrelMasterSquirrelMaster


Thanks, that works for validating. What about setting accounting type to vendor now?

Shashikant SharmaShashikant Sharma

Gr8 now we can move to update

 

Use this code

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

var account = new sforce.SObject("Account");
account.id = "{!Account.Id}";
account.Name = 'Test ABCD';
var result = sforce.connection.update([account]);

 Change .Name with your field API Name , I took Name for example purpose.

SquirrelMasterSquirrelMaster

I'm getting errors on this now. I just need it to select a checkbox

Shashikant SharmaShashikant Sharma

Can you give me your code as well as your field API name of check box

SquirrelMasterSquirrelMaster

thanks, i pmed you the information

SquirrelMasterSquirrelMaster

So it is working now, but I have to refresh the page after I hit the button to see that it checked the field. Can we add something that will refresh it after I press the button?

Shashikant SharmaShashikant Sharma

I just used this

 

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

var account = new sforce.SObject("Account");
account.id = "{!Account.Id}";
account.CheckBoxField__c = 'True'; 
var result = sforce.connection.update([account]);

 It orked for me, can you try it once more, one thing this code will not refresh yur screen automatically so if you don't see result please refresh your page by hitting the url. If you want to refresh the page automatically I can provide you the code

 

SquirrelMasterSquirrelMaster

Yes thank you so much for this. Is it easy to get it to refresh automatically?

Shashikant SharmaShashikant Sharma

just pu this in the end of that script code.

 

location.href = '/'+"{!Account.Id}";

This was selected as the best answer
SquirrelMasterSquirrelMaster

Thanks a lot Shashikant. You really helped me out today. 

Shashikant SharmaShashikant Sharma

Your welcome SquirrelMaster