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
Salesforce Admin 110Salesforce Admin 110 

simple question what is wrong with this code -

this code is supposed to create a new opp when a new contact is created but no opp is created??!!

trigger newoppty on contact (after insert, after update) {

    list<opportunity> opplist = new list<opportunity>();
    
    for (contact con : trigger.new) {

        if(con.Stage__c == 'Lead') {
            opportunity newopp = new opportunity ();
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'package option and epc'; 
            newopp.account = con.account;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Prospecting';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.name;
            newopp.closedate = Date.today().addDays(7);
            
            


            opplist.add(newopp);

}
}


        try {
        insert opplist;
} catch (system.dmlexception e) {
system.debug (e);
}

    
}
SKolakanSKolakan
con.Stage__c == 'Lead'  --- Does the Contact you are creating have Stage__c value as 'Lead' ?

Did you enable debug logs? If you did, have couple of debug statements within your code to troubleshoot.

 
Mario PaesMario Paes
I see that you are not creating an Opportunity Contact Role. So, the opportunity might be getting created, but you don't see it on the contact since there is no OpportunityContactRole linking the opportunity to the contact. To check if the opportunity is being created, I would suggest checking the opportunities linked to the acouunt.

To link the opportunity to the contact:
Create a list of OpportunityContactRoles and insert these once the opportunities have been inserted. 
list<OpportunityContactRole> ocrlist = new list<OpportunityContactRole>();




 
Salesforce Admin 110Salesforce Admin 110
i'm relatively new to apex so i haven't debugged the code.....which i will try. @mario - how would i write into that trigger linking contact roles? 
Mario PaesMario Paes
Here's the code to insert the OpportunityContactRole. 

First create a field on the opportunity to save the contactId. Let call it contactId__c. This is only to generate the OCR later and doesn't serve any other purpose. You don't need to display it on the page layout

list<opportunity> opplist = new list<opportunity>();
    
    for (contact con : trigger.new) {

        if(con.Stage__c == 'Lead') {
            opportunity newopp = new opportunity ();
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'package option and epc'; 
            newopp.account = con.account;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Prospecting';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.name;
            newopp.closedate = Date.today().addDays(7);
            //Add the new field
            newopp.contactId__c = con.Id;
            
            


            opplist.add(newopp);

}
}


        try {
        Database.InsertResult[] OppResults = Database.Insert(oppList);
       
        //Build the corresponding OCR object
            ////Loop through the Opportunity results and get the records that have been
            //succesfully added. 
            for (Integer idx = 0; idx < oppResultsSize; idx++) {

                if (oppResults[idx].isSuccess() && oppResults[idx].isCreated()) {
                    //Get the corresponding opp record
                    Opportunity addedOpp = upsertOpportunity[idx];

                    Id contactId = addedOpp.contactId__c ;
                    system.debug('contactId in added opp : ' + contactId);
                                        
                    if (ContactId == null) {
                        system.debug('Contact not found in insertOCR. clientId : ' + ContactId);
                    }

                    //Combine the Ids to get the OCR Record
                    ocr = new OpportunityContactRole();
                    ocr.ContactId = contactId;
                    ocr.OpportunityId = oppResults[idx].getId();
                    ocr.IsPrimary = true;
                    ocr.Role = 'Decision Maker';
                    insertOCR.add(ocr);
                }
            }

            //Add the opportunity contact roles
            if (insertOCR.size() > 0) {
                system.debug('before insertOCR');

                Database.SaveResult[] OcrResults = Database.insert(insertOCR);
                system.debug('OcrResults.size : ' + OcrResults.size());
            }

} catch (system.dmlexception e) {
system.debug (e);
}

    
}
 
Mario PaesMario Paes
noticed a typo .. Replace
Opportunity addedOpp = upsertOpportunity[idx];
 with 
Opportunity addedOpp = oppList[idx];