Insert Pdf and and Attachemt
There is Detail Page Button On Contract Object when Click on this button then it call another visual force based on Type Field Value and Genrate pdf after that again a pdf is created and after that first pdf is attached under second pdf and redirect to second pdf object ID
public class clsGenerateAndSavePdf {
public boolean err{get;set;}
public boolean isTest = false;
public echosign_dev1__SIGN_Agreement__c agmt ;
public clsGenerateAndSavePdf(){
err = false;
agmt = new echosign_dev1__SIGN_Agreement__c();
}
public pageReference generatePdf(){
//GenrateAgreementPdf();
String id = '';
if(ApexPages.currentPage().getParameters().get('id')!= null && ApexPages.currentPage().getParameters().get('id')!= ''){
id = ApexPages.currentPage().getParameters().get('id');
try{
Contract ob = [select id, Type__c from Contract where id = :id];
String str = '';
if(ob.Type__c == 'Restaurant Agreement SS'){
str = 'restaurantAgreementSS';
}else if(ob.Type__c == 'Restaurant Agreement AyCE'){
str = 'restaurantAgreementAyCE';
}else{
str = 'restaurantAgreement';
}
pagereference pdf = new pagereference('/apex/'+str+'?id='+id);
system.debug('!pdf'+pdf);
pdf.setRedirect(true);
Blob b ;
if(!isTest){
b = pdf.getContent();
}else{
b = blob.valueOf('test mode');
}
Attachment a = new Attachment(parentId = id, name=ob.Type__c+'.pdf', body = b);
insert a;
GenrateAgreementPdf();
Attachment Attach = a.clone();
Attach.parentId = agmt.id;
insert Attach;
return new pagereference('/'+agmt.id);
//return null;
}catch(Exception e){
system.debug('!Bingo '+e);
err = true;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error occur. Please try after some time.'));
}
}else{
err = true;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Url !'));
}
return null;
}
public void GenrateAgreementPdf()
{
String contractid = ApexPages.currentPage().getParameters().get('id');
Contract objcontract = new Contract();
objcontract = [Select id, AccountID,ACCOUNT.Name from Contract where Id =:contractid limit 1 ];
//To get Contact Id
Contact objcon = new Contact();
system.debug('objcontract.AccountID++++'+objcontract.AccountID);
if(objcontract.AccountID !=null){
list<Contact> lstcon=new list<Contact>();
lstcon= [Select ID, AccountId,Account.Name,Email from Contact where AccountID =:objcontract.AccountID and Email !='' order by LastModifiedDate desc limit 1];
if(lstcon.size()>0 && lstcon !=null)
{
objcon =lstcon[0];
}
}
// To Get Merge Mapping Id
/* echosign_dev1__SIGN_Merge_Mapping__c objMergeMapping = new echosign_dev1__SIGN_Merge_Mapping__c();
objMergeMapping =[Select id, Name from echosign_dev1__SIGN_Merge_Mapping__c where Name ='TestMap2_ppt' limit 1];*/
// To Get Merge Mapping Id
String customLabelValue = System.Label.EchoSignDataMapping;
echosign_dev1__SIGN_Data_Mapping__c objProcessTemplate = new echosign_dev1__SIGN_Data_Mapping__c();
objProcessTemplate =[Select id,Name from echosign_dev1__SIGN_Data_Mapping__c where Name=:customLabelValue limit 1];
agmt = new echosign_dev1__SIGN_Agreement__c();
if(objcon.ID!=null)
{
agmt.echosign_dev1__Recipient__c = objcon.ID;// Contact field
}
else
{
agmt.echosign_dev1__Recipient__c = '';// Contact field
}
agmt.Name = objcontract.ACCOUNT.Name +'- Contract';
agmt.echosign_dev1__Contract__c = contractid ;// ContractID;
agmt.echosign_dev1__Account__c = objcontract.AccountID;//ACCOUNT id
// agmt.echosign_dev1__Merge_Mapping__c =objMergeMapping.ID;//Echo Sign Merge Mapping
agmt.echosign_dev1__Process_Template__c =objProcessTemplate.ID;// Echo Sign Data Mapping
agmt.echosign_dev1__RemindRecipient__c = 'Every Day, Until Signed';
agmt.echosign_dev1__SenderSigns__c = true;
agmt.echosign_dev1__Message__c = 'Please sign the attached Insertion Order';
agmt.echosign_dev1__SignatureType__c = 'e-Signature';
agmt.echosign_dev1__PasswordProtectSign__c = false;
agmt.echosign_dev1__SenderSigns__c = true;
agmt.echosign_dev1__PasswordProtectPDF__c = false;
agmt.echosign_dev1__SignatureOrder__c = 'Recipient signs, then I sign';
agmt.echosign_dev1__Status__c = 'Draft';
insert agmt;
System.debug(agmt);
}
Class Name
public boolean err{get;set;}
public boolean isTest = false;
public echosign_dev1__SIGN_Agreement__c agmt ;
public clsGenerateAndSavePdf(){
err = false;
agmt = new echosign_dev1__SIGN_Agreement__c();
}
public pageReference generatePdf(){
//GenrateAgreementPdf();
String id = '';
if(ApexPages.currentPage().getParameters().get('id')!= null && ApexPages.currentPage().getParameters().get('id')!= ''){
id = ApexPages.currentPage().getParameters().get('id');
try{
Contract ob = [select id, Type__c from Contract where id = :id];
String str = '';
if(ob.Type__c == 'Restaurant Agreement SS'){
str = 'restaurantAgreementSS';
}else if(ob.Type__c == 'Restaurant Agreement AyCE'){
str = 'restaurantAgreementAyCE';
}else{
str = 'restaurantAgreement';
}
pagereference pdf = new pagereference('/apex/'+str+'?id='+id);
system.debug('!pdf'+pdf);
pdf.setRedirect(true);
Blob b ;
if(!isTest){
b = pdf.getContent();
}else{
b = blob.valueOf('test mode');
}
Attachment a = new Attachment(parentId = id, name=ob.Type__c+'.pdf', body = b);
insert a;
GenrateAgreementPdf();
Attachment Attach = a.clone();
Attach.parentId = agmt.id;
insert Attach;
return new pagereference('/'+agmt.id);
//return null;
}catch(Exception e){
system.debug('!Bingo '+e);
err = true;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error occur. Please try after some time.'));
}
}else{
err = true;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Url !'));
}
return null;
}
public void GenrateAgreementPdf()
{
String contractid = ApexPages.currentPage().getParameters().get('id');
Contract objcontract = new Contract();
objcontract = [Select id, AccountID,ACCOUNT.Name from Contract where Id =:contractid limit 1 ];
//To get Contact Id
Contact objcon = new Contact();
system.debug('objcontract.AccountID++++'+objcontract.AccountID);
if(objcontract.AccountID !=null){
list<Contact> lstcon=new list<Contact>();
lstcon= [Select ID, AccountId,Account.Name,Email from Contact where AccountID =:objcontract.AccountID and Email !='' order by LastModifiedDate desc limit 1];
if(lstcon.size()>0 && lstcon !=null)
{
objcon =lstcon[0];
}
}
// To Get Merge Mapping Id
/* echosign_dev1__SIGN_Merge_Mapping__c objMergeMapping = new echosign_dev1__SIGN_Merge_Mapping__c();
objMergeMapping =[Select id, Name from echosign_dev1__SIGN_Merge_Mapping__c where Name ='TestMap2_ppt' limit 1];*/
// To Get Merge Mapping Id
String customLabelValue = System.Label.EchoSignDataMapping;
echosign_dev1__SIGN_Data_Mapping__c objProcessTemplate = new echosign_dev1__SIGN_Data_Mapping__c();
objProcessTemplate =[Select id,Name from echosign_dev1__SIGN_Data_Mapping__c where Name=:customLabelValue limit 1];
agmt = new echosign_dev1__SIGN_Agreement__c();
if(objcon.ID!=null)
{
agmt.echosign_dev1__Recipient__c = objcon.ID;// Contact field
}
else
{
agmt.echosign_dev1__Recipient__c = '';// Contact field
}
agmt.Name = objcontract.ACCOUNT.Name +'- Contract';
agmt.echosign_dev1__Contract__c = contractid ;// ContractID;
agmt.echosign_dev1__Account__c = objcontract.AccountID;//ACCOUNT id
// agmt.echosign_dev1__Merge_Mapping__c =objMergeMapping.ID;//Echo Sign Merge Mapping
agmt.echosign_dev1__Process_Template__c =objProcessTemplate.ID;// Echo Sign Data Mapping
agmt.echosign_dev1__RemindRecipient__c = 'Every Day, Until Signed';
agmt.echosign_dev1__SenderSigns__c = true;
agmt.echosign_dev1__Message__c = 'Please sign the attached Insertion Order';
agmt.echosign_dev1__SignatureType__c = 'e-Signature';
agmt.echosign_dev1__PasswordProtectSign__c = false;
agmt.echosign_dev1__SenderSigns__c = true;
agmt.echosign_dev1__PasswordProtectPDF__c = false;
agmt.echosign_dev1__SignatureOrder__c = 'Recipient signs, then I sign';
agmt.echosign_dev1__Status__c = 'Draft';
insert agmt;
System.debug(agmt);
}
}
Page Name
<apex:page showHeader="false" sidebar="false" controller="clsGenerateAndSavePdf" action="{!generatePdf}"> <apex:outputPanel rendered="{!!err}"> <h1 style="font-size:20px;margin:20px">Processing....</h1> </apex:outputPanel> <apex:outputPanel rendered="{!err}"> <apex:pageMessages ></apex:pageMessages> </apex:outputPanel>
</apex:page>
Comments
Post a Comment