You need to sign in to do that
Don't have an account?
Ingrid 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 ;
}
}
- 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 ;
}
}
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.