You need to sign in to do that
Don't have an account?
Shannon Andreas 1
Need help with trigger that creates a new contract
So...I created my almost very first trigger...it's not firing. Please help. I pieced together code from different sources and thought I was on the right track...hmmmm
Trying to create a contract when the checkbox "Ready for Contract" is checked. Do I need the opportunity ID as well? Not sure why this is not working. Thanks in advance for help. Shannon
trigger CreateContract on Opportunity (before insert) {
private List<Contract> ctr = new List<Contract>();
for(Opportunity o : Trigger.new) {
if(o.Ready_for_Contract__c = true) {
Contract c = new Contract(Id = o.Account.Id,
Name = o.Name,
Status = 'Activated',
Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
StartDate = o.Contract_Start_Date__c,
Payment_Status__c = 'Ready to Be Invoiced');
ctr.add(c);
}
insert ctr;
}
}
Trying to create a contract when the checkbox "Ready for Contract" is checked. Do I need the opportunity ID as well? Not sure why this is not working. Thanks in advance for help. Shannon
trigger CreateContract on Opportunity (before insert) {
private List<Contract> ctr = new List<Contract>();
for(Opportunity o : Trigger.new) {
if(o.Ready_for_Contract__c = true) {
Contract c = new Contract(Id = o.Account.Id,
Name = o.Name,
Status = 'Activated',
Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
StartDate = o.Contract_Start_Date__c,
Payment_Status__c = 'Ready to Be Invoiced');
ctr.add(c);
}
insert ctr;
}
}
You probably want to set the accountID.
Try code below (not sure, but you might have other errrors, we'll see :-)
There are few mistakes in your code.
1. In If condition use "==" instead of "=".
2. DML statement (insert ctr) written inside for loop.
3. trigger should be on "after insert" event.
Try to use following code.
Apex Trigger:
Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Which issue is coming now? can you please post the error message here?
Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Contract c = new Contract(Id = o.Account.Id
trigger AndreiTest on Opportunity (before insert) {
private List<Contract> ctr = new List<Contract>();
for(Opportunity o : Trigger.new) {
if(o.Ready_for_Contract__c = true) {
Contract c = new Contract(Name = o.Name,
Status = 'Draft',
Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
StartDate = o.Contract_Start_Date__c,
Payment_Status__c = 'Ready to Be Invoiced'
,AccountId = o.AccountId);
ctr.add(c);
}
insert ctr;
}
}
it looked like you pretty much went in full circle, your final code looked like what I proposed :-)
Thx
Yours:
Mine:
If you are inserting, you do not need to set the id.
You probably want to set the accountID.
Thx
Try the following code, It will be helpful to you.
trigger CreateContract on Opportunity (before update)
{
List<Contract> ctr = new List<Contract>();
for(Opportunity o : Trigger.new)
{
if(o.Ready_for_Contract__c == true)
{
Contract c = new Contract(Name = o.Name,
Status = 'Draft',
Total_Contract_Value__c = o.Amount,
StartDate = o.Contract_Start_Date__c,
Payment_Status__c = 'Ready to be Invoiced',
AccountId = o.AccountId,
Opportunity_Name__c = o.Id);
ctr.add(c);
}
}
if(ctr.size() > 0)
{
System.debug('-ctr------->'+ctr.size());
insert ctr;
}
}
Now I have other issues! I would be greatful for your help with this post: