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
Ram 1969Ram 1969 

Want parent picklist to update some of the child record picklist

Hi, 
I am newbie here and pretty new to SFDC. I would like to achieve following
1. On opportunity stage - when I select "Opp Lost" - I need it to auto change all records in related list (custom object) picklist field to "Opp Lost"
2. But if I select opportunity stage "Opp Won" - I need it to change the records to "Opp Won" that are currently "In Progress" stage. Any records that are selected "Opp Lost" are not changed.
FYI - In Opportunity stage - we have 7 stage including "Opp Lost" & "Opp Won" however in the related list custom object picklist field - we have 3 stages "Opp Won", "In Progress" & "Opp Lost" 
How can I achieve this? Coding is not an option since I am a rookie.
Thanks / Ram
 
Best Answer chosen by Ram 1969
Deepak Pandey 13Deepak Pandey 13
Yes sure .1stly you just write a trigger on Opportunity after update.
Please view this video it will help you- https://www.youtube.com/watch?v=ArxaE-CHnCM

All Answers

Ram 1969Ram 1969
A follow up question to above.
On the Opp page - I have "Opp Value" which is a formula field picking value from roll up summary. The roll up summary is aggregating "value" from custom object field
On every custom object record (as mentioned above) - I have 3 stages - "Opp Won", "In Progress" & "Opp Lost" & the user can also enter the "value" for each record. 
So now if a user changes the stage in custom object to "Opp Lost" - I would like "Opp Value" exclude the value of this record (so basically the roll up summary is aggregating the records that are either "Opp Won"or "In Progress"  
How can this be achieved? 

 
Deepak Pandey 13Deepak Pandey 13
I think you can write a trigger on Opportunity which is closed then update it's related list.
Ram 1969Ram 1969
Thanks Deepak. Any suggestion? I am very new to Triggers.
Deepak Pandey 13Deepak Pandey 13
I did't know your child Api but your Trigger like-
Trigger OppClosewonChangechildRecord on Opportunity(After Update)
{
    Set<id> setoppid = new set<id>();
    for(Opportunity Objopp : trigger.new)
    {
        if(Objopp.Stagename == 'Close lost')
        {
            setoppid.add(Objopp.id);
        }
    }
    list<ChildObjectName> lstchildobject = new list<ChildObjectName>();
    
    if(setoppid.size()>0)
    {
        for(Childobjectname instant_name : [select id,Childobject_Field from Childobjectname where oppid(OpportunityAPI_on_childObject) in: setoppid])
        {
            instant_name.Fieldname = 'Closed Lost';
            lstchildobject.add(instant_name);
        }
        if(lstchildobject.size()>0)
            update lstchildobject;
    }
}
please try. let me know this is working or not.
Ram 1969Ram 1969
Is trigger the only solution for both of my problems - above? 
Deepak Pandey 13Deepak Pandey 13
For second you take a another set which is holding opp id which is come close won follow above cod. and after adding child stagename  on list just check is this not close lost if it is then leave otherwise add it.
Ram 1969Ram 1969
Sorry deepak...as I said that I am newbie and the object name and API that I mentioned above are not actual API and object name - I just wanted to simply the situation for everbody to understand and give my recommendation.

I am not sure whether you have a step-by-step instructions on the whole scenario? If needed - I can give the actual object names and  API 

Thanks for all your help, so far!
Deepak Pandey 13Deepak Pandey 13
Okey. Just send your Api with child and parent.
Ram 1969Ram 1969
The child API is PET_Product__c and the stage in this Child object is Status__c. Status has 3 picklist - Open, Won & Lost
The parent API is Opportunity and Stage
 
Deepak Pandey 13Deepak Pandey 13
Try
Trigger OppClosewonChangechildRecord on Opportunity(After Update)
{
    Set<id> setoppid = new set<id>();
    Set<id> setoppClosewon = new set<id>();
    for(Opportunity Objopp : trigger.new)
    {
        if(Objopp.Stagename == 'Close lost' && trigger.oldmap.get(Objopp.id).StageName != Objopp.Stagename )
        {
            setoppid.add(Objopp.id);
        }
        if(Objopp.Stagename == 'Close Won' && trigger.oldmap.get(Objopp.id).StageName != Objopp.Stagename )
        {
            setoppClosewon.add(Objopp.id);
        }
    }
    list<PET_Product__c> lstchildobject = new list<PET_Product__c>();
    
    if(setoppid.size()>0)
    {
        for(PET_Product__c  Petpro : [select id,Name,Status__c from PET_Product__c where Opportunity in: setoppid])
        {
            Petpro.Status__c = 'Closed Lost';
            lstchildobject.add(Petpro);
        }
        if(lstchildobject.size()>0)
            update lstchildobject;
    }
    
    if(setoppid.size()>0)
    {
        for(PET_Product__c  Petpro : [select id,Name,Status__c from PET_Product__c where Opportunity in: setoppid and Status__c != 'Closed Lost'])
        {
            Petpro.Status__c = 'Closed Won';
            lstchildobject.add(Petpro);
        }
        if(lstchildobject.size()>0)
            update lstchildobject;
    }
}
Ram 1969Ram 1969
Thank you so much Deepak and so sorry that my team has changed the requirement now. I am providing screen shot and the new scenario. Can you please suggest whether a Validation Rule will work for this? If yes, what would be the formula?
User-added image
Scenario 1: If user changes the Selling stage in Opportunity to Closed Won, then none of the PET Products record should have status as Open. And at least one record should be Won. 
Scenario 2: If user changes the Selling stage in Project Detail to Closed Lost, then none of the PET Products record should have status as Open or Won.
We want the user to manually change the status, for Scenario 1, instead of automatic trigger.
However, for Scenario 2 - trigger is okay.
Can you please suggest a solution?

Many Thanks again for your help!

 
Deepak Pandey 13Deepak Pandey 13
I think, you can not prevent the multiple record with Validation. So you have to write a trigger to prevent or not wanted to change the record.
Ram 1969Ram 1969
Please note that the user first create the Opportunity and add multiple PET_Product records with Status ranging from Open to Lost to Won for each record. 
Now if the user change the Opportunity stage to "Closed Won" - I need them to make sure that at least one record in PET_Product has Status as "Won" and none of the records are "Open"
Can you please advise what would the trigger do in this situation? And what would be the trigger? 
I tried the trigger you sent earlier above but it is giving some errors....I believe these errors are because I did not provide you the API name in the first place and I dont know where to change the API names in the trigger script.
Deepak Pandey 13Deepak Pandey 13
Yes sure .1stly you just write a trigger on Opportunity after update.
Please view this video it will help you- https://www.youtube.com/watch?v=ArxaE-CHnCM
This was selected as the best answer