1

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)
     {
         if(cont.size()==0)
         if(con.Account_Status__c=='Qualified')
         {
             Opportunities__c op = new Opportunities__c(Name=con.LastName,Contact__c=con.id,
             Account_Source__c=con.Lead_Source__c,
             Property_Type__c=con.Property_Type__c,
             Location__c=con.Location__c,
             Property_Status__c=con.Property_Status__c,
             Bedroom__c=con.Bedroom__c,
             If_Under_Construction__c=con.If_UC__c,
             Facing__c=con.Facing__c,
             Min_Bydget__c=con.Min_Budget__c,
             Max_Budget__c=con.Max_Budget__c,
             Min_Sqft__c=con.Min_Sqft__c,
             Purpose_of_Purchase__c = con.Purpose_of_Purchase__c,
             Investment_Range__c=con.Investment_Range__c,
             City_of_Interest__c = con.City_of_Interest__c,
             Location1__c = con.Prefered_Location__c,
             Project__c = con.Project__c,
             Max_Sqft__c=con.Max_Sqft__c);

             oplist.add(op);
         }
    }

     try
     {
         //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
2

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:page>

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.