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
NANCY1NANCY1 

How to set the value of one field from workflow and Trigger simultaneously??

Hi,

 

I have a Master object and a Detail object. In the Master object i have a Text field that is getting updated based on the child object records.

 

Also, the same Master field is also getting updated based on the workflow.

 

No, my question is if i have set a value of taht field using the workflow once my workflow condition gets dissatified my the value set by the trigger should get updated..

 

Please let me know how can i do this..

Cory CowgillCory Cowgill

For this, it is critical to understand the order of execution for Triggers and Workflows.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

 

1. Your triggers Before and After triggers will fire first and perform their updates..

2. Your Worklow rule will now fire. If the Workflow Rule evaluates to true, and it performs a field update on the record, your Before and After triggers will execute one more time, BUT ONLY ONE MORE TIME.

 

Based on the order of exeuction, you should be able to figure out how to make your triggers and workflows work appropriately.

 

1. You can use a Static Variable to hold the value whether to execute the Trigger value update. In your Trigger, check to see if this value is true. If it is false, then peform the field update in the trigger.

2. In your after Insert trigger logic, set the Static Variable to true

3. Your workflows rule will now fire and perform an update if appropriate

4. You triggers will fire the second time, but because the Static Variable is set to 'True' the triggers will not update the values in the records.

 

 

Cory CowgillCory Cowgill

For an example of controller Static variables in a trigger,  checkout the Force.com Cookbook Recipes:

 

http://developer.force.com/cookbook/recipe/controlling-recursive-triggers

NANCY1NANCY1

Hi,

 

Thanks for guiding me.. however i am not able tomunderstand how can check for the master field in the trigger of the detail object. In master object i have a field Allocation__c, if this field is blank only then i want the trigger to execute...

 

please suggest...

 

trigger status on Detail__c (after update, after insert, after delete) 
{

 

        List < Id > List1= new List < Id >();
        List < Id > List2 = new List < Id >();

        for ( Detail__c  c: Trigger.New )
        {
            List2.add( c.Id );
            empmasterIds.add( c.EmpCode__c);
        }
   
        /* This will contain the aggregated proposal status of the employee*/
        List<Master__c> opps = [select id, Pstatus__c from Master__c where id in :empmasterIds];
   
        /* Mapping between employeeId and employee*/
        Map < Id ,Master__c > map = new Map < Id , Master__c >();
        for ( Master__c  a : opps   )
        {
            map.put( a.Id, a);
        }

        /* List of employees to be updated. This will have only one employee - the current employee.*/
        List < Master__c > EmpToUpdate = new List < Master__c >();
   
        /* Get the status of the new proposal */
        for(Detail__c c: Trigger.New)
        {
            /* Fetch the current employee */
            Master__c ac = map.get( c.EmpCode__c );        
            if ( ac == null )
            {   
                  continue;
            }
       
            /* If the new proposal's status is not closed, then set the Aggregated proposal status as the status
             of the new Proposal, else mark it as unassigned. */
           
            if (c.Status__c != 'Closed')
            {
               ac.Pstatus__c = c.Status__c;   
               EmpToUpdate.add( ac );
            }
            else
            {
               ac.Pstatus__c = 'Unassigned';
               EmpToUpdate.add( ac );
            }
       
            /* If the new Proposal's status is equal to 'Blocked' or 'Selected' then close rest of the proposals. */
            if((c.Status__c == 'Selected-New') || (c.Status__c == 'Blocked') || (c.Status__c == 'Selected-Extended'))
            {
                system.debug('----------->'+c.Status__c);
                /* Get all proposals for this employee and mark them as closed. */
                List<Detail__c> proObj = [select Id,Status__c,EmpCode__c from Detail__c where Id !=:c.Id and EmpCode__c =:ac.Id ];
                for(Detail__c pc: proObj )
                {
                   system.debug('BEFORE STATUS--->'+pc.Status__c);
         
                   if ((pc.Status__c != 'Closed')
                       && (pc.Status__c != 'Likely Extension')
                       && (pc.Status__c != 'Blocked')
                       && (pc.Status__c != 'Selected-New')
                       && (pc.Status__c != 'Selected-Extended')){            
                       pc.Status__c = 'Closed';
                       system.debug('AFTER STATUS--->'+pc.Status__c);
                       update pc;
                   }//if
                }//for
             }//if
         }//For Trigger.New
         upsert EmpToUpdate;      
     }