You need to sign in to do that
Don't have an account?
NANCY1
How to update all the Detail Object Records??
Hi,
Master Object > RMG_Employee_Master__c
Detail object > Proposal__c
Detail Object Field > Status__c
Under this master object, I have number of detail object records.
The functionality is something like when I create a new record in the Detail Object where Status__c is not equal to ‘Proposed’, than for all the other records [already created] Status__c should become ‘Closed’.
Please let me know is this can be achieved?? Thanks
HI, Nancy
If i am not wrong then you just want to update all other Child records status value of particular Master Object right .... ?
if Yes then i have done some changes in previous Trigger code ..... Hopes this time it satisfiy your requirement......
New Trigger Code ..........
trigger roposalstatus on Proposal__c (after insert) {
List < Id > empmasterIds = new List < Id >();
List < Id > proposalIds = new List < Id >();
for ( Proposal__c c: Trigger.New ) {
proposalIds.add( c.Id );
empmasterIds.add( c.RMG_Employee_Code__c);
}
System.debug('----------> '+empmasterIds.size());
List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();
for ( RMG_Employee_Master__c a : opps ) {
empmap.put( a.Id, a);
}
List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
for(Proposal__c c: Trigger.New) {
RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );
if ( ac == null ) {
continue;
}
if(c.Status__c != 'Proposed'){
System.debug('--------- REG EMP Master ID :- '+ ac.Id);
System.debug('--------- New Proposal ID :- '+ c.Id);
List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id ];
System.debug('--------- Proposal size : '+ proObj.size());
for(Proposal__c pc: proObj ){
pc.Status__c = 'Closed';
update pc;
}
}
/*If (c.Status__c != 'Closed'){
ac.Proposal_Status__c = c.Status__c;
EmpToUpdate.add( ac );
}else{
ac.Proposal_Status__c = '';
EmpToUpdate.add( ac );
}*/
}
// upsert EmpToUpdate;
}
All Answers
HI, Nancy
If i am not wrong then you just want to update all other Child records status value of particular Master Object right .... ?
if Yes then i have done some changes in previous Trigger code ..... Hopes this time it satisfiy your requirement......
New Trigger Code ..........
trigger roposalstatus on Proposal__c (after insert) {
List < Id > empmasterIds = new List < Id >();
List < Id > proposalIds = new List < Id >();
for ( Proposal__c c: Trigger.New ) {
proposalIds.add( c.Id );
empmasterIds.add( c.RMG_Employee_Code__c);
}
System.debug('----------> '+empmasterIds.size());
List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();
for ( RMG_Employee_Master__c a : opps ) {
empmap.put( a.Id, a);
}
List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
for(Proposal__c c: Trigger.New) {
RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );
if ( ac == null ) {
continue;
}
if(c.Status__c != 'Proposed'){
System.debug('--------- REG EMP Master ID :- '+ ac.Id);
System.debug('--------- New Proposal ID :- '+ c.Id);
List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id ];
System.debug('--------- Proposal size : '+ proObj.size());
for(Proposal__c pc: proObj ){
pc.Status__c = 'Closed';
update pc;
}
}
/*If (c.Status__c != 'Closed'){
ac.Proposal_Status__c = c.Status__c;
EmpToUpdate.add( ac );
}else{
ac.Proposal_Status__c = '';
EmpToUpdate.add( ac );
}*/
}
// upsert EmpToUpdate;
}
Hi Hitesh,
based on your code.. i am using the below code to fulfill my requirements, but i just wanted to know if i try uploading the detail object records data using the Data Loader.. i use to get the following error:
proposalstatus: execution of AfterUpdate
caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements
Trigger.proposalstatus: line 53, column 2
trigger proposalstatus on Proposal__c (after insert,after update)
{
List < Id > empmasterIds = new List < Id >();
List < Id > proposalIds = new List < Id >();
for ( Proposal__c c: Trigger.New )
{
proposalIds.add( c.Id );
empmasterIds.add( c.RMG_Employee_Code__c);
}
List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();
for ( RMG_Employee_Master__c a : opps )
{
empmap.put( a.Id, a);
}
List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
for(Proposal__c c: Trigger.New)
{
RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );
if ( ac == null )
{
continue;
}
If (c.Status__c != 'Closed')
{
ac.Proposal_Status__c = c.Status__c;
EmpToUpdate.add( ac );
}
else
{
ac.Proposal_Status__c = 'Unassigned';
EmpToUpdate.add( ac );
}
if(c.Status__c == 'Selected' || c.Status__c == 'Blocked' || c.Status__c == 'Awaiting Background Check')
{
List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id ];
for(Proposal__c pc: proObj )
{
pc.Status__c = 'Closed';
update pc;
}
}
}
upsert EmpToUpdate;
}
Hi, NANCY
u have written wrong code see...
// upsert EmpToUpdate;
this code was in commented....
and now check it again....
Hi Hitesh,
If i'll comment out the
// upsert EmpToUpdate;
then how will i update my Proposal_Status__c field ??
HI, NANCY
let me Know What Excetly you want to do ?? are u want to update other child records or only want to update parent records...
plz clear about ur Question...