0
trigger ProcdureTrigger on Test1105__Procedure__c (before update) {
    public map<Test1105__Procedure__c,List<Test1105__Procedure_product__c>> accountContactMap = new map<Test1105__Procedure__c,List<Test1105__Procedure_product__c>>();

    List<Test1105__Procedure__c> lstAccount = [SELECT Id, name, (SELECT Test1105__Procedure_Name__c,Test1105__Steerable__c,Test1105__CS__c FROM Test1105__Procedure_products__r) FROM Test1105__Procedure__c];

    for(Test1105__Procedure__c acc : lstAccount)
    {
        accountContactMap.put(acc, acc.Test1105__Procedure_products__r);
    }

    for(Test1105__Procedure__c pro :accountContactMap.Values()){
        if(pro.Test1105__Status__c =='Completed' && pro.Test1105__Type_of_Event__c =='Rythmia Case'){
            if(pro.Test1105__Steerable__c == NULL){
                pro.addError('Need Related Records');
            }

        }
    }

    System.debug(accountContactMap);

}
2

To access child records you need a nested for loop based on your existing code. Also you don't need map and list here and can directly query records in for loop.

You are also querying all records while you only need currently processed record so I have added check for that as well. This is how your final code will look like

trigger ProcdureTrigger on Test1105__Procedure__c (before update) {

    for(Test1105__Procedure__c pro : [SELECT Id, name, (SELECT Test1105__Procedure_Name__c,Test1105__Steerable__c,Test1105__CS__c FROM Test1105__Procedure_products__r) FROM Test1105__Procedure__c WHERE ID IN: Trigger.New]){
        for(Test1105__Procedure_product proChild : pro.Test1105__Procedure_products__r) { // check API name here
            if(proChild.Test1105__Status__c =='Completed' && proChild.Test1105__Type_of_Event__c =='Rythmia Case' && proChild.Test1105__Steerable__c == NULL){
                Trigger.Newmap.get(pro.Id).addError('Need Related Records');
            }
        }
    }

    System.debug(accountContactMap);

}
|improve this answer|||||
  • 1
    Is it a good practice to write query in for loop??.I changed my code and it is working correctly .Thank you :-) .I was asked to use map ,so i was wondering how it would work. – Nikita Gupta Feb 16 '19 at 10:21
  • @NikitaGupta yes it is. Depends on the situation also. with map you only need to add nested for loop – Tushar Sharma Feb 16 '19 at 10:40
  • Hey Tushar i am able to save child records even if my parent record are completed and rythmia case – Nikita Gupta Feb 18 '19 at 12:59
  • @NikitaGupta just add break condition as well. – Tushar Sharma Feb 19 '19 at 9:08
  • Not working.map<Test1105__Procedure__c,List<Test1105__Procedure_product__c>> ProMap = new map<Test1105__Procedure__c,List<Test1105__Procedure_product__c>>();.Can you help me with this?i am asked to use map for the same scenario. – Nikita Gupta Feb 19 '19 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.