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
abinayaabinaya 

workflow field update and trigger functionality

In my custom object i have 7 status fields. All these are pick-list fields.
 
If the user update first status field, status message will be updated in the x field. I am doing this using trigger before insert and before update.
 
If the status message is approved and business day is 5(Last Modified date +7), I have to update the next pick-list field to "No" and update the date. I am able to achieve this using workflow field update.
 
Now based on the field update, i have to update the status message field(x) using trigger.
 
This is working fine for first record of the object. For the upcoming records the field (status message) is not updated using trigger where as the field update is happening correctly.
 
Kindly advice.
Best Answer chosen by Admin (Salesforce Developers) 
Marko LamotMarko Lamot

Hello,

 

this statement takes only first record (when you do must update):

 

Candidate_Mapping__c ac = Trigger.new[0];   // it takes only first record 

if(ac.Status1__c == 'Applied')

 

.....

 

you should use for to proccess all updating/inserting recorda

 

for (Candidate_Mapping__c ac:Trigger.new)

{

 

if(ac.Status1__c == 'Applied')

 

....

....

 

 

}

 

 

All Answers

Marko LamotMarko Lamot

post the trigger code

abinayaabinaya

trigger statusupdate on Candidate_Mapping__c (before insert, before update)
{

if(Trigger.isUpdate)
{
Candidate_Mapping__c ac = Trigger.new[0];
system.debug('ac:'+ ac);

Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;


system.debug('newContactMap:' + newContactMap );
system.debug('oldContactMap:' + newContactMap );

if(ac.Status1__c == 'Applied')
{
ac.R_Email__c = ac.R_email_id__c;
ac.Requirement_Owner_Email__c = ac.Req_Owner_Email__c;
ac.Manager_Email_ID__c = ac.LR_Email_ID__c;
}

if (ac.LR_Status__c == 'Approved' )
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.LR_Status__c == 'Rejected')
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Approved')
{
//
for(Id contactId:newContactMap.keySet())
{
//Loop through the map
Candidate_Mapping__c myNewContact = newContactMap.get(contactId);
Candidate_Mapping__c myOldContact = oldContactMap.get(contactId);
if (myNewContact.MR_Status__c <> myOldContact.MR_Status__c)
{
ac.MR_Status1__c = 'Approved';

}
else
{
ac.MR_Status1__c = 'None';
}
}
//
//ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Rejected')
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
}

for(Id candidateId :newContactMap.keySet())
{
//Loop through the map
Candidate_Mapping__c myNewContact1 = newContactMap.get(candidateId );
Candidate_Mapping__c myOldContact1 = oldContactMap.get(candidateId );
if (myNewContact1.Status1__c == myOldContact1.Status1__c)
{
if (ac.Submitted_to_Client__c == 'Yes')
{
ac.status1__c = 'Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Submitted_to_Client__c == 'No')
{
ac.status1__c = 'Not Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Interview_Scheduled__c == 'Yes')
{
ac.status1__c = 'Interview Requested';
ac.Priority__c = 'b';
}

if (ac.Interview_Scheduled__c == 'No')
{
ac.status1__c = 'Not Shortlisted For Interview';
ac.Priority__c = 'b';
}

if (ac.Interview_Accepted__c == 'Yes')
{
ac.status1__c = 'Interview Accepted';
ac.Priority__c = 'c';
}

if (ac.Interview_Accepted__c == 'No')
{
ac.status1__c = 'Candidate Not Available';
ac.Priority__c = 'c';
}

if (ac.Client_Interviewed__c == 'Yes')
{
ac.status1__c = 'Client Interviewed';
ac.Priority__c = 'd';
}

if (ac.Client_Interviewed__c == 'No')
{
ac.status1__c = 'Client Did Not Interview';
ac.Priority__c = 'd';
}

if (ac.Client_Offered__c == 'Yes')
{
ac.status1__c = 'Client Offered';
ac.Priority__c = 'e';
}

if (ac.Client_Offered__c == 'No')
{
ac.status1__c = 'Interviewed and rejected';
ac.Priority__c = 'e';
}

if (ac.Candidate_started__c == 'Yes')
{
ac.status1__c = 'Candidate started';
ac.Priority__c = 'f';
}

if (ac.Candidate_started__c == 'No')
{
ac.status1__c = 'Candidate did not start';
ac.Priority__c = 'f';
}

}
}
system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c);
system.debug('MR_Status1__c:' + ac.MR_Status1__c );


}

if(Trigger.isInsert)
{
Candidate_Mapping__c ac = Trigger.new[0];
system.debug('ac:'+ ac);

//Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
//Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;


system.debug('LRStatus:' + ac.LR_Status__c);
system.debug('MRStatus:' + ac.MR_Status__c);

if(ac.Status1__c == 'Applied')
{
ac.R_Email__c = ac.R_email_id__c;
ac.Requirement_Owner_Email__c = ac.Req_Owner_Email__c;
ac.Manager_Email_ID__c = ac.LR_Email_ID__c;
}

if (ac.LR_Status__c == 'Approved')
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.LR_Status__c == 'Rejected')
{

ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Approved')
{
ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Rejected')
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
}

/* if (ac.Submitted_to_Client__c == 'Yes')
{
ac.status1__c = 'Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Interview_Scheduled__c == 'Yes')
{
ac.status1__c = 'Interview Requested';
ac.Priority__c = 'b';
}

if (ac.Interview_Accepted__c == 'Yes')
{
ac.status1__c = 'Interview Accepted';
ac.Priority__c = 'c';
}

if (ac.Client_Interviewed__c == 'Yes')
{
ac.status1__c = 'Client Interviewed';
ac.Priority__c = 'd';
}

if (ac.Client_Offered__c == 'Yes')
{
ac.status1__c = 'Client Offered';
ac.Priority__c = 'e';
}

if (ac.Candidate_started__c == 'Yes')
{
ac.status1__c = 'Candidate started';
ac.Priority__c = 'f';
}


system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c); */

}

 


}

Marko LamotMarko Lamot

Hello,

 

this statement takes only first record (when you do must update):

 

Candidate_Mapping__c ac = Trigger.new[0];   // it takes only first record 

if(ac.Status1__c == 'Applied')

 

.....

 

you should use for to proccess all updating/inserting recorda

 

for (Candidate_Mapping__c ac:Trigger.new)

{

 

if(ac.Status1__c == 'Applied')

 

....

....

 

 

}

 

 

This was selected as the best answer
abinayaabinaya

tried this too still the first record is updating. Please advise.

Marko LamotMarko Lamot

please post whole  trigger with "for (Candidate_Mapping__c ac:Trigger.new)" approach.

abinayaabinaya

trigger statusupdate on Candidate_Mapping__c (before insert, before update)
{

if(Trigger.isUpdate)
{
// Candidate_Mapping__c ac = Trigger.new[0];
for(Candidate_Mapping__c ac : Trigger.new)
{
system.debug('ac:'+ ac);

Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;


system.debug('newContactMap:' + newContactMap );
system.debug('oldContactMap:' + newContactMap );

if(ac.Status1__c == 'Applied')
{
ac.R_Email__c = ac.R_email_id__c;
ac.Requirement_Owner_Email__c = ac.Req_Owner_Email__c;
ac.Manager_Email_ID__c = ac.LR_Email_ID__c;
}

if (ac.LR_Status__c == 'Approved' )
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.LR_Status__c == 'Rejected')
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Approved')
{
//
for(Id contactId:newContactMap.keySet())
{
//Loop through the map
Candidate_Mapping__c myNewContact = newContactMap.get(contactId);
Candidate_Mapping__c myOldContact = oldContactMap.get(contactId);
if (myNewContact.MR_Status__c <> myOldContact.MR_Status__c)
{
ac.MR_Status1__c = 'Approved';

}
else
{
ac.MR_Status1__c = 'None';
}
}
//
//ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Rejected')
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
}

/* for(Id candidateId :newContactMap.keySet())
{
//Loop through the map
Candidate_Mapping__c myNewContact1 = newContactMap.get(candidateId );
Candidate_Mapping__c myOldContact1 = oldContactMap.get(candidateId );
if (myNewContact1.Status1__c == myOldContact1.Status1__c)
{ */
if (ac.Submitted_to_Client__c == 'Yes')
{
ac.status1__c = 'Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Submitted_to_Client__c == 'No')
{
ac.status1__c = 'Not Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Interview_Scheduled__c == 'Yes')
{
ac.status1__c = 'Interview Requested';
ac.Priority__c = 'b';
}

if (ac.Interview_Scheduled__c == 'No')
{
ac.status1__c = 'Not Shortlisted For Interview';
ac.Priority__c = 'b';
}

if (ac.Interview_Accepted__c == 'Yes')
{
ac.status1__c = 'Interview Accepted';
ac.Priority__c = 'c';
}

if (ac.Interview_Accepted__c == 'No')
{
ac.status1__c = 'Candidate Not Available';
ac.Priority__c = 'c';
}

if (ac.Client_Interviewed__c == 'Yes')
{
ac.status1__c = 'Client Interviewed';
ac.Priority__c = 'd';
}

if (ac.Client_Interviewed__c == 'No')
{
ac.status1__c = 'Client Did Not Interview';
ac.Priority__c = 'd';
}

if (ac.Client_Offered__c == 'Yes')
{
ac.status1__c = 'Client Offered';
ac.Priority__c = 'e';
}

if (ac.Client_Offered__c == 'No')
{
ac.status1__c = 'Interviewed and rejected';
ac.Priority__c = 'e';
}

if (ac.Candidate_started__c == 'Yes')
{
ac.status1__c = 'Candidate started';
ac.Priority__c = 'f';
}

if (ac.Candidate_started__c == 'No')
{
ac.status1__c = 'Candidate did not start';
ac.Priority__c = 'f';
}

// }
//}
system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c);
system.debug('MR_Status1__c:' + ac.MR_Status1__c );

}
}

if(Trigger.isInsert)
{
//Candidate_Mapping__c ac = Trigger.new[0];
for(Candidate_Mapping__c ac : Trigger.new)
{
system.debug('ac:'+ ac);

//Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
//Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;


system.debug('LRStatus:' + ac.LR_Status__c);
system.debug('MRStatus:' + ac.MR_Status__c);

if(ac.Status1__c == 'Applied')
{
ac.R_Email__c = ac.R_email_id__c;
ac.Requirement_Owner_Email__c = ac.Req_Owner_Email__c;
ac.Manager_Email_ID__c = ac.LR_Email_ID__c;
}

if (ac.LR_Status__c == 'Approved')
{
ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.LR_Status__c == 'Rejected')
{

ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Approved')
{
ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
}

if (ac.MR_Status__c == 'Rejected')
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
}

/* if (ac.Submitted_to_Client__c == 'Yes')
{
ac.status1__c = 'Submitted to Client';
ac.Priority__c = 'a';
}

if (ac.Interview_Scheduled__c == 'Yes')
{
ac.status1__c = 'Interview Requested';
ac.Priority__c = 'b';
}

if (ac.Interview_Accepted__c == 'Yes')
{
ac.status1__c = 'Interview Accepted';
ac.Priority__c = 'c';
}

if (ac.Client_Interviewed__c == 'Yes')
{
ac.status1__c = 'Client Interviewed';
ac.Priority__c = 'd';
}

if (ac.Client_Offered__c == 'Yes')
{
ac.status1__c = 'Client Offered';
ac.Priority__c = 'e';
}

if (ac.Candidate_started__c == 'Yes')
{
ac.status1__c = 'Candidate started';
ac.Priority__c = 'f';
}


system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c); */

}


}


}