+ Start a Discussion
justynjustyn 

Auto Update a field on a different object

I want to be able to create a workflow so that when one of our contacts is marked as inactive, a field in a related custom object is updated to an inactive status as well. I can create all the workflow apart from the field update as it will not allow me access to that objects field. There is a Master-Detail relationship.

 

Is there a way this can be done?

 

Thanks.

 

Justyn

skodisanaskodisana

Hi,

 

There is no way to update the Child records from Parent record using Workflow.

You have to Write the before update trigger on Contact object which will query and update the Child object records with the Inactive Status.

 

Thanks,

Kodisana

Shashikant SharmaShashikant Sharma

If you are asking is there a way to achieve this using workflow no thers is no way using workflow as you can not update any other field except the field on the object it self on which the field update is created.

But Yes there is a Way that is trigger. If you want  I can help you in writting trigger for this. 

justynjustyn

Hi 

 

Thanks for the helpful link and comments. Any help on writing the trigger would be appreciated. The scenario is:

 

  1. Two objects - Contacts and Counselling Practice (custom object counselling_pratice__c).
  2. Contacts field called membership_status__c which is a drop down list containing the options of Active, Lapsed, Deceased, Sanctioned.
  3. Counselling Practice field called Currently_Active__c which is a tick box.
  4. There is a master detail relationship between the Contact and the Counselling Practice objects records.
  5. When a Contacts membership status moves from active to either Lapsed, Deceased or Sanctioned, I need Salesforce to automatically change the Currently_Active__c field from checked to unchecked.

What would be trigger coding be?

 

Many thanks

 

Justyn

VPrakashVPrakash

try this sample code,

 

trigger updateactive on contact (after update){

counselling_pratice__c[] cpstoupdate = new counselling_pratice__c[]{};

for(contact c : trigger.new ){

for(counselling_pratice__c cp: [ select id,name,Currently_Active__c from counselling_pratice__c  wherecontact=:c.id]){

if ( c.membership_status__c != Active && cp.Currently_Active__c ){

cp.Currently_Active__c == false;

}

cpstoupdate.add(cp);

}

}

if(cpstoupdate.size()>0) 

update cpstoupdate;

}

 

 

If you need any more info, let me know.

 

-VPrakash

justynjustyn

Hi

 

Thanks for the help. I've copied and pasted the code in but there is a error message of:

 


Error: Compile Error: expecting right square bracket, found '=' at line 4 column 111

 

Any thoughts?

 

Justyn

VPrakashVPrakash

 

What is the relationship field in counselling_pratice__c?, if it is contact__c thenmodify the code as follows:

 

trigger updateactive on contact (after update){

counselling_pratice__c[] cpstoupdate = new counselling_pratice__c[]{};

for(contact c : trigger.new ){

for(counselling_pratice__c cp: [ select id,name,Currently_Active__c from counselling_pratice__c  where Contact__c =:c.id] ){

if ( c.membership_status__c != Active && cp.Currently_Active__c ){

cp.Currently_Active__c == false;

}

cpstoupdate.add(cp);

}

}

if(cpstoupdate.size()>0) 

update cpstoupdate;

}

justynjustyn

Hi

 

Thanks for the update. The relationship field on the Counselling_Practice__c  is Member__c. I've amended the code, and also correct a typo in the Object name I realised that I'd original typed Counselling_Pratice__c not Counselling_Practice__c . The latest code therefore is:

 

}

trigger updateactive on contact (after update){

Counselling_Practice__c[] cpstoupdate = new Counselling_Practice__c[]{};

for(contact c : trigger.new ){

for(Counselling_Practice__c cp: [ select id,name,Currently_Active__c from Counselling_Practice__c  where Member__c =:c.id] ){

if (c.Membership_Status__c != Active && cp.Currently_Active__c ){

cp.Currently_Active__c == false;

}

cpstoupdate.add(cp);

}

}

if(cpstoupdate.size()>0)

update cpstoupdate;

 

I now get a slightly different error message which is:

 

 

Error: Compile Error: Variable does not exist: Active at line 5 column 31

 

I can confirm that the value does exist. The field is a picklist which I don't know if that has an impact. The list of values in that field are:



Active
Lapsed
Sanction
Withdrawn
Deceased
Shashikant SharmaShashikant Sharma

Here is your trigger

trigger updateCounsellingStatus on contact (after update)
{
Set<Id> setconIds = new Set<Id>();
for(contact c : trigger.new )
{
    setconIds.add(c.id);
}
MAP<ID , List<counselling_pratice__c>> mapCons = new Map<ID , List<counselling_pratice__c>>();

List<counselling_pratice__c> consList = [select Currently_Active__c from counselling_pratice__c  where Member__c =:c.id]
List<counselling_pratice__c> tempList = new List<counselling_pratice__c>();
for(counselling_pratice__c consObj : consList)
{
if(mapCons.ContainsKey(consObj.Member__c))
{
tempList = mapCons.get(consObj.Member__c);
}
else
{
tempList = new List<counselling_pratice__c>();
}
tempList.add(consObj);
mapCons.put(consObj.Member__c , tempList);
}

for(contact c : trigger.new )
{
 if(mapCons.ContainsKey(c.id))
 {
   for(counselling_pratice__c conObj : mapCons.get(c.id))
   {
     if ( (c.membership_status__c == 'Lapsed' || c.membership_status__c == 'Deceased') && trigger.oldMap.get(c.id).membership_status__c == 'Active' && conObj.Currently_Active__c )
     {
       conObj.Currently_Active__c = false;
      }
      cpstoupdate.add(conObj);
    }
  }
}
if(cpstoupdate.size()>0) 
update cpstoupdate;
}

 I have covered bulk handling as well in this

VPrakashVPrakash

My bad,

 

 This should work,

 

trigger updateactive on contact (after update){

Counselling_Practice__c[] cpstoupdate = new Counselling_Practice__c[]{};

for(contact c : trigger.new ){

for(Counselling_Practice__c cp: [ select id,name,Currently_Active__c from Counselling_Practice__c  where Member__c =:c.id] ){

if (c.Membership_Status__c != 'Active' && cp.Currently_Active__c ){

cp.Currently_Active__c == false;

}

cpstoupdate.add(cp);

}

}

if(cpstoupdate.size()>0)

update cpstoupdate;

}

justynjustyn

Hi

 

Thanks for the code. I've copied and pasted by I get the following error:

 


Error: Compile Error: unexpected token: 'List' at line 11 column 0

 

I've also amended the Custom Object name as I'd originally given an incorrect name as I'd missed out a 'c' in practice.

 

The amended code is:

 

}

 

trigger updateCounsellingStatus on contact (after update)

{

Set<Id> setconIds = new Set<Id>();

for(contact c : trigger.new )

{

    setconIds.add(c.id);

}

MAP<ID , List<counselling_practice__c>> mapCons = new Map<ID , List<counselling_practice__c>>();

 

List<counselling_practice__c> consList = [select Currently_Active__c from counselling_practice__c  where Member__c =:c.id]

List<counselling_practice__c> tempList = new List<counselling_practice__c>();

for(counselling_practice__c consObj : consList)

{

if(mapCons.ContainsKey(consObj.Member__c))

{

tempList = mapCons.get(consObj.Member__c);

}

else

{

tempList = new List<counselling_practice__c>();

}

tempList.add(consObj);

mapCons.put(consObj.Member__c , tempList);

}

 

for(contact c : trigger.new )

{

 if(mapCons.ContainsKey(c.id))

 {

   for(counselling_practice__c conObj : mapCons.get(c.id))

   {

     if ( (c.membership_status__c == 'Lapsed' || c.membership_status__c == 'Deceased') && trigger.oldMap.get(c.id).membership_status__c == 'Active' && conObj.Currently_Active__c )

     {

       conObj.Currently_Active__c = false;

      }

      cpstoupdate.add(conObj);

    }

  }

}

if(cpstoupdate.size()>0)

update cpstoupdate;

 

justynjustyn

Hi VPrakash

 

I get the following error now with your amended code:

 

 

Error: Compile Error: Expression cannot be a statement at line 6 column 1
VPrakashVPrakash

Use cp.Currently_Active__c =false; instead of cp.Currently_Active__c == false;

Shashikant SharmaShashikant Sharma

Did you tried my code , that covers bulk handling as well. And if you will not do bulk handling then your trigger will fail with bulk data. Just try that one.

justynjustyn

Hi Shashikant

 

I did try your code thanks. I got the following error when I tried to save the file:

 

Error: Compile Error: unexpected token: 'List' at line 11 column 0

 

Any thoughts?

 

Regards

 

Justyn

justynjustyn

Hi VPrakash

 

Thanks for the amendment. The code works. Have you any thoughts on the Test code so that it can be deployed to the production unit?

 

Regards

 

Justyn