You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
Did you enable debug logs? If you did, have couple of debug statements within your code to troubleshoot.
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>();
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);
}
}
Opportunity addedOpp = upsertOpportunity[idx];
with
Opportunity addedOpp = oppList[idx];