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
Radha Rathinavel PandianRadha Rathinavel Pandian 

update the checbox true in child when the parent is updated

Hi ,

Parent Object: Legal_Description__c
Child Object : Case

Legal description has a lookup field to case. I have a check box(Check_Legal_Description) in the case object, so that whenever I inserted a new record on legal description the checkbox in the case object needs to be checked.


 
Best Answer chosen by Radha Rathinavel Pandian
Amit Chaudhary 8Amit Chaudhary 8
Add RecordTyoe.Name in query.

trigger LegalCheck on Legal_Description__c (after insert, after update) {
List<Id> legalId = new List<Id>();
for(Legal_Description__c lg : Trigger.new)
{
legalId.add(lg.Case__c);
}

List<Case> caseRecord = new List<Case>();
for(Case c:[SELECT id,Check_Legal_Description__c,RecordType.name FROM Case WHERE id IN:legalId])
{
c.Check_Legal_Description__c=true;
caseRecord.add(c);
}
update caseRecord;
}

Let us know if this will help you
 

All Answers

Raj VakatiRaj Vakati
You can do it with process Builder 

https://help.salesforce.com/articleView?id=000213419&type=1
https://automationchampion.com/2015/02/13/getting-started-with-process-builder-part-1-auto-create-a-record/
Radha Rathinavel PandianRadha Rathinavel Pandian
Raj,

I need to implement in trigger
Amit Chaudhary 8Amit Chaudhary 8
Hi Radha,

Your code should be like below. Please change the API according to your org
trigger updateCase on Legal_Description__c(After insert ,After update) 
{
	Set<ID> setCaseID = new Set<Id>();
	for(Legal_Description__c ld :Trigger.New){
		if( ld.Check_Legal_Description__C == true )
		{
			if(id.case__C != null){
				setCaseID.add(id.case__C);
			}
		}
	}
	if(setCaseID.size() > 0 ){
		List<Case> lstCase = [Select id,Check_Legal_Description__C from case where id in :setCaseID];
		for(Case cs: lstCase){
			cs.Check_Legal_Description__C = true;
		}
		update lstCase;
	}
}

Let us know if this will help you
 
Jainam ContractorJainam Contractor
Hi Radha,

I am bit confused with where does the Lookup field resides..?? If Legal_Description__c has a look up of Case, then you can update the Checkbox field on Case. If Legal_Description__c is the Parent Object then you cannot update the Case record as there would not be any relation between the Case and the newly inserted Legal_Description__c record.

Can you please confirm as to where does the lookup field resides and which record you want to update.

Thanks,
Jainam Contractor
Radha Rathinavel PandianRadha Rathinavel Pandian
jainam,

Legal Description have a lookup for case. I need to enable the checkbox on case object when I added the record in legal description.
Radha Rathinavel PandianRadha Rathinavel Pandian
@Amit,

Thank you for the code. I have structured with my requirement. Please find below,
trigger LegalCheck on Legal_Description__c (after insert, after update) {
List<Id> legalId = new List<Id>();
for(Legal_Description__c lg : Trigger.new)
{
legalId.add(lg.Case__c);
}

List<Case> caseRecord = new List<Case>();
for(Case c:[SELECT id,Check_Legal_Description__c FROM Case WHERE id IN:legalId])
{
c.Check_Legal_Description__c=true;
caseRecord.add(c);
}
update caseRecord;
}

Here, for my 2 nd step how to get the record type name from the case record?
Amit Chaudhary 8Amit Chaudhary 8
Add RecordTyoe.Name in query.

trigger LegalCheck on Legal_Description__c (after insert, after update) {
List<Id> legalId = new List<Id>();
for(Legal_Description__c lg : Trigger.new)
{
legalId.add(lg.Case__c);
}

List<Case> caseRecord = new List<Case>();
for(Case c:[SELECT id,Check_Legal_Description__c,RecordType.name FROM Case WHERE id IN:legalId])
{
c.Check_Legal_Description__c=true;
caseRecord.add(c);
}
update caseRecord;
}

Let us know if this will help you
 
This was selected as the best answer
Jainam ContractorJainam Contractor
Hi Radha,

I would suggest you to add a check for Case__c != NULL before adding it to the list. It might throw error if Case__c is not populated in the Legal_Description__c record.

IF(lg.Case__c != NULL){
    legalId.add(lg.Case__c);
}

And before that SOQL for Loop, check if the List is not NULL && Not empty. 

IF(legalId != NULL && legalId.size()>0){
    //Your Code
}


You can get the Record Type Name from the below line of code:

string recordtypename =Schema.SObjectType.Case.getRecordTypeInfosById().get(c.recordtypeid).getname();

Let me know if you need more assistance.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Radha Rathinavel PandianRadha Rathinavel Pandian
Amit,
 My next requirement is, I have a list of record types A, B and C..... My case object checkbox wants to be enable default when the record type was A or B , orelse it should be unchecked...If I create a legal description for C..my checkbox want to be checked. How can I proceed this?
Radha Rathinavel PandianRadha Rathinavel Pandian

Hi Amit,
This my new requirement forum​https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005V7OQAU