I have a trigger to serve this purpose but i'm not able to call trigger using custom button

and how to apex class using custom button

trigger AutoCreateOpportunity on Contact (after insert,after update) {

 list<Opportunities__c> oplist= new list<Opportunities__c>();

 list<Opportunities__c> cont=[select id from Opportunities__c where Contact__c=:trigger.newmap.keyset()];

     for(contact con : trigger.new)
             Opportunities__c op = new Opportunities__c(Name=con.LastName,Contact__c=con.id,
             Purpose_of_Purchase__c = con.Purpose_of_Purchase__c,
             City_of_Interest__c = con.City_of_Interest__c,
             Location1__c = con.Prefered_Location__c,
             Project__c = con.Project__c,


         //insert oplist;
         upsert oplist;
        //update oplist;
         catch(DmlException e)
             system.debug('Not Inserted');

  • Do you want to convert the lead and create opp, contact and account? – Prady Dec 25 '13 at 5:26
  • 1
    Are you using a VF page? – Prady Dec 25 '13 at 7:53
  • no i'm not using vf page – Sharat Dec 25 '13 at 13:55
  • Custom buttons can't directly invoke a trigger - they have to cause a Contact record to be updated - that in turn causes the trigger to execute. Thus, your choices are to (1) associate the page+button to a VF controller that does the Contact update, (2) use a button+SFDC AJAX toolkit+javascript to update a Contact record, (3) Write an APEX web service that you invoke from a button+javascript (see salesforcesource.blogspot.com/2009/06/…) – cropredy Dec 27 '13 at 2:16

I'm not entirely sure what you are attempting to do in your code, but what you are implying (creating an Opportunity upon click) cannot be done with a trigger. Triggers can only be invoked via a DML operation (insert, update, delete, undelete).

Instead, what you want to do is create a custom Visualforce Page and APEX Class. If you set up a StandardController for Contact in your APEX Class (assuming this button is on the Contact page), you can redirect to the Visualforce Page, invoke an action on the page load, create the Opportunity, and then redirect back to the original Contact page or the new Opportunity page.

At a high level:

Visualforce Page

<apex:page standardController="Contact" extensions="ContactExtension" action="{!createOpportunity}" />
  <!-- Show nothing -->

Apex Class

public with sharing ContactExtension
  public String contactId;

  public ContactExtension(ApexPages.StandardController std)
    contactId = std.getId();

  public PageReference createOpportunity()
    Opportunity oppty = new Opportunity();
    oppty.Contact__c = contactId;
    // ... set all the other fields ...
    insert oppty;

    // Redirect to new Opportunity
    return new PageReference('/' + oppty.Id);

Once you have this set up, you can create the custom Button that references this Visualforce page.

  • Great answer! +1 from me! Why has this not had any upvotes or selected as the correct one?!?! :-( – Andrew Fawcett Jul 14 '14 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.