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
Priti LokhandePriti Lokhande 

how to incerese count on trigger and show this wit associated record

hello everyone 
I have create trigger on project object and i want to show project record associated wit h contract if i delete inbetween then count should go with next value not with deleted value.
e. contract number : 00000101
project = p-00000101-1
 p-00000101-2//if deleted then count is be
p-00000101-3 not p-00000101-2

trigger RollupTriggerrContractProjectCount on Project__C (after insert,after delete){     // RollUpApexClass Handlerclass = new RollUpApexClass();    // Handlerclass.rollupCountMethod();     if(Trigger.isAfter && Trigger.isInsert ){​​​​​​​                  Integer count=1;         System.debug('Inside the Trigger');         list<id> contractId =new list<id>();                  for(Project__c p1:Trigger.new )         {​​​​​​​             system.debug('Inside for loop Row 9');             contractId.add(p1.Contract__c);         }​​​​​​​         list<Contract> updatedContract = new list<contract>();         list<Contract>updatedContractAcc = new list<Contract>([select Id,(select Id from Projects__r)from contract]);         for(Contract c1:updatedContractAcc)         {​​​​​​​             system.debug('inside for loop Row 16');             c1.count__c = c1.Projects__r.size();             system.debug('Count of project '+c1.Projects__r.size());             updatedContract.add(c1);                      }​​​​​​​         if(count==1 ){​​​​​​​             try{​​​​​​​                 update updatedContract;                 count++;                 system.debug('Inserted'+updatedContract);             }​​​​​​​catch(Exception e){​​​​​​​                 system.debug('Error'+e);             }​​​​​​​         }​​​​​​​                              }​​​​​​​       if(Trigger.isAfter && Trigger.isDelete ){​​​​​​​
System.debug('Inside the Trigger'); list<id> contractId =new list<id>();
for(Project__c p1:Trigger.old ) {​​​​​​​ system.debug('Inside for loop Row 37 delete operation'); contractId.add(p1.Contract__c); }​​​​​​​ list<Contract> updatedContract = new list<contract>(); list<Contract>updatedContractAcc = new list<Contract>([select Id,(select Id from Projects__r)from contract]); for(Contract c1:updatedContractAcc) {​​​​​​​ system.debug('inside for loop Row 44'); c1.count__c = c1.Projects__r.size(); system.debug('Count of project '+c1.Projects__r.size()); updatedContract.add(c1); }​​​​​​​ try{​​​​​​​ update updatedContract; system.debug('Updated'+updatedContract); }​​​​​​​catch(Exception e){​​​​​​​ system.debug('Error'+e); }​​​​​​​
}​​​​​​​    }​​​​​​​

help for the code 
​​​​​​​Thank you in advance
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please paste your code in a systematic manner so that community can help you so easily.

User-added image

Thank You

Priti LokhandePriti Lokhande
trigger RollupTriggerrContractProjectCount on Project__C (after insert,after delete){ 
 
    if(Trigger.isAfter && Trigger.isInsert ){
        
        Integer count=1;
      
        list<id> contractId =new list<id>();
        
        for(Project__c p1:Trigger.new )
        {
            system.debug('Inside for loop Row 9');
            contractId.add(p1.Contract__c);
        }
        list<Contract> updatedContract = new list<contract>();
        list<Contract>updatedContractAcc = new list<Contract>([select Id,(select Id from Projects__r)from contract]);
        for(Contract c1:updatedContractAcc)
        {
            system.debug('inside for loop Row 16');
            c1.count__c = c1.Projects__r.size();
            system.debug('Count of project '+c1.Projects__r.size());
            updatedContract.add(c1);
            
        }
        if(count==1 ){
            try{
                update updatedContract;
                count++;
                system.debug('Inserted'+updatedContract);
            }catch(Exception e){
                system.debug('Error'+e);
            }
        }
               
        
    } 
    
if(Trigger.isAfter && Trigger.isDelete ){

 

System.debug('Inside the Trigger');
list<id> contractId =new list<id>();

 

for(Project__c p1:Trigger.old )
{

contractId.add(p1.Contract__c);
}
list<Contract> updatedContract = new list<contract>();
list<Contract>updatedContractAcc = new list<Contract>([select Id,(select Id from Projects__r)from contract]);
for(Contract c1:updatedContractAcc)
{

c1.count__c = c1.Projects__r.size();
system.debug('Count of project '+c1.Projects__r.size());
updatedContract.add(c1);
}
try{
update updatedContract;
system.debug('Updated'+updatedContract);
}catch(Exception e){
system.debug('Error'+e);
}

 


}