-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
4Replies
Auto generate Quote pdf file using Javascript button
I'm trying to create a function that will allow the user to select multiple opportunities in the opportunity list view, and then click a button to generate all the quote s and PDF files. The button looks liks this
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
var idsToInsert= {!GETRECORDIDS( $ObjectType.Opportunity )};
if (idsToInsert.length) {
// Now make a synchronous call to the Apex Web service
// method
var result = sforce.apex.execute(
"MassQuoteInsert", // class
"insertNotes", // method
{iParentIds : idsToInsert // method arguments
}
);
{
window.alert(idsToInsert.length+" quote inserted.");
}
} else if (idsToInsert.length == 0) {
alert("Please pick the opportunities");
}
The Apex class is pasted below. The line "Blob pdfBlob = pr.getContent();" is causing a problem. I get an error message like this. Can some one be so kind and take a look for me? Thank you in advance!
Error message
global class MassQuoteInsert{
WebService static Integer insertNotes(Id[] iParentIds)
{
string TemplateId = '0EHU0000000LPpL';
List<Quote> qts = new List<Quote>();
List<QuoteLineitem> qtlines = new List<QuoteLineitem>();
List <Opportunity> opts = [SELECT id, AccountId, Name, Type, CloseDate, SyncedQuoteId, Renewal_Due_Date__c, Pricebook2Id
from Opportunity where Id IN :iParentIds ];
//Get all the contact roles
List <OpportunityContactRole> OptContacts = [SELECT OpportunityId, Id, ContactId, IsPrimary from OpportunityContactRole where OpportunityId in : iParentIds];
//Sort contact roles according to opportunity id
Map<Id, List<OpportunityContactRole>> OptContactMap= new Map<Id, List<OpportunityContactRole>>();
for(OpportunityContactRole OptContact : OptContacts)
{
if(OptContactMap.get(OptContact.OpportunityId)!=NULL)
OptContactMap.get(OptContact.OpportunityId).add(OptContact);
else
{
List<OpportunityContactRole> TempContactRole = new List<OpportunityContactRole>();
TempContactRole.add(OptContact);
OptContactMap.put(OptContact.OpportunityId, TempContactRole);
}
}
//Get all the opportunity line item
List <OpportunityLineitem> OptProducts = [SELECT OpportunityId, PricebookEntryId, Quantity, UnitPrice, PricebookEntry.Product2Id, Subtotal,TotalPrice from OpportunityLineitem where OpportunityId IN : iParentIds];
Map<Id, List<OpportunityLineitem>> OptProductMap= new Map<Id, List<OpportunityLineitem>>();
for(OpportunityLineitem OptProduct : OptProducts)
{
if(OptProductMap.get(OptProduct.OpportunityId)!=NULL)
OptProductMap.get(OptProduct.OpportunityId).add(OptProduct);
else
{
List<OpportunityLineitem> TempProduct = new List<OpportunityLineitem>();
TempProduct.add(OptProduct);
OptProductMap.put(OptProduct.OpportunityId, TempProduct);
}
}
//Finish all the preparation
//Creating quotes
for (Opportunity opt : opts){
Quote TempQuote = new quote( NAME = opt.Name, OpportunityId = opt.id, Valid_for_days__c = 15, Valid_For__c = '15',
Status = 'Auto Generated',
Rep_Name__c = UserInfo.getUserId(),
Quote_Type__c = 'New Revenue',
Pricebook2Id = opt.Pricebook2Id
//,Description = string.valueof(opt.Renewal_Due_Date__c)
);
//Renewal Quote type
If(opt.type == 'Renewal')
TempQuote.Quote_Type__c = 'Renewal';
//Renewal Due Date customization
Date maintdate;
if(opt.Renewal_Due_Date__c != NULL)
maintdate = opt.Renewal_Due_Date__c;
else
maintdate = opt.CloseDate;
TempQuote.Description = 'Maintenance: '+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Year())+' to '+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.adddays(365).Year());
// TempQuote.Description_dd_mm_yy__c = 'Maintenance: '+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Year())+' to '+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.adddays(365).Year());
//Find out who's the contact
List<OpportunityContactRole> QuoteContact = OptContactMap.get(opt.id);
if(QuoteContact!=NULL)
{
integer i =0;
While(TempQuote.Bill_to__c == NULL && i < QuoteContact.size())
{
if(QuoteContact[i].IsPrimary == true)
TempQuote.Bill_to__c = QuoteContact[i].ContactId;
i++;
}
i=0;
While(TempQuote.Bill_to__c == NULL && i < QuoteContact.size())
TempQuote.Bill_to__c = QuoteContact[i].ContactId;
if(TempQuote.Bill_to__c!=NULL)
TempQuote.Ship_to__c = TempQuote.Bill_to__c;
}
qts.add(TempQuote);
}
insert qts;
List<QuoteDocument> sr = new List<QuoteDocument>();
for (Quote q: qts)
{
PageReference pr = new PageReference('/quote/quoteTemplateDataViewer.apexp?id='+q.id+'&'+'summlid='+TemplateId); // link from answer by techtrekker
Blob pdfBlob = pr.getContent();
sr.add (new QuoteDocument(
QuoteId = q.Id,
document = pdfBlob));
}
insert sr;
for (Opportunity opt : opts){
for(Quote qt:qts)
{
if(qt.OpportunityId == opt.id)
{
opt.SyncedQuoteId = qt.id;
List<OpportunityLineItem> QuoteProduct = OptProductMap.get(opt.id);
if(QuoteProduct!=NULL)
{
for(OpportunityLineitem optline:Quoteproduct)
{
Quotelineitem templineitem = new QuoteLineitem(
QuoteId = qt.id,
UnitPrice = optline.UnitPrice,
Quantity = optline.Quantity,
PricebookEntryId = optline.PricebookEntryId,
Product2Id = optline.PricebookEntry.Product2Id
);
qtlines.add(templineitem);
}
// PageReference pr = new PageReference('/quote/quoteTemplateDataViewer.apexp?id='+qt.id+'&'+'summlid='+TemplateId); // link from answer by techtrekker
// Blob pdfBlob = pr.getContent();
// Attachment a = new Attachment(parentId = qt.id, name=qt.name + '.pdf', body = pdfBlob);
// insert a;
}
}
}
}
insert qtlines;
update opts;
- Alice Ju
- March 18, 2014
- Like
- 0
- Continue reading or reply
Need help in increasing test coverage
I'm really new to apex and visual force page, and I'm trying to meet the 75% test coverage bar to deploy my VS page. I'm stuck in how to increase the test class now. Take the trigger below for example, the two lines that I mark as bold is marked as "not covered" in the developers console. And these two lines won't fire off any trigger, so I really don't know how to write a test class for code like this. Can any one be so kind and give me some advise? Thank you!
List<Opportunity> AllClosedOp= [SELECT AccountId, Id,Invoiced_Date__c, Renewal_Due_Date__c, CloseDate
from Opportunity where AccountId in :accountIds and (StageName='Closed Won' OR StageName='Closed Won Waiting for invoice')];
List<Contact> AllContact=[Select Id, AccountId, Contact_Role__c From Contact where AccountId IN :accountIds and Contact_Role__c!=Null];
For(Opportunity opt:AllClosedOp)
{
ClosedWonOps.get(opt.AccountId).add(opt);
}
For(Contact ct:AllContact)
{
AccountContacts.get(ct.AccountId).add(ct);
}
- Alice Ju
- January 10, 2014
- Like
- 0
- Continue reading or reply
Test.startTest() Variable test is used before it is declared
I'm trying to add Test.StartTest() in my test class, but I kept getting this error message.
"Error: Compile Error: Variable test is used before it is declared. at line 4 column 46"
I then go back and check if I have any class called "test" that override the test method, but I can't find any. Do you guys happen to know where the error comes from? If you can kindly give me some advice I'll very much apprecaite. Thank you!
- Alice Ju
- January 09, 2014
- Like
- 0
- Continue reading or reply
Selecting records where a datetime value meets criteria
Hi there,
I have a querry in my apex trigger that looks like this:
List<DatedConversionRate> ExchRateCache = [SELECT ISOCode, ConversionRate, StartDate, NextStartDate FROM DatedConversionRate where ISOCode in :CurrCodes ];
This works fine, but it pulls the currency exchange rate of all time, and I only want the exchange rate on the 1st day of the month. I then changed it as below.
List<DatedConversionRate> ExchRateCache = [SELECT ISOCode, ConversionRate, StartDate, NextStartDate FROM DatedConversionRate where ISOCode in :CurrCodes AND ExchRateCache.StartDate.Day()='1'];
But I got an error saying "unexpected token: ')' at line 70 column 192"
Can some one be so kind and give me some instruction? Very much appreciate!
Alice
- Alice Ju
- November 22, 2013
- Like
- 0
- Continue reading or reply
Test.startTest() Variable test is used before it is declared
I'm trying to add Test.StartTest() in my test class, but I kept getting this error message.
"Error: Compile Error: Variable test is used before it is declared. at line 4 column 46"
I then go back and check if I have any class called "test" that override the test method, but I can't find any. Do you guys happen to know where the error comes from? If you can kindly give me some advice I'll very much apprecaite. Thank you!
- Alice Ju
- January 09, 2014
- Like
- 0
- Continue reading or reply
Selecting records where a datetime value meets criteria
Hi there,
I have a querry in my apex trigger that looks like this:
List<DatedConversionRate> ExchRateCache = [SELECT ISOCode, ConversionRate, StartDate, NextStartDate FROM DatedConversionRate where ISOCode in :CurrCodes ];
This works fine, but it pulls the currency exchange rate of all time, and I only want the exchange rate on the 1st day of the month. I then changed it as below.
List<DatedConversionRate> ExchRateCache = [SELECT ISOCode, ConversionRate, StartDate, NextStartDate FROM DatedConversionRate where ISOCode in :CurrCodes AND ExchRateCache.StartDate.Day()='1'];
But I got an error saying "unexpected token: ')' at line 70 column 192"
Can some one be so kind and give me some instruction? Very much appreciate!
Alice
- Alice Ju
- November 22, 2013
- Like
- 0
- Continue reading or reply