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
Irish@accIrish@acc 

when child's filed updated update the parent's field..Need help

Hi,

I have two object QOD__c--> child object, Approval_Status__c is a field on this object

Project__c--> Parent Object, Project_State__c is a field on this object.

 

I'm writting a trigger whenevr Approval_Status__c changes and equal to Approved then update the Project__State__c to "Closed"

 

below is the trigger but m getting error on it...

trigger Populate on Quality_of_Delivery__c (after update) {

List<Project__c> Lstprj=new List<Project__c>(); Set<Id> PrjId =new Set<Id>();

Lstprj=[Select Id from Project__c];

for(Project__c proj : lstprj){

PrjId.add(proj.Id);

}

    Map<Id,Project__c> prj = new Map<Id, Project__c>([select Project_State__c,Id from project__c where id in :prjid]);//Getting error here..DML requires SObject or SObject list type: MAP<Id,Project__c> at line 30 column 13 

 

      for(Quality_of_Delivery__c q: Trigger.new)  

{  

 

if(trigger.oldmap.get(q.Id).Approved_Status__c!=trigger.newmap.get(q.Id).Approved_Status__c)  

 

    {  

 

    if(  trigger.newmap.get(q.Id).Approved_Status__c=='Approved'){

                     prj.get(q.Project__c).Project_State__c='Closed';      

                          }      

      }         

    update prj;  

        

                      }

  }

 

please look into this

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

The first part of your trigger seems kind of redundant since you are building a set of all the project ids and then querying all the project records again into a map. You are also attempting to update a record inside a FOR loop, which is a no-no. Try out this bulkified code and see if it works...

 

trigger Populate on Quality_of_Delivery__c (after update) 
{
    //create map of all projects
    Map<Id,Project__c> prj = new Map<Id, Project__c>([SELECT Id, Project_State__c FROM Project__c]);
    //create list of all updated projects
    Project__c[] updatedProjects = new Project__c[]{};
     
    for(Quality_of_Delivery__c q : Trigger.new)  
    {  
        //check if the Approved_Status__c has changed
        if(trigger.oldmap.get(q.Id).Approved_Status__c!=q.Approved_Status__c)  
        {  
            //check if the Approved_Status__c is set to "Approved"
            if(q.Approved_Status__c=='Approved')
            {
                Project__c p = prj.get(q.Project__c);
                p.Project_State__c='Closed';
                updatedProjects.add(p);
            }      
        }         
    }

    //check if there are any projects to update 
    if(updatedProjects.size() > 0) update updatedProjects;
}

 

 

Let me know if you have any questions.

All Answers

jbroquistjbroquist

The first part of your trigger seems kind of redundant since you are building a set of all the project ids and then querying all the project records again into a map. You are also attempting to update a record inside a FOR loop, which is a no-no. Try out this bulkified code and see if it works...

 

trigger Populate on Quality_of_Delivery__c (after update) 
{
    //create map of all projects
    Map<Id,Project__c> prj = new Map<Id, Project__c>([SELECT Id, Project_State__c FROM Project__c]);
    //create list of all updated projects
    Project__c[] updatedProjects = new Project__c[]{};
     
    for(Quality_of_Delivery__c q : Trigger.new)  
    {  
        //check if the Approved_Status__c has changed
        if(trigger.oldmap.get(q.Id).Approved_Status__c!=q.Approved_Status__c)  
        {  
            //check if the Approved_Status__c is set to "Approved"
            if(q.Approved_Status__c=='Approved')
            {
                Project__c p = prj.get(q.Project__c);
                p.Project_State__c='Closed';
                updatedProjects.add(p);
            }      
        }         
    }

    //check if there are any projects to update 
    if(updatedProjects.size() > 0) update updatedProjects;
}

 

 

Let me know if you have any questions.

This was selected as the best answer
Irish@accIrish@acc
Thanks for the Reply but m getting one error on first If condition...

Method does not exist or incorrect signature: tr​igger.newmap.get(Id) at line 11 column 57
jbroquistjbroquist

Did you pass just "Id" into the method, or "q.Id" like in my example above? In all reality though you can change trigger.newmap.get(q.Id).Approved_Status__c to just q.Approved_Status__c since it's in the Trigger.new loop.