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
atharva Vispute 3atharva Vispute 3 

Help me to solve this problem

Hi,

 Create an apex trigger on Resource to make sure that It does not exceeds Project assignment limit as per Defined on Project types. 
·         If Project.Type == Hot than max 4 Resources Allowed to be created under that project 
·         If Project.Type == Warm than max 2 Resources Allowed to be created under that project 
·         If Project.Type == Cold than max 1 Resource is Allowed to be created under that project 
  
Example: Consider there is project named “TUV” with Type as “Warm” than system should only allow max 2 resources to be relate to TUV Project, if User tries to related more than 2 resources than System should Throw error that “Project Assignment Limit Reached” .
Plzz help me to find this solution in trigger. 

Thanks!!
Best Answer chosen by atharva Vispute 3
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Can you try the below apex trigger.
 
trigger Resourcescount on Resource__c (before insert,before update) {
    set<id> projectids= new set<id>();
    resource__c oldrs= new resource__c();
    for(Resource__c rs:Trigger.new){
        if(Trigger.isupdate)
             oldrs= trigger.oldmap.get(rs.id);
        
        if((Trigger.isinsert && rs.Project_Name__c!=null) || (Trigger.isupdate && rs.Project_Name__c!=oldrs.Project_Name__c) ){
            projectids.add(rs.Project_Name__c);

        }
    }

    map<id, Project__c>mapproj=new map<id,Project__c>([select id,Name,Type__c,(select id from resources__r) from project__C where id in :projectids]);
    
    for(Resource__c rse:Trigger.new){
        if(mapproj.containsKey(rse.Project_Name__c)){

           Project__c pre= mapproj.get(rse.Project_Name__c);
            system.debug('pre.Type__c'+pre.Type__c);
            system.debug('pre.resources__r.size()'+pre.resources__r.size());
            if(pre.Type__c=='Hot' && pre.resources__r.size() >3){
               
                rse.adderror('you canot create more than 4 child records for project');
            }
            if(pre.Type__c=='Warm' && pre.resources__r.size() >1){
               
                rse.adderror('you canot create more than 2 child records for project');
            }
            if(pre.Type__c=='cold' && pre.resources__r.size() ==0){
               
                rse.adderror('you canot create more than 1 child records for project');
            }
        }
    }
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

In the above scenerio Project is the parent object and Resource is child object. Can you confirm the API name of project lookup in Resource object.

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Project_Name__c   This is the API name in lookup object.

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Can you try the below apex trigger.
 
trigger Resourcescount on Resource__c (before insert,before update) {
    set<id> projectids= new set<id>();
    resource__c oldrs= new resource__c();
    for(Resource__c rs:Trigger.new){
        if(Trigger.isupdate)
             oldrs= trigger.oldmap.get(rs.id);
        
        if((Trigger.isinsert && rs.Project_Name__c!=null) || (Trigger.isupdate && rs.Project_Name__c!=oldrs.Project_Name__c) ){
            projectids.add(rs.Project_Name__c);

        }
    }

    map<id, Project__c>mapproj=new map<id,Project__c>([select id,Name,Type__c,(select id from resources__r) from project__C where id in :projectids]);
    
    for(Resource__c rse:Trigger.new){
        if(mapproj.containsKey(rse.Project_Name__c)){

           Project__c pre= mapproj.get(rse.Project_Name__c);
            system.debug('pre.Type__c'+pre.Type__c);
            system.debug('pre.resources__r.size()'+pre.resources__r.size());
            if(pre.Type__c=='Hot' && pre.resources__r.size() >3){
               
                rse.adderror('you canot create more than 4 child records for project');
            }
            if(pre.Type__c=='Warm' && pre.resources__r.size() >1){
               
                rse.adderror('you canot create more than 2 child records for project');
            }
            if(pre.Type__c=='cold' && pre.resources__r.size() ==0){
               
                rse.adderror('you canot create more than 1 child records for project');
            }
        }
    }
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
atharva Vispute 3atharva Vispute 3
Hi Praveen,

No error in code but I am not getting any output.

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Are you trying to create 5th resource record on project with type__c field as 'Hot' ?

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,
Yes I am creating 5th record and field type is also hot

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Did you check the debug logs in developer console after creating a new record?

Are you able to get the twi system.debug statements?

Thanks.
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Yes, I am getting that statement.

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Are you getting three statemnst and values. Can you share the screenshot of it.

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

No I am not geeting that three statements.

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share. the screenshot of the log here in screenshot.

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

User-added imageUser-added image

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

This is working on my org. Can you check the API names of the objects and also related list as well . This is working as expected in my org with the above trigger.

If you need we can connect and check the issue.

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen, 

Now it's working. 

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Can you confirm what excatly the issue is  and how is it resolved.


Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

I guess there is an issue in my org so run it in my friends org and ir works.

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Thanks for confirming.

Happy Learning!!

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Yhank you very much for helping me.. If  you don't mind can you provide me your contact detail, So i can reach you via mail or contact no also..

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

I am happy to know this! I am a regular visitor of developer forums.  Feel free to make a new post about where you are stuck and I’m here to help :) 

Thanks,