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 

urgent: based on the Detail object field how can i update the Master Object field??

Hi,

 

Master Object > Master

Master Object Field > Detail

 

Child Object > Master__c

Child Object Field > Detail__c

 

 

 

 

to meet this requirement i have developed something like, i am able to update the master object field with the detail object field but not with the multiple detail object record. Please help....

 

trigger proposalstatus on Master__c (after insert,after update)
{

   List<Master> opps = new List<Master>();
   List<ID> masterIds = new List<ID>();
   Map<ID, String> childDetailMap = new Map<ID, String>();
  
   for(Master__c c: Trigger.new)
    {
      masterIds.add(c.RMG_Employee_Code__c);
      childDetailMap.put(c.RMG_Employee_Code__c, (c.Detail__c));
    }
    opps = [select id, Detail from Master where id in :masterIds];
 
   for(Master rem: opps)
    {
      rem.Detail = childDetailMap.get(rem.id);
    }
    if(opps.size() > 0)
    {
     Update opps;
     }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
hitzhitz

Hi....

 

Yes It works for multiple detail records just u need to do some change in code...

 

just replace old code with nwe one..........

 

old code

--------------------

If (c.Status__c != 'Closed'){
            
             ac.Proposal_Status__c = c.Status__c;
             EmpToUpdate.add( ac );
            
        }else{
            ac.Proposal_Status__c = '';
            EmpToUpdate.add( ac );
     }

 

 

New  Code

------------------------------

 

If (c.Status__c != 'Closed'){
     ac.Proposal_Status__c = c.Status__c;
     EmpToUpdate.add( ac );
    
}else if(c.Status__c == 'Proposed'){
    ac.Proposal_Status__c = 'AUP';
    EmpToUpdate.add( ac );
}else{
    ac.Proposal_Status__c = '';
    EmpToUpdate.add( ac );
}

 

All Answers

hitzhitz

HI, NANCY
Use Below Trigger code ........

trigger roposalstatus 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 = '';
            EmpToUpdate.add( ac );
        }
    }

    upsert EmpToUpdate;       

}

 

 

Hopes This Will Helps You........... :)

NANCY1NANCY1

Hi,

 

Please ignore my last post,

 

The code has been saved sucessfully.. but while creating the new proposal record i am getting the following error. Please let me know :

 

Force.com Sandbox

 

Apex script unhandled trigger exception by user/organization: 005Q0000000FR7f/00DQ00000009dgV Source organization: 00D80000000Zd3z (null)

roposalstatus: execution of AfterInsert

 

caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements

 

Trigger.roposalstatus: line 42, column 5

hitzhitz

Hi, Nancy

 

Try This one code it works and tested also .......... :)

 

trigger roposalstatus 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);

    }
    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 != 'Closed'){
            
             ac.Proposal_Status__c = c.Status__c;
             EmpToUpdate.add( ac );
            
        }else{
            ac.Proposal_Status__c = '';
            EmpToUpdate.add( ac );
        }
    }

    upsert EmpToUpdate;       

}

NANCY1NANCY1

Hi Hitesh,

 

Now its working fine.. i wanted to ask another important thing..

 

If i have multiple detail records with the same Status as 'Proposed' then Proposal_Status__c should be 'AUP',

 

please let me know how can i achieve that..

hitzhitz

Hi....

 

Yes It works for multiple detail records just u need to do some change in code...

 

just replace old code with nwe one..........

 

old code

--------------------

If (c.Status__c != 'Closed'){
            
             ac.Proposal_Status__c = c.Status__c;
             EmpToUpdate.add( ac );
            
        }else{
            ac.Proposal_Status__c = '';
            EmpToUpdate.add( ac );
     }

 

 

New  Code

------------------------------

 

If (c.Status__c != 'Closed'){
     ac.Proposal_Status__c = c.Status__c;
     EmpToUpdate.add( ac );
    
}else if(c.Status__c == 'Proposed'){
    ac.Proposal_Status__c = 'AUP';
    EmpToUpdate.add( ac );
}else{
    ac.Proposal_Status__c = '';
    EmpToUpdate.add( ac );
}

 

This was selected as the best answer
anishaanisha

Hi all,

 

i am getting the same error like you can any one help me out?

 

Error that i am getting is 

 

System.ListException: Before Insert or Upsert list must not have two identically equal elements Trigger.UpdateCustomAsset: line 86...

Apex script unhandled trigger exception by user/organization: 00570000001ED9M/00D700000008W5q

UpdateCustomAsset: execution of AfterInsert

caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements

Trigger.UpdateCustomAsset: line 86, column 1

anishaanisha

This is my code

 

 

trigger UpdateCustomAsset on Asset(after insert,after update,after delete) {
List<Asset> stdAsset=new List<Asset>();
List<Asset__c> custAsset=new List<Asset__c>();
Asset__c custAsset1=new Asset__c();
if(updationAsset.deletionFlag==false)
{
if(trigger.isDelete)
{
updationAsset.deletionFlag=true;
List<Asset> seldelAsset=Trigger.old;
List<Id> sid=new List<Id>();
integer i;
for(i=0;i<seldelAsset.size();i++)
{
sid.add(seldelAsset[i].id);
}
List<Asset__c> existingCustomAsset=[select id,account__c,Product_ID__c from Asset__c where Product_ID__c in :sid];
 for(Asset sa:trigger.old)
    {
        for(Asset__c ca:existingCustomAsset)
        {
        //system.debug('Update Trigger1'+'Standard Asset'+sa.name+'Custom Asset'+ca.Name);
            if(sa.id==ca.Product_ID__c)
            {
            custAsset.add(ca);
            system.debug('Custom Asset Id'+ca.id);
            }
         }
     }       
Delete custAsset;   

}





if(UpdationAsset.insertFlag==false){
if(trigger.isInsert)
{
    UpdationAsset.insertFlag=true;
    for(Asset a: trigger.new)
    {    
    System.debug('insertion');
    custAsset1.Account__c=a.AccountId;    
   // custAsset1.Contact__c=a.ContactId;    
    custAsset1.Product_ID__c=a.id;
    custAsset1.nickname__c=a.nickname__c;
    custAsset1.Asset_Name__c=a.Name;
    custAsset1.Application_Server_OS__c=a.Application_Server_OS__c;
    custAsset1.Database__c=a.Database__c;
    custAsset1.Database_Server_OS__c=a.Database_Server_OS__c;
    //stdAsset1.External_Asset_Name__c=a.External_Asset_Name__c;
    custAsset1.Front_Back_Access__c=a.Front_Back_Access__c;
    custAsset1.Managed_Hosting__c=a.Managed_Hosting__c;
    custAsset1.Installed_Product_ID__c=a.Installed_Product_ID__c;
    custAsset1.JDK_Version__c=a.JDK_Version__c;
    custAsset1.Network__c=a.Network__c;
    custAsset1.Platform__c=a.Platform__c;
    custAsset1.Software_URL__c=a.Software_URL__c;
    custAsset1.Version_Build__c=a.Version_Build__c;
    
    custAsset1.Product__c =  a.Product2.Description;
    custAsset1.Type__c =  a.Type__c;
    custAsset1.Psft_Product_Id__c = a.Psft_Product_Id__c;
    custAsset1.License_Expiration_Date__c = a.License_Expiration_Date__c;
    custAsset1.External_Description__c = a.External_Description__c;    
    custAsset1.Core_License__c = a.Core_License__c;    
    custAsset1.Version__c = a.Version__c;    
    custAsset1.Version_Effective_Date__c = a.Version_Effective_Date__c;    
    custAsset1.Serial_Number__c = a.SerialNumber;    
    custAsset1.Comments__c = a.Comments__c;    
    custAsset1.Status__c = a.Status;    
    custAsset1.Quantity__c = a.Quantity;    
    custAsset1.Install_Date__c = a.InstallDate;    
    custAsset1.Date_Registered__c = a.Date_Registered__c;    
    custAsset1.Customer_Value__c = a.Customer_Value__c;    
    custAsset1.I_and_C_support__c = a.I_and_C_support__c;    
    custAsset1.Support_Level__c = a.Support_Level__c;    
    custAsset1.Upgraded__c = a.Upgraded__c;    
    custAsset1.Days_to_Expiration__c = a.Days_to_Expiration__c;    
    custAsset1.Upgrade_Flag__c = a.Upgrade_Flag__c;    
    custAsset1.Last_Upgrade_Date__c = a.Last_Upgrade_Date__c;    
        custAsset.add(custAsset1);
    }
    insert custAsset;
    
    //UpdationAsset.insertFlag=true;
    }
    }
    if(UpdationAsset.updateFlag==false)
    {
if(trigger.isUpdate)
{
UpdationAsset.updateFlag=true;
system.debug('Update Trigger');
List<Asset> selAsset=Trigger.new;
List<Id> sid=new List<Id>();
integer i;
for(i=0;i<selAsset.size();i++)
{
sid.add(selAsset[i].id);
}
List<Asset__c> existingCustomAsset=[select id,account__c,Product_ID__c,Name,Application_Server_OS__c,Database__c,
Database_Server_OS__c,External_Asset_Name__c,Front_Back_Access__c,Managed_Hosting__c,Installed_Product_ID__c,
JDK_Version__c,Network__c,Nickname__c,Platform__c,Software_URL__c,Version_Build__c from Asset__c where Product_ID__c in :sid];
    for(Asset sa:trigger.new)
    {
        for(Asset__c ca:existingCustomAsset)
        {
        system.debug('Update Trigger1'+'Standard Asset'+sa.name+'Custom Asset'+ca.Name);
            if(sa.id==ca.Product_ID__c)
            {
            //sp.id=sa.id;            
            ca.Account__c=sa.accountId;           
           // ca.Contact__c=sa.contactId;            
            ca.Asset_Name__c=sa.Name;
            ca.Nickname__c=sa.NickName__c;
            ca.Application_Server_OS__c=sa.Application_Server_OS__c;
            ca.Database__c=sa.Database__c;
            ca.Database_Server_OS__c=sa.Database_Server_OS__c;
            //stdAsset1.External_Asset_Name__c=a.External_Asset_Name__c;
            ca.Front_Back_Access__c=sa.Front_Back_Access__c;
            ca.Managed_Hosting__c=sa.Managed_Hosting__c;
            ca.Installed_Product_ID__c=sa.Installed_Product_ID__c;
            ca.JDK_Version__c=sa.JDK_Version__c;
            ca.Network__c=sa.Network__c;
            ca.Platform__c=sa.Platform__c;
            ca.Software_URL__c=sa.Software_URL__c;
            ca.Version_Build__c=sa.Version_Build__c;
            
    ca.Product__c =  sa.Product2.Description;
    ca.Type__c =  sa.Type__c;
    ca.Psft_Product_Id__c = sa.Psft_Product_Id__c;
    ca.License_Expiration_Date__c = sa.License_Expiration_Date__c;
    ca.External_Description__c = sa.External_Description__c;    
    ca.Core_License__c = sa.Core_License__c;    
    ca.Version__c = sa.Version__c;    
    ca.Version_Effective_Date__c = sa.Version_Effective_Date__c;    
    ca.Serial_Number__c = sa.SerialNumber;    
    ca.Comments__c = sa.Comments__c;    
    ca.Status__c = sa.Status;    
    ca.Quantity__c = sa.Quantity;    
    ca.Install_Date__c = sa.InstallDate;    
    ca.Date_Registered__c = sa.Date_Registered__c;    
    ca.Customer_Value__c = sa.Customer_Value__c;    
    ca.I_and_C_support__c = sa.I_and_C_support__c;    
    ca.Support_Level__c = sa.Support_Level__c;    
    ca.Upgraded__c = sa.Upgraded__c;    
    ca.Days_to_Expiration__c = sa.Days_to_Expiration__c;    
    ca.Upgrade_Flag__c = sa.Upgrade_Flag__c;    
    ca.Last_Upgrade_Date__c = sa.Last_Upgrade_Date__c;    
            custAsset.add(ca);
            system.debug('Update Trigger2');

            }
        }
    }
    //system.debug('Update Trigger');

    update custAsset;
   //UpdationAsset.updateFlag=true;     
    
}
}
}