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
Kiran Kumar 225Kiran Kumar 225 

Trigger for update child status value

hi, I have a following code, which updates all child records when status changes from complete to not complete. but my code is not woking.
trigger status on Product__c (after update) {
    set<String> productId = new set<String>();
    for(Product__c pro : trigger.old){
        if(pro.Status__c == 'Complete'){
            productId.add(pro.id);
            System.debug('Completed--->'+pro);
        }    
    }
    List<Vehicle__c> vechicalList = new List<Vehicle__c>();
    for(Vehicle__c vech:[select id,name,Status__c,Product__r.Status__c from Vehicle__c WHERE Product__c IN: productId ]){
        system.debug('vech---->'+vech);
        if(vech.Product__r.Status__c== 'Not Completed'){
            vech.Status__c = 'Incomplete';
            vechicalList.add(vech);
       }
    }
    update vechicalList;
}
Kindlyletme know where I did mistake.
Best Answer chosen by Kiran Kumar 225
Irfan Khan 14Irfan Khan 14
Hi Kiran,

You can do this like below, first you need to check the status is changed or not and then check its 'Complete'
 
trigger status on Product__c (after update) {
    set<String> productId = new set<String>();
    for(Product__c pro : trigger.new){
		// check if status is changed and its 'Complete' now. 
        if(pro.status__c != trigger.oldmap.get(pro.id).status__c && pro.Status__c == 'Complete'){
            productId.add(pro.id);
            System.debug('Completed--->'+pro);
        }    
    }
    List<Vehicle__c> vechicalList = new List<Vehicle__c>();
    for(Vehicle__c vech:[select id,name,Status__c,Product__r.Status__c from Vehicle__c WHERE Product__c IN: productId ]){
        system.debug('vech---->'+vech);
        if(vech.Product__r.Status__c == 'Not Completed'){
            vech.Status__c = 'Incomplete';
            vechicalList.add(vech);
       }
    }
    update vechicalList;
}

Let me know in case it doesn't work.

Thanks.

All Answers

Vijay NagarathinamVijay Nagarathinam
Hi Kiran,

Use the below code it will work,
 
trigger status on Product__c (after update) {
    set<String> productId = new set<String>();
    for(Product__c pro : trigger.old){
        if(pro.status__c != trigger.oldmap.get(pro.status__c) && pro.Status__c = 'Complete'){
            productId.add(pro.id);
            System.debug('Completed--->'+pro);
        }    
    }
    List<Vehicle__c> vechicalList = new List<Vehicle__c>();
    for(Vehicle__c vech:[select id,name,Status__c,Product__r.Status__c from Vehicle__c WHERE Product__c IN: productId ]){
        system.debug('vech---->'+vech);
        if(vech.Product__r.Status__c= 'Not Completed'){
            vech.Status__c = 'Incomplete';
            vechicalList.add(vech);
       }
    }
    update vechicalList;
}

FYI, You can acheive this using process builder to update all child record status when the status is Completed.

Let me know if you need any help regarding this. 

Thanks,
Vijay
Irfan Khan 14Irfan Khan 14
Hi Kiran,

You can do this like below, first you need to check the status is changed or not and then check its 'Complete'
 
trigger status on Product__c (after update) {
    set<String> productId = new set<String>();
    for(Product__c pro : trigger.new){
		// check if status is changed and its 'Complete' now. 
        if(pro.status__c != trigger.oldmap.get(pro.id).status__c && pro.Status__c == 'Complete'){
            productId.add(pro.id);
            System.debug('Completed--->'+pro);
        }    
    }
    List<Vehicle__c> vechicalList = new List<Vehicle__c>();
    for(Vehicle__c vech:[select id,name,Status__c,Product__r.Status__c from Vehicle__c WHERE Product__c IN: productId ]){
        system.debug('vech---->'+vech);
        if(vech.Product__r.Status__c == 'Not Completed'){
            vech.Status__c = 'Incomplete';
            vechicalList.add(vech);
       }
    }
    update vechicalList;
}

Let me know in case it doesn't work.

Thanks.
This was selected as the best answer
Kiran Kumar 225Kiran Kumar 225
Thanks Vijay and Irfan