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
tsalbtsalb 

JS Onclick - Attachment on Object (VFpage PDF)

I was wondering if something like this is possible through S controls and a JS button?

 

The attachment would need to be on the Service_Request__c object, but the Id for the visualforce page references a different object (Proposal__c)

 

I'm getting the error (Expected ';' ) and I can't tell where i'm going wrong...

 

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
sforce.connection.serverUrl = '{!$Site.Prefix}/services/Soap/u/24.0';

try {
	var strQuery="Select Id from Proposal__c where Status__c = 'Awarded' and Service_Request__c = '"+'{!Proposal__c.Service_RequestId__c}'+"'";
	var LoeID = sforce.connection.query(strQuery);
	PageReference savePDF = new PageReference('/apex/vendorloe?rfpId='+LoeID);
	Blob pdfBlob = savePDF.getContent();
	var thisOb = new sforce.SObject("Service_Request__c");
		thisOb.Id = "{!Service_Request__c.Id}";
	var Atch = new sforce.SObject("Attachment");
		Atch.ParentId = thisOb.Id;
		Atch.Name = 'Letter of Engagement'+'.pdf'; 
		Atch.ContentType = 'pdf';
		ATch.Body = pdfBlob;
	
	result = sforce.connection.insert([Atch]);
} 
catch(er) {
	alert(er);
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

PageReference object will not available in Javascript.

But we have a solution for your problem. We can create apex webservice [ Which will contain logic to insert attachment ]

Call this webservice from Javascript code 

Done 

 

Below are the steps you need to do 

1) Create a apex webservice , below is code 

This will contain atcual code for attachment 

global class AttachmentGenerator
{
   webService static String AttachPDFToProposal(string SRId, string ServiceReqId ) 
   {
        string retRes = '';
        try
        {
		 Proposal__c objPro = [Select Id from Proposal__c where Status__c = 'Awarded' and Service_Request__c = : SRId];
         PageReference pageRef = new PageReference('/apex/vendorloe?rfpId=='+objPro.Id );
         Attachment obj = new Attachment();
         obj.Body=pageRef.getContent();
         obj.Name='Letter of Engagement.pdf';
         obj.ParentId = ServiceReqId;
		 obj.ContentType ='pdf';
         insert obj;
         retRes='SUCCESS';
         return retRes;
       }catch(exception ex)
       {
           System.debug('--- Error ----------'+ ex);
           retRes= ex.getMessage();
           return retRes;
       }
   }
}

 

 

 

 

2) Call this webservice from button code 

Below is button code 

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

var res = sforce.apex.execute("AttachmentGenerator","AttachPDFToProposal",{ SRId : '{!Proposal__c.Service_RequestId__c}' ,ServiceReqId : '{!Service_Request__c.Id}' }); 
if(res=='SUCCESS')
window.location.reload(); 
else
alert('Error in attaching file ----'+ res);
}catch(er) 
{ 
alert(er); 
}

 

 

If you need any more assistance , feel free to post 

 

 

Cheers,

Bala

 

All Answers

b-Forceb-Force

PageReference object will not available in Javascript.

But we have a solution for your problem. We can create apex webservice [ Which will contain logic to insert attachment ]

Call this webservice from Javascript code 

Done 

 

Below are the steps you need to do 

1) Create a apex webservice , below is code 

This will contain atcual code for attachment 

global class AttachmentGenerator
{
   webService static String AttachPDFToProposal(string SRId, string ServiceReqId ) 
   {
        string retRes = '';
        try
        {
		 Proposal__c objPro = [Select Id from Proposal__c where Status__c = 'Awarded' and Service_Request__c = : SRId];
         PageReference pageRef = new PageReference('/apex/vendorloe?rfpId=='+objPro.Id );
         Attachment obj = new Attachment();
         obj.Body=pageRef.getContent();
         obj.Name='Letter of Engagement.pdf';
         obj.ParentId = ServiceReqId;
		 obj.ContentType ='pdf';
         insert obj;
         retRes='SUCCESS';
         return retRes;
       }catch(exception ex)
       {
           System.debug('--- Error ----------'+ ex);
           retRes= ex.getMessage();
           return retRes;
       }
   }
}

 

 

 

 

2) Call this webservice from button code 

Below is button code 

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

var res = sforce.apex.execute("AttachmentGenerator","AttachPDFToProposal",{ SRId : '{!Proposal__c.Service_RequestId__c}' ,ServiceReqId : '{!Service_Request__c.Id}' }); 
if(res=='SUCCESS')
window.location.reload(); 
else
alert('Error in attaching file ----'+ res);
}catch(er) 
{ 
alert(er); 
}

 

 

If you need any more assistance , feel free to post 

 

 

Cheers,

Bala

 

This was selected as the best answer
tsalbtsalb

Thanks for your solution - I've implemented it and it's working - but I was wondering if we can take it a step further and automate it completely?

 

I'll likely start a new post for this - but i was wondering if you could assist me first. Instead of having this through a button - I've modified a trigger to perform this functionality.

 

However, I realized that i can't use the PageReference and getContent methods in a trigger. How would I be able to automate a pickval change (srf.Status__c = 'Appraisal Contracted') and then programatically call a method and provide it the variables needed to do the attachment creation?

trigger CreateDocuments on Service_Request__c (after update) {

	List <Service_Request__c> srfList = 
	  [SELECT s.Status__c, s.Id
	   FROM Service_Request__c s
	   WHERE (s.Status__c = 'Appraisal Contracted' || s.Status__c = 'Completed')
	   AND s.Service_Request__c.Id IN :Trigger.new];
		 
	for (Service_Request__c srf: srfList) {
          //Looking for newly contracted LOEs to create in Attachments
	    if ( s.Status__c = 'Appraisal Contracted') {
	      Proposal__c rfp = [SELECT Id, Name, Status__c, Service_Request__c, Vendor__c, Vendor__r.Name 
			         FROM Proposal__c 
			         WHERE Status__c = 'Accepted' AND Service_Request__c = :srfList];
	  //Create the PDF
	    PageReference savePDF = new PageReference('/apex/vendorloe?rfpId='+rfp.Id+'&vendorId='+rfp.Vendor__c);
	    Blob pdfBlob = savePDF.getContent();
	    attachment Atch = new Attachment(ParentId = s.Id, Name = 'Letter Of Engagement - '+rfp.Vendor__r.Name+' - '+rfp.Name+'.pdf', ContentType = 'pdf', Body = pdfBlob);
	    insert Atch;
            }
	}
}