+ Start a Discussion
System Admin 949System Admin 949 

Update Email Fields in Related Objects

Dear Community,
I have two Objects ProductFamily__c and PlantContact__c.ProductFamily is parent and PlantContact is child.i have email fields in both objects like AR&D,Commercial,DQA,Marketing,Pilot,Production,QA,QC,R&D..etc.also one checkbox field called 'Default ' in PlantContact object.
and  to fill ProductFamily Email Fields  based on the default value on PlantContact for that type.
use trigger/Process builder shall be involed every time a plant contact is created or updated with default = true, action will then be to find the corresponding product family record and update the relevant email field based on type of plant contact that is being set default.If default=false put null value in the corresponding fields of ProductFamily.For each type in PlantContact put only one default value to the corresponding ProductFamily.
For example ProductFamily reocrd is PF-0001 and it is has only one default record like  AR&D,R&D,QA,QC,..etc child records.If the user try to add one more record for above types as default it will throws error.if the above types are don't select default=true put blank/null values in corresponding ProductFamily Record.
thanks in Advance
NitishNitish
Hi,

As far as i have understood your requirement i think this code will work for you or else please try to explain clearly.
Trigger PlantContactTrigegr on PlantContact__c(after insert,after update){
	if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)) {
	List<ProductFamily__c> pfList=new List<ProductFamily__c>();
		for(PlantContact__c pc:Trigger.new){
			if(pc.default__c == TRUE){
				ProductFamily__c pf=new ProductFamily__c();
				pf.AR&D=pc.AR&D;
				pf.Commercial=pc.Commercial;
				pf.DQA=pc.DQA;
				pf.Marketing=pc.Marketing;
				pf.Pilot=pc.Pilot;
				pf.Production=pc.Production;
				pf.Id=pc.productFamilyId;
				pfList.add(pf);
				
			}else{
				ProductFamily__c pf=new ProductFamily__c();
				pf.AR&D='';
				pf.Commercial='';
				pf.DQA='';
				pf.Marketing='';
				pf.Pilot='';
				pf.Production='';
				pf.Id=pc.productFamilyId;
				pfList.add(pf);
			}
		}
		if(pfList.size()>0){
			update pfList;
		}
	}
}

Let me know in case of any query.

Thanks,
Nitish
System Admin 949System Admin 949
Thank you so much NItish for your quick answer.i.e. my requirement..
in 2nd case,i want to create a ProductFamily Record.later through ProductFamily related list i want to create a PlantContact Record.for this while i m clicking a new button in PlantContact and select any type like AR&D,R&D,QC,QA....etc.And it is the first record for each type.I mean suppose i created record of PlantContact with AR&D and not selecting the checkbox Default=true.for that time any record of type AR&D not associated to ProductFamily record after saving the record the checkbox set to true,bcoz this is the first record for AR&D type to that ProductFamily Record.later user created record in PlantContact of same type AR&D  with default=true to same ProductFamily Record(same Parent),it will through error bcoz already record created with AR&D type and default=true.
How to achieve this?? how system knows if the record of each type should be associated or not to that perticular Parent record.
thanks in advance
NitishNitish
Hi,
AR&D,R&D all these fields are Email fields right on PlantContact Object? Then how you are selecting type?
System Admin 949System Admin 949
sorry for  that AR&D,R&D,....etc are the picklist values in TypeOfContact Field in PlantContact object.also The email__c field is there in PlantConatct object,based on that email field update the ProductFamily Email Fields.i posted sample code here you will understood for this..

Trigger PlantContactTrigegr on PlantContact__c(after insert,after update){
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)) {
    List<ProductFamily__c> pfList=new List<ProductFamily__c>();
        for(PlantContact__c pc:Trigger.new){
            if(pc.Default__c == TRUE){
                ProductFamily__c pf=new ProductFamily__c();
                if(pc.TypeOfContact__c==AR&D){
                pf.ARDRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==R&D){
                pf.RDRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==Production){
                pf. ProductionRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==RA){
                pf.RARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==DQA){
                pf.DQARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==QC){
                pf.QCRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==QA){
                pf.QARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==Pilot){
                pf.PilotID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==Marketing){
                pf.MarketingID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c==Commercial){
                pf.CommercialRespID__c=pc.Email__c;
                }
                
               /*f.Commercial=pc.Commercial;
                pf.DQA=pc.DQA;
                pf.Marketing=pc.Marketing;
                pf.Pilot=pc.Pilot;
                pf.Production=pc.Production;*/
                
                pf.Id=pc.ProductFamily__c;
                pfList.add(pf);
                
            }else{
                ProductFamily__c pf=new ProductFamily__c();
                pf.ARDRespID__c='';
                pf.RDRespID__c='';
                pf.CommercialRespID__c='';
                pf.DQARespID__c='';
                pf.MarketingID__c='';
                pf.PilotID__c='';
                pf.ProductionRespID__c='';
                pf.QARespID__c='';
                pf.QCRespID__c='';
                pf.PilotID__c='';
                pf.Id=pc.ProductFamily__c;
                pfList.add(pf);
            }
        }
        if(pfList.size()>0){
            update pfList;
        }
    }
}
Note: Error: Compile Error: Variable does not exist: AR at line 7 column 41
NitishNitish
Hi,
Try something like below code it might help or let me know if you will face any specific issue in that because i haven't tried that perticular code from my side.
Trigger PlantContactTrigegr on PlantContact__c(after insert,after update){
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)) {
	Set<ProductFamily__c> parentId=new Set<ProductFamily__c>();
	for(PlantContact__c p1:Trigger.new){
		parentId.add(p1.ProductFamily__r.Id);
	}
	
	
    List<ProductFamily__c> pfList=new List<ProductFamily__c>();
        for(PlantContact__c pc:Trigger.new){
            if(pc.Default__c == TRUE){
                ProductFamily__c pf=new ProductFamily__c();
                if(pc.TypeOfContact__c=='AR&D'){
                pf.ARDRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='R&D'){
                pf.RDRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='Production'){
                pf.ProductionRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='RA'){
                pf.RARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='DQA'){
                pf.DQARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='QC'){
                pf.QCRespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='QA'){
                pf.QARespID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='Pilot'){
                pf.PilotID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='Marketing'){
                pf.MarketingID__c=pc.Email__c;
                }
                else if(pc.TypeOfContact__c=='Commercial'){
                pf.CommercialRespID__c=pc.Email__c;
                }
                
               /*f.Commercial=pc.Commercial;
                pf.DQA=pc.DQA;
                pf.Marketing=pc.Marketing;
                pf.Pilot=pc.Pilot;
                pf.Production=pc.Production;*/
                
                pf.Id=pc.ProductFamily__c;
                pfList.add(pf);
                
            }else{
			
					for(ProductFamily__c pf2:[SELECT Id,ARDRespID__c,RDRespID__c,DQARespID__c,QCRespID__c,QARespID__c,PilotID__c,MarketingID__c,CommercialRespID__c
									FROM ProductFamily__c WHERE Id in : parentId]){
						if(pf2.ARDRespID__c!='' && pc.TypeOfContact__c=='AR&D' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.RDRespID__c!='' && pc.TypeOfContact__c=='R&D' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.ProductionRespID__c!='' && pc.TypeOfContact__c=='Production' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.RARespID__c!='' && pc.TypeOfContact__c=='RA' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.DQARespID__c!='' && pc.TypeOfContact__c=='DQA' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.QCRespID__c!='' && pc.TypeOfContact__c=='QC' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.QARespID__c!='' && pc.TypeOfContact__c=='QA' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.PilotID__c!='' && pc.TypeOfContact__c=='Pilot' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.MarketingID__c!='' && pc.TypeOfContact__c=='Marketing' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}else if(pf2.CommercialRespID__c!='' && pc.TypeOfContact__c=='Commercial' && pf2.Id == pc.ProductFamily__r.Id){
							
							pc.addError('One record is already available');
						}
					}		
			
            }
        }
        if(pfList.size()>0){
            update pfList;
        }
		
		
    }
}

Thnaks,
Nitish​