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
Ingrid Stone 1Ingrid Stone 1 

Creating opportunity automatically on load of contact

I want to create opportuites automatically when I load new contacts.   I have wrtten a trigger but which sort of work am struggling with a couple of things:
- opportunity name should be opp-'contact name'  - this is not working
- the opportunity is not visible on the contact (from the opportunity you can get to contact).
- I have an additional field in opportunity that is a look up to user (not always the sane one loading data) this does not work.

This is my code - this is the first time I have written apex code :)

trigger CreateOpportunityFromContentContact on Contact (after insert) {
      List<Opportunity> oppsToUpdate = new List<Opportunity>();
      // If record_type = content_Contact 
      ID contactrecordid =[select Id from RecordType where Name = 'Content contacts' and SobjectType = 'Contact' limit 1].ID;
    ID opprecordid = [select Id from RecordType where Name = 'Content opportunity' and SobjectType = 'Opportunity' limit 1].ID; 
    
    for(Contact cc : Trigger.new) {

    // here is where you check if opportunity that is being inserted meets the criteria
          
   if (cc.RecordTypeid ==contactrecordid) {

    Opportunity o = new Opportunity (); 
    //set the object to put values for future record
// get account id  - this is not working 
  //    ID accrecordid = [select Accountid from account where name = :cc.id limit 1];        
       
    // now map opportunity fields to new object that is being created with this opportunity
    
list<contact> contact_name = [select name from contact where ID = :cc.id];
string oppname = cc.name;
  o.Name = 'Content Opp -' + cc.name;     
  o.recordtypeid = opprecordid;
  o.Content_Contact__c = cc.id ;
 // o.accountid=accrecordid;     
  o.Account = [select id from Account where name = 'xx'];
  //user opp_solicitor = [select ID from User where Name = :cc.Content_opp_Solictior__c];
  o.Content_Solicitor__c = cc.Content_opp_Solictior__c;
  
  list<ITCS_product__c> content_opp = [select ID from ITCS_Product__c where MySQL_Product_ID_Number__c = :cc.Content_opp_Product_ID__c];
    If (content_opp.size ( ) > 0) 
    { o.Content_ITCS_product__c = content_opp[0].id;
    } else o.Content_ITCS_product__c = null;
  o.StageName = 'In Progress'; 
  // close date to be a year from today
  o.CloseDate = Date.today() + 365;   
    

    oppsToUpdate.add(o);

 //   }//end if

}//end for o

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.

    insert oppsToUpdate ;

    }
    }
Nithesh NNithesh N
Try This...
 
trigger CreateOpportunityFromContentContact on Contact (before insert) {
    List<Opportunity> oppsToCreate = new List<Opportunity>();
    ID contactrecordid =[select Id from RecordType where Name = 'Content contacts' and SobjectType = 'Contact' limit 1].ID;
    ID opprecordid = [select Id from RecordType where Name = 'Content opportunity' and SobjectType = 'Opportunity' limit 1].ID; 
    
    for(Contact c : [SELECT Id, Name, AccountId FROM Contact Where RecordTypeid = :contactrecordid AND Id IN :Trigger.New ]) {
       Opportunity o = new Opportunity (); 
       o.Name = 'opp - ' + c.Name;
       o.RecordTypeid = opprecordid;
       o.StageName = 'In Progress'; 
       o.CloseDate = Date.today() + 365; 
       o.AccountId = c.AccountId;
       o.Content_Contact__c = c.id ;
       o.Content_Solicitor__c = c.Content_opp_Solictior__c;
       oppsToCreate.add(o);
    }
    
      insert oppsToCreate;
}

This Should work, But I haven't  added  ITCS_product__c details to the Opp as I am still not sure of fields and properties of that object.

Best, 
Nithesh.
Ingrid Stone 1Ingrid Stone 1
thanks - i tries it but it do not create an opportunity
Nithesh NNithesh N
Are you getting any error??