+ Start a Discussion
Zain ButtZain Butt 

Custom Controller to add quote lines to Opportunity Product

I'm tasked with creating a custom controller that will add quote lines to Opportunity Product. How would I go about creating this controller to add the quotes? Sample code would be awesome!
Best Answer chosen by Zain Butt
Vivian Charlie 1208Vivian Charlie 1208

Hi Zain,

 

  • Create a custom button that uses a controller method declared as webservice
  • Query and fetch the QuoteLineItems (QLI) for this Quote
  • Iterate over the list of QLI and create an instance of OpportunityLineItem (OLI) for every QLI
  • Insert the list of OLI related to the Quote.OpportunityId value
global class buttonHandler{ 
   	Webservice static void copyQLItoOLI(String strRecId){
		list<OpportunityLineItem> lstOLI = new list<OpportunityLineItem>();
		List<QuoteLineItem> lstQLI = [Select ...., Quote.OpportunityId from QuoteLineItem where QuoteId =: strRecId];
		
		for(QuoteLineItem objQ : lstQLI){
			OpportunityLineItem objOLI = new OpportunityLineItem();
			// mapp all fields
			objOLI.OpportunityId = objQ.Quote.OpportunityId;
			lstOLI.add(objOLI);
		}
		if(!lstOLI.isEmpty(){
			insert lstOLI;
		}
	}
}

 

Custom button code

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
sforce.apex.execute("buttonHandler","copyQLItoOLI",{id:"{!Quote.Id}"}); 
location.reload();


Thanks

Vivian

All Answers

Vivian Charlie 1208Vivian Charlie 1208

Hi Zain,

 

  • Create a custom button that uses a controller method declared as webservice
  • Query and fetch the QuoteLineItems (QLI) for this Quote
  • Iterate over the list of QLI and create an instance of OpportunityLineItem (OLI) for every QLI
  • Insert the list of OLI related to the Quote.OpportunityId value
global class buttonHandler{ 
   	Webservice static void copyQLItoOLI(String strRecId){
		list<OpportunityLineItem> lstOLI = new list<OpportunityLineItem>();
		List<QuoteLineItem> lstQLI = [Select ...., Quote.OpportunityId from QuoteLineItem where QuoteId =: strRecId];
		
		for(QuoteLineItem objQ : lstQLI){
			OpportunityLineItem objOLI = new OpportunityLineItem();
			// mapp all fields
			objOLI.OpportunityId = objQ.Quote.OpportunityId;
			lstOLI.add(objOLI);
		}
		if(!lstOLI.isEmpty(){
			insert lstOLI;
		}
	}
}

 

Custom button code

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
sforce.apex.execute("buttonHandler","copyQLItoOLI",{id:"{!Quote.Id}"}); 
location.reload();


Thanks

Vivian

This was selected as the best answer
Zain ButtZain Butt
Thanks a ton!