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
budilsterbudilster 

Copy Oppurtunity Line Items to Quote

I'm trying to use apex to build a quote.  I'm able to add OpportunityLineItems but need these to show up in a Quote as line item.  I add the OpportunityLineItems from Apex using the code below, but I can only get these line item to the quote by logging into the website, viewing the quote there, and clicking the "Copy Opportunity Lines into Quote" link.  Then, my line items are added to the quote.  I need a way to do this from apex but can't figure out how.  Here is the code I am using:

 

 private void createQuoteSample()

{

//Verify that we are already authenticated, if not

//call the login function to do so

 if (!loggedIn)

{

if (!login()) return;

}

try

{

apex.sObject quote; sObject[] quots = new sObject[1];

{

quote = new apex.sObject();System.Xml.

XmlElement[] qts = new System.Xml.XmlElement[5];

int index = 0;qts[index++] = GetNewXmlElement(

"OpportunityId", "0063000000Z1lV2AAJ");

qts[index++] = GetNewXmlElement("PricebookEntryId", "01u30000002d5kAAAQ");qts[index++] = GetNewXmlElement(

"Quantity", "3");

qts[index++] = GetNewXmlElement("Description", "Bad to the bone");qts[index++] = GetNewXmlElement(

"UnitPrice", "12");quote.type = "OpportunityLineItem";

quote.Any = qts;

quots[0] = quote;

}

//create the object(s) by sending the array to the web service

 SaveResult[] sr = binding.create(quots); for (int j = 0; j < sr.Length; j++)

{

if (sr[j].success)

{

Console.Write(System.Environment.NewLine + "A opportunity line item was created with an id of: "

+ sr[j].id);

}

else

{

//there were errors during the create call, go through the errors

//array and write them to the screen

 for (int i = 0; i < sr[j].errors.Length; i++)

{

//get the next error

 Error err = sr[j].errors[i];

Console.WriteLine("Errors were found on item " + j.ToString());

Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message);

}

}

}

{

quote = new apex.sObject();

System.Xml.XmlElement[] qts = new System.Xml.XmlElement[1];

int index = 0;

qts[index++] = GetNewXmlElement("SyncedQuoteID", "a0h30000002awV5AA1");quote.type =

"Opportunity"; quote.Id = "0063000000Z1lV2AAJ";

// quote.Any = qts;

quots[0] = quote;

}

sr = binding.update(quots);

for (int j = 0; j < sr.Length; j++)

{

if (sr[j].success)

{

Console.Write(System.Environment.NewLine + "A opportunity was updated with an id of: "

+ sr[j].id);

}

else

{

//there were errors during the create call, go through the errors

//array and write them to the screen

 for (int i = 0; i < sr[j].errors.Length; i++)

{

//get the next error

 Error err = sr[j].errors[i];

Console.WriteLine("Errors were found on item " + j.ToString());

Console.WriteLine("Error code is: " + err.statusCode.ToString()); Console.WriteLine("Error message: " + err.message);

}

}

}

Console.WriteLine("\nHit return to continue..."); Console.ReadLine();

}

catch (Exception ex)

{

Console.WriteLine("\nFailed to update quote, error message was: \n"

+ ex.Message);

Console.Write("\nHit return to continue..."); Console.ReadLine();

}

}

Will MoxleyWill Moxley
Have you looked at the new standard quote object where this logic exists standard with the product?