0

I have a custom object named Order, its fields same with Opportunity and I set lookup between them, what I need is when I create an Order record, a related Opportunity record is also be created automatically.

I have this trigger, gives no error but doesnt working. I think I need ID reference between my custom object(Order) and Opportunity but I don't know how to do it.

trigger CreateaOpportunity on Order__c(after insert, after update){

        List<Opportunity> OpportunityList=new List<Opportunity>();

        for(Order__c OrderObj : Trigger.new){
             Opportunity Opp = new Opportunity(
                  Name=OrderObj.Order_Name__c+'-Opportunity',
                  CloseDate=OrderObj.Close_Date__c,
                  StageName=OrderObj.StageName__c
             );
             OpportunityList.add(Opp);
        }

        if(OpportunityList.size()>0)
             insert OpportunityList;
}
1

You mention that you have a lookup between the objects, assuming you have a lookup to the order object on the opportunity, you just need to reference the order object in your creation of the new opportunity

Opportunity Opp = new Opportunity(
      Name=OrderObj.Order_Name__c+'-Opportunity',
      CloseDate=OrderObj.Close_Date__c,
      StageName=OrderObj.StageName__c,
      Order__c = OrderObj.Id
);  
|improve this answer|||||
1

If you don't already know, in the Spring 14 release for Salesforce they are releasing a new standard object called Order.

http://buttonclickadmin.com/getting-ready-salesforce-spring-14-release/

To link two objects together, you could use a Master-Detail field that relates the two records together. On your custom object, create a custom field (Master-Detail) and relate it back to the Opportunity. Once that's in place, when you create a Order record, insert the id of the Opportunity that you want to link it to and you're all set.

|improve this answer|||||
  • Yes but Order object here should be custom object and I could achieve it with a Lookup Relationship. Thanks for your answer. – user5204 Jan 10 '14 at 10:53

Your Answer

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