Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Trigger to make it bulkify. to check the count of ...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Trigger to make it bulkify. to check the count of relatedlis t
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 09:38 PM
How can i remove query from For loop and check this count
trigger Opportunity_With_onecontract on Contract__
{
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
for(opportunity o:Oppids)
{
count=[select count() from Contract__c where Opp
I want to remove this query from for loop.
if(count >1)
{
system.debug('ccccccccccccccccccccccccc'+count
trigger.new[0].Opportunity__c.addError('Canno
}
}
}
Re: Trigger to make it bulkify. to check the count of relatedlis t
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 09:57 PM
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
Re: Trigger to make it bulkify. to check the count of relatedlis t
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 10:06 PM
ok will try.
Re: Trigger to make it bulkify. to check the count of relatedlis t
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-11-2012 05:42 PM
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');
}
}
}
Re: Trigger to make it bulkify. to check the count of relatedlis t
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-11-2012 09:02 PM
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

