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
Beth WalterBeth Walter 

Creating a Visual Force page, to create a custom button

I was sent here by someone on the Salesforce support team. She informed me that I need to create a visualforce page, so that I can create a custom button. I want to create a button or action that gives me the option to create a quote right after creating (saving) an opportunity. Do I need to create a Visual Force page, then a custom button or do I need to do something with the Opportunity object workflow? I need help creating these things, either way. 
NagendraNagendra (Salesforce Developers) 
Hi Walter,

Sorry for this issue you are facing.

You can use apex trigger to create a new quote when an opportunity is created.

Please find the trigger below which creates a new quote when a new opportunity is created.
trigger QuoteCreate on Opportunity (after insert) {
    
        List<Id> oppIdsList = new List<Id>();
        List<Quote> quoteList = new List<Quote>();
         
         for (Opportunity o : Trigger.new) {
        if(o.FME_AMC_Renewal__c == True)
        
        oppIdsList.add(o.id);
}
        Opportunity[] oppList = [SELECT name, id, account.billingStreet, account.billingCity,
                                    account.billingState, account.billingCountry
                                FROM Opportunity
                                WHERE id IN :oppIdsList];
        for (Opportunity o : oppList) {
        Quote q = new Quote();
        q.name = o.name;
        q.opportunityId = o.id;
        q.billingStreet = o.account.billingStreet;
        q.billingCity = o.account.billingCity;
        q.billingState = o.account.billingState;
        q.billingCountry = o.account.billingCountry;
    quoteList.add(q);
    }
    insert quoteList;
}
Hope this helps.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are facing a similar issue.

Thanks,
Nagendra
 
Beth WalterBeth Walter
I appreciate your response, but when I put this in a visualforce page it came up with the following errors.  

Error: Opportunity_Quote line 1, column 1: Content is not allowed in prolog
Error: Content is not allowed in prolog.

I am new at this, so I am not sure how to fix it. 

trigger QuoteCreate on Opportunity (after insert) {
    
        List<Id> oppIdsList = new List<Id>();
        List<Quote> quoteList = new List<Quote>();
         
         for (Opportunity o : Trigger.new) {
        if(o.FME_AMC_Renewal__c == True)
        
        oppIdsList.add(o.id);
}
        Opportunity[] oppList = [SELECT name, id, account.billingStreet, account.billingCity,
                                    account.billingState, account.billingCountry
                                FROM Opportunity
                                WHERE id IN :oppIdsList];
        for (Opportunity o : oppList) {
        Quote q = new Quote();
        q.name = o.name;
        q.opportunityId = o.id;
        q.billingStreet = o.account.billingStreet;
        q.billingCity = o.account.billingCity;
        q.billingState = o.account.billingState;
        q.billingCountry = o.account.billingCountry;
    quoteList.add(q);
    }
    insert quoteList;
}