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
sangam vvsangam vv 

Friends i am stuck in java script button validation Rule?

I have Doctor Object in that i have status (picklist) field.
In detail page button I want to Send someting(Email) when Status is available
or else if status is Blocked i want to show error message
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Sangam,

Please try this. Validation is on Opprotunities' stage fiels. Please change accrodingly.
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

if('{!Opportunity.StageName}' == 'Qualification') {
  //Send email here 
} else {
    alert('Error message');
}

Hope it will help you. 

Thanks, 
Sumit Kuamr Singh
 
 
sangam vvsangam vv
its working fine.how to do email functionality here.
Here i want to send one pdf attachment
Rohit K SethiRohit K Sethi
Hi sangam ,

As 'Sumit' said the js condtions is right. and now implement logic for  "send email" section sending you need to call the apex web service class by using sforce .

var callback = {                
                 onSuccess:function(result){  alert(result);  }, 
                 onFailure:function(error){ alert(error);} 
               };
try{
    var res = sforce.apex.execute('ApexClassName','MethodName',{Id: '{!Opportunity.id}'}, callback);
   
response = res;
}catch(err){
    response = err;
}
global class ApexClassName {
	Webservice static Boolean MethodName(String Id){
		
		List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>(); 
		List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
		/*Query on attachment*/
		for (Attachment a : [select Name, Body, BodyLength from Attachment where id = :attIds]){
			Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
			efa.setFileName(a.Name);
			efa.setBody(a.Body);
			fileAttachments.add(efa);
		}
		/*Set Field value according your need*/
		
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
		email.setSubject();
		email.setTargetObjectId();               
		email.setSaveAsActivity(false);
		email.setHtmlBody('Hi');
		email.setFileAttachments(fileAttachments);
		emailList.add(email);
		if(emailList.size()>0)
			Messaging.sendEmail(emailList);                      
	}
}
don't run as it is code. This code have some Pseudo code so change code according to your requirement.


If this post solves your problem kindly mark it as solution.
Thanks.
 
sangam vvsangam vv
Hi guys again i am stuck
scenario:
Object 1 have 3 records i need to create 1 button submit for approval  if i click that button if the status is approvred then i want to send attachment
if the status is pending Popup 1 error messge. how to do in Java script
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Just modif a bit, the example given by Rohit - 

You have opportunity ID in the method - make a query -

Opprorunity o = [Select StageName from Opporttunity where Id : Id]
....
..
//Here check whether to send the attachment or ont?
if(o.StageName.equalsIgnoreCase('MtStageName')) {
     email.setFileAttachments(fileAttachments);
}