ShowAll Questionssorted byDate Posted
Rizwan Ali 12

# Trigger: On Oppotunity to create Job object Records

Hi,
Criteria:
On create Opportunity record , the Job Object records will create with according to Opportunity 'Quntity' Field ex: if quntity is 5 then create 5 job records and opportunity 'Amount' set in Job Object divided by 5 ex: If quntity is 5 and Amount is 1000 , set 200 to each Job records
I did this but didn't work
```If(Trigger.IsAfter && Trigger.isInsert)
{
for(Opportunity opp : Trigger.New)
{
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;

List<job__c> listofjob = new List<job__c>();

for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++)
{
job__c ajob = new job__c(Name = 'Job'+i, Amount__c =Amount );
}

if(listofjob.size()>0)
{
insert listofjob;
}
}
}```

Best Answer chosen by Rizwan Ali 12
Sunil Rathore
Hi Rizwan,

Please refer the below code. The trigger will execute only when the new Opportunity will create. It won't execute on the update of the opportunity.
```If(Trigger.IsAfter && Trigger.isInsert){
List<job__c> listofjob = new List<job__c>();
for(Opportunity opp : Trigger.New){
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c =  opp.id);
}
}
if(listofjob.size()>0){
insert listofjob;
}
}```

Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore

Sunil Rathore
Hi Rizwan,

Your code looks perfect. You just need to avoid performing DML operation inside the for loop. I have removed the Insert Operation from the for loop.

While insertion of the Job record make sure that value should be assigned to all the required field of the Job and hoping that your trigger is Active.

Refer to the below-updated code.
```If(Trigger.IsAfter && Trigger.isInsert){
List<job__c> listofjob = new List<job__c>();
for(Opportunity opp : Trigger.New){
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount );
}
}
if(listofjob.size()>0){
insert listofjob;
}
}```

Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
Rizwan Ali 12
Please can you tell me the reason why we put it outside
Rizwan Ali 12
I tryed but it didn't work.
I created master detailed field related to Opportunity in Job Object.
Trigger is also active.
and call all requried field.
```If(Trigger.IsAfter && Trigger.isInsert)
{
List<job__c> listofjob = new List<job__c>();
for(Opportunity opp : Trigger.New)
{
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
String Opportunity;
for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++)
{
job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c = Opportunity );
}

if(listofjob.size()>0)
{
insert listofjob;
}
}
}```
Sunil Rathore
Hi Rizwan,

There is a governor limit for DML statements. To avoid hitting the governor limit it is a best practice to avoid writing DML statements inside the for loop. It won't affect the signal record but when you perform an operation on bulk record it will throw an error.

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

Many Thanks,
Sunil Rathore
Sunil Rathore
Hi Rizwan,

Please refer the below code. The trigger will execute only when the new Opportunity will create. It won't execute on the update of the opportunity.
```If(Trigger.IsAfter && Trigger.isInsert){
List<job__c> listofjob = new List<job__c>();
for(Opportunity opp : Trigger.New){
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c =  opp.id);
}
}
if(listofjob.size()>0){
insert listofjob;
}
}```

Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
This was selected as the best answer
Rizwan Ali 12
Yes i create new OPP and
```If(Trigger.IsAfter && Trigger.isInsert)
{
List<job__c> listofjob = new List<job__c>();
for(Opportunity opp : Trigger.New){
Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c = opp.Id );