function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bhanu_prakashbhanu_prakash 

create a rollup summary to child object

we have two object transcation and balance with lookup relationship,
when ever balance child records is more than 50 which is associated to transtion record
if i try to add 51 need to show error . how can i acheive it ?
trigger UpdateItemRollup on Balance__c (after delete, after insert, after update) {

    Set<id> TransactionIds = new Set<id>();
    List<Transaction__c> tr = new List<Transaction__c>();
    List<Balance__c> ba = new List<Balance__c>();

    for (Balance__c item : Trigger.new)
        TransactionIds.add(item.Transaction__c);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Balance__c item : Trigger.old)
            TransactionIds.add(item.Transaction__c);
    }

    // get a map of the traments with the number of items
    Map<id,Transaction__c> TransactionMap = new Map<id,Transaction__c>([select id, RollUp_Count__c from Transaction__c where id IN :TransactionIds]);

    // query the traments and the related inventory items and add the size of the inventory items to the trament's RollUp_Count__c
    for (Transaction__c tra : [select Id, Name, RollUp_Count__c,(select id from Balance__r) from Transaction__c where Id IN :TransactionIds]) {
        TransactionMap.get(tra.Id).RollUp_Count__c = tra.Balance__r.size();
        // add the value/trament in the map to a list so we can update it
        tr.add(TransactionMap.get(tra.Id));
    }

    update tr;

}
Showing error at relationship error
 
[select Id, Name, RollUp_Count__c,(select id from Balance__r) from Transaction__c where Id IN :TransactionIds]) {

we cannot want to install third paty rollup helper or similar ..

Thanks on advance
 
Ankushs@lesforceAnkushs@lesforce
Hi Bhanu , 

Just try to add a if condition at line 20 by checking the size of Balance__r. 
if(Balance__r.size() > 50 ){
trigger.addError('You Cannot Insert more than 50 ');
}
else {
   //do update 
}
bhanu_prakashbhanu_prakash
THanks for update, But still error with realtionship :(
 
trigger UpdateItemRollup on Balance__c (after delete, after insert, after update) {

    Set<id> TransactionIds = new Set<id>();
    List<Transaction__c> tr = new List<Transaction__c>();
    List<Balance__c> ba = new List<Balance__c>();

    for (Balance__c item : Trigger.new)
        TransactionIds.add(item.Transaction__c);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Balance__c item : Trigger.old)
            TransactionIds.add(item.Transaction__c);
    }

    // get a map of the traments with the number of items
    Map<id,Transaction__c> TransactionMap = new Map<id,Transaction__c>([select id, RollUp_Count__c from Transaction__c where id IN :TransactionIds]);

    // query the traments and the related inventory items and add the size of the inventory items to the trament's RollUp_Count__c
    for (Transaction__c tra : [select Id, Name, RollUp_Count__c,(select id from Balance__r) from Transaction__c where Id IN :TransactionIds]) {
           if(Balance__r.size() > 50 ){
             trigger.addError('You Cannot Insert more than 50 ');
              }
          else { 
	          TransactionMap.get(tra.Id).RollUp_Count__c = tra.Balance__r.size();
                tr.add(TransactionMap.get(tra.Id));
                }
		update tr;

}

 
Kiran Kumar 690Kiran Kumar 690
Hi Bhanu Prakash,

  What is the error you are getting? 
Also make sure that you are using correct child relation name in your query.

Regards,
Kiran
bhanu_prakashbhanu_prakash
Yes, relationship is correct

RollUp_Count__c,(select id from Balance__c) from Transaction__c
                                ^
ERROR at Row:1:Column:50
Didn't understand relationship 'Balance__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Kiran Kumar 690Kiran Kumar 690
Hi,

Cross the child Relation ship name once,

User-added image
Here Employee__c is child object and Project is a Parent object, so use the relationship as Employees__r

Ex: Select Id,Name,(Select Id,Name from Employees__r) From Project__c