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
salesforce_hoonigansalesforce_hoonigan 

Trigger Help: Update Parent checkbox if child field was changed

Hi Experts,

I'm still total newb in apex. I hope you could assist me on this.

Criteria:
Uncheck Textbox1__c (Lead - Parent) if Lookup__c (Document__r - child) was changed to blank.

I would appreciate any help coming from the experts.

Thank you.
ManojjenaManojjena
HI salesforce_hoonigan,
Here I have few questions ,please clarify so that we can help you .
What is your child object name ?
Do you mean when your look up field Document__r wil change to blank you need to make the Textbox1__c needs to blank .
Please confirm .
Thanks
Manoj


 
pconpcon
Take a look over these articles on how to start writing a trigger [1] [2].  Then you will want to iterate through all of your Leads when they are updated and if the Lookup__c == null then set Textbox1__c = false;  This will be a before update trigger so you do not have to update the records.

[1] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[2] http://www.sfdc99.com/2013/05/12/example-how-to-write-a-simple-apex-trigger-2/
salesforce_hoonigansalesforce_hoonigan
Hi Manoj,

Thank you in advance. Just to clarify:

Lead (parent)
Document__r (custom - child)

Textbox1__c (residing in Lead)
Lookup__c (residing in custom object)

Expected behavior:
When the lookup__c has been changed to blank, it will uncheck the box in the textbox__c

Thanks in advance Manoj.
ManojjenaManojjena
HI Salesforce_hoonigan,

Try with belwo code !!
trigger UpdateParent on Document__c (after update){
  List<Lead> ledListToUpdate=new List<Lead>();
  for(Document__c doc: Trigger.new){
    if(doc.Lookup__c==null && Trigger.oldMap.get(doc.Id).Lookup__c !=null){
	    Lead led =new Lead(id=Trigger.oldMap.get(doc.Id),Textbox1__c=False);
		ledListToUpdate.add(led);
	}
  }
  if(!ledListToUpdate.isEmpty()){
    Try{
	  update ledListToUpdate;
	}catch(DmlException de){
	  System.debug(de);
	}
  }
}

Let me know if it helps !!
Thanks
Manoj
salesforce_hoonigansalesforce_hoonigan
Hi Manoj.

I've tried your code and I got Invalid initial expression type for field Lead.Id, expecting: Id at Line 5

Thank you.
ManojjenaManojjena
HI Hoonigan,
Try with below code it will work I have missed some thing ..
trigger UpdateParent on Document__c (after update){
  List<Lead> ledListToUpdate=new List<Lead>();
  for(Document__c doc: Trigger.new){
    if(doc.Lookup__c==null && Trigger.oldMap.get(doc.Id).Lookup__c !=null){
	    Lead led =new Lead(id=Trigger.oldMap.get(doc.Id).Lookup__c,Textbox1__c=False);
		ledListToUpdate.add(led);
	}
  }
  if(!ledListToUpdate.isEmpty()){
    Try{
	  update ledListToUpdate;
	}catch(DmlException de){
	  System.debug(de);
	}
  }
}

Let me know if it helps !!
Thanks
Manoj