+ Start a Discussion
Amn12345Amn12345 

Unable to update parent field from child object.. Please help!!!!!!!!!!!!!!!

Hi,

 

I need help in updating a parent record field with Yes or No value.

 

When I create a child record with lookup field value as hardcoded value, trigger get save but doesnt allow to save child record.

This is the code i am not able to save the child record. On child record I have visualforce page.

 

trigger PopulateDAA on DAA__c (after insert, after update)
{
    set<id> DealIDs = new set<id>();
    set<id> DdataIDs = new set<id>();
    
    for(DAA__c l : trigger.new)
    {
        DealIDs.add(l.Deal__c);
    }
   
    List<Deal__c> deals= [select id, PovRate2530__c from Deal__c where id in :DealIDs ];
    
      deals[0].PovRate2530__c = 'No';
      
        
  for(DAA__c l : trigger.new)
        { 
        
           if(l.Deal__c == 'Poverty Rates Greater Than 25 Percent')
           {
            deals[0].PovRate2530__c = 'Yes';
           
           }

        else

         {
             deals[0].PovRate2530__c = 'No';  
         }
        }
    update deals;    
}

 

This code executes but doesnt work...

As i have VF page on child object its not allowing me to save the record.

 

I feel its a lookup field (Deal__c) so its not reading the value.. may be I am wrong.

 

Please help!!!!

 

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa

Try something like

 

[code quotes removed by author's request]

 

(Changed deals from List to Map (so it becomes bulk friendly for objects that belong to different deals)

(Else statements removed, reason (correct me if my assumption is wrong)

You have object A pointing to object C (A->C)  and object B pointing to object C (B->C), and you have two fields in C called modifiedA__c and modifiedB__c)

If the user updates A (trigger is executed and set modifiedA to 'Yes' and modifiedB to 'No'). [thats ok]

If after that, user updates B (trigger is executed and set modifiedA to 'No' and modifiedB to 'Yes'). [shouldn't modifiedA be kept to 'Yes'?. If so, that is why I removed the else statements].

)

 

Hope I made my self clear :S

 

Regards.

All Answers

SeAlVaSeAlVa
    if(l.NMTC_Deal__c == 'Poverty Rates Greater Than 25 Percent')

 If NMTC_Deal__c is a Lookup field, you should compare it to an ID field or a String that represents an ID, not a plain text String.

If not, explain a little bit more how is your model (where is the lookup field), etc. and what is the business logic you want to achieve

WaveOCWaveOC
In your case PovRate2530__c field will always equal to "No", because this condition "if(l.NMTC_Deal__c == 'Poverty Rates Greater Than 25 Percent')" always equals to FALSE. Maybe you meant to use another field here.
Amn12345Amn12345
Thank u for the reply.. Yes NMTC_Deal__c is a lookup field. In NMTC Deal object we have set of 15 records which are fixed number of records. User cannot change/modify these records, So I want whenever any record is chosen lets say "Robin" then on parent record value updated should be Yes. MORE On this, If(NMTC_Deal__c == 'Robin') { deals[0].PovRate2530__c = 'Yes'; } else deals[0].PovRate2530__c = 'No'; I tried with taking a formula field instead of string which will give me a RecordID but its getting true in all the other case as I have many fields to be updated. What shall I do please suggest??
SeAlVaSeAlVa

1. Have you tried both IDs? (15 and 18 characters long)

2. What is Robin? is that the value of the Name field in the parent record?

3. By this, are you trying to track changes performed on your "children" (details) records at parent Level? Is like an 'used' flag or something?

 

Regards.

Amn12345Amn12345

Thanks very much for ur reply

 

1. Yes I tried with both the ID's, now my code is working one way its updating Parents record fields as Yes or No but everytime record is getting creted it updates all the fields of parent record as NO and only one as YES.

 

As I have used if else seperately, I created another code where I have used ElseIF statement though its giving me YES for perticular field but remaining field are getting value as BLANK. When there is no value there has to be a NO value. how to achieve that??

 

 

2. Robin is just a Record Name i.e. Parent record.

3. Yes I am trying to track changes made to child object on parent level in the form of YES and No Value not any Flag.

I have many fields on Parent as Text fields whenevr Trigger condition satisfies it should update parent field as Yes otherwise No.

 

Thanks in advance!!!!

SeAlVaSeAlVa

Try something like

 

[code quotes removed by author's request]

 

(Changed deals from List to Map (so it becomes bulk friendly for objects that belong to different deals)

(Else statements removed, reason (correct me if my assumption is wrong)

You have object A pointing to object C (A->C)  and object B pointing to object C (B->C), and you have two fields in C called modifiedA__c and modifiedB__c)

If the user updates A (trigger is executed and set modifiedA to 'Yes' and modifiedB to 'No'). [thats ok]

If after that, user updates B (trigger is executed and set modifiedA to 'No' and modifiedB to 'Yes'). [shouldn't modifiedA be kept to 'Yes'?. If so, that is why I removed the else statements].

)

 

Hope I made my self clear :S

 

Regards.

This was selected as the best answer
Amn12345Amn12345

Thank you so much SeAlVa!!!!!!

 

You got it right.. Its working fine for me now.

On parent I took the field as Picklist and made it default as NO now its getting updated by trigger as YEs.

 

Thank you again.. :)