Reply
Regular Contributor
street9
Posts: 98
0

Trigger to make it bulkify. to check the count of relatedlist

How can i remove query from For loop and check this count

 

trigger Opportunity_With_onecontract on Contract__c (after insert)
 {  
    public integer count; 
    Set<ID> CID=new Set<ID>();
    for(Contract__c c:trigger.new)
    {
          CID.add(c.Opportunity__c );
    }
    opportunity []Oppids=[select id,AccountId from opportunity where id in:CID];
    for(opportunity  o:Oppids)
    {
    count=[select count() from Contract__c where Opportunity__c=:o.id ];

    I want to remove this query from for loop. 
    if(count >1)
    {
    system.debug('ccccccccccccccccccccccccc'+count);
     trigger.new[0].Opportunity__c.addError('Cannot insert Contract. As there is already a Contract to this Opportunity');
    }
    }
   }

Regular Contributor
MoUsman
Posts: 74
0

Re: Trigger to make it bulkify. to check the count of relatedlist

You can use the parent child relationship query as give below try somthing like this it would be bulkified.

trigger Opportunity_With_onecontract on Contract__c (after insert)
 {  
    public integer count; 
    Set<ID> CID=new Set<ID>();
    for(Contract__c c:trigger.new)
    {
          CID.add(c.Opportunity__c );
    }
	
    opportunity []Oppids=[select id,AccountId ,(select id from Contracts__r)from opportunity where id in:CID];
    for(opportunity  o:Oppids)
    {    
	count = 0;
	for(Contract__c co:o.Contracts__r){
		count++;
	}    
    if(count >1)
    {
     system.debug('ccccccccccccccccccccccccc'+count);
     trigger.new[0].Opportunity__c.addError('Cannot insert Contract. As there is already a Contract to this Opportunity');
    }
    }
   }

 

--
Thanks
Usman

Regular Contributor
street9
Posts: 98
0

Re: Trigger to make it bulkify. to check the count of relatedlist

ok will try. 

Regular Contributor
street9
Posts: 98
0

Re: Trigger to make it bulkify. to check the count of relatedlist

I have tried it, but we could not get the contract id in foe loop inside the forloop of opportunity.

 

trigger Opportunity_With_onecontract on Contract__c (after insert)
{
public integer count;
Set<ID> CID=new Set<ID>();
for(Contract__c c:trigger.new)
{
CID.add(c.Opportunity__c );
}

opportunity []Oppids=[select id,AccountId ,(select id from Contracts__r)from opportunity where id in:CID];
for(opportunity o:Oppids)

count = 0;
for(Contract__c co:o.Contracts__r){

//here we cant get contract id to iterate
count++;
}
if(count >1)
{
system.debug('ccccccccccccccccccccccccc'+count);
trigger.new[0].Opportunity__c.addError('Cannot insert Contract. As there is already a Contract to this Opportunity');
}
}
}

 

 

 

Regular Contributor
MoUsman
Posts: 74
0

Re: Trigger to make it bulkify. to check the count of relatedlist

Hi street9,

 

Please try this, Do have tried same manner as given below, I am **bleep** sure it will work.

 

Set<Id> contractIds = new Set<Id>();
  for(Contract__c co:o.Contracts__r){
     contractIds.add(co.Id);
     count++;
  }

  --

Thanks 

Usman