• Mariam Ibrahim 10
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 14
    Replies
Hi there,
I am working on a code  to sum up value in a custom field  'Total_Months_in_this_Position__c' of a custom object 
'TargetX_SRMb__Family_Relationship__c' and add the sum to a custom field 'Total_Months_Employed__c' of a related custom object 'TargetX_SRMb__Application__c'. 
One 'TargetX_SRMb__Application__c' can have multiple TargetX_SRMb__Family_Relationship__c. The relationship is a lookup.

Below is the code I wrote, I am having a hard time fixing the error on the last line.
Any help will be very much appreciated.


trigger CalcTotalMonths on TargetX_SRMb__Family_Relationship__c (After insert, After Update) {
    // gets a set of all application ids and application name with relationship
    Set<Id>applicationIds = new Set<Id>();   
    Map<Id,TargetX_SRMb__Application__c>appToUpdate =new Map<Id,TargetX_SRMb__Application__c>();
    Map<decimal,decimal>monthsWorked = new Map<decimal,decimal>();
    
    // collect the application ids of all new relationships with employer name
    if(trigger.isInsert){
    for (TargetX_SRMb__Family_Relationship__c Rel : Trigger.new)
    if(!string.isblank(Rel.TargetX_SRMb__Employer__c) && !string.isBlank(Rel.TargetX_SRMb__Contact__c)
      
      && (Rel.TargetX_SRMb__Relationship__c == 'Current Employer' || Rel.TargetX_SRMb__Relationship__c == 'Previous Employer'))
       
    {applicationIds.add(Rel.TargetX_SRMb__Application__c);
        }
        
        // collect the application ids of all updated relationships with employer name
      if(Trigger.isUpdate){
        
      for(TargetX_SRMb__Family_Relationship__c Rel : Trigger.new)
        {
     if((Trigger.oldMap.get(Rel.id).Total_Months_in_this_Position__c != Trigger.newMap.get(Rel.id).Total_Months_in_this_Position__c) 
      && !string.isBlank(Rel.TargetX_SRMb__Contact__c)
      && (Rel.TargetX_SRMb__Relationship__c == 'Current Employer' || Rel.TargetX_SRMb__Relationship__c == 'Previous Employer') )
            {
            Applicationids.add(Rel.TargetX_SRMb__Application__c); 
            }
        }
        
        //   Use aggregate query to get the total months worked for each relationship
          
          for(AggregateResult result :[Select Count(id) Total,SUM(Total_Months_in_this_Position__c) Months , TargetX_SRMb__Application__c FROM TargetX_SRMb__Family_Relationship__c
                WHERE TargetX_SRMb__Application__c IN :Applicationids GROUP BY TargetX_SRMb__Application__c               
                                      ])
          {  Decimal Months = (Decimal)result.get('Months');
             Decimal Total = (Decimal)result.get('Total');
           System.debug('Total Months Worked'+ 'Total_Months_in_this_Position__c' );
           Decimal field = monthsWorked.get(Months);
           if ( field != null)
               {
               TargetX_SRMb__Application__c app = appToUpdate.get((id)result.get('TargetX_SRMb__Application__c')); 
               app.Total_Months_Employed__c =  field;
               app.put(Total,field);
               }
          }

       
              }
      

}
}

Thanks,
Mariam
Hi,
Please can anyone help me  with how to merge these two codes into one?
I wrote the following  codes separately  for same the custom object, and I dont know how to consolidate them into one code
. Any help will be appreciated !

1st Code: 
trigger SetContactOnAssetRecord on Assets_Check_in_Checkout__c (before update,before insert) {
    set<string>Netid = new Set<string>();
    for (Assets_Check_in_Checkout__c a  : Trigger.new){
       Netid.add(a.GU_Net_ID__c) ;
        
    }
    List<Contact> ContactList = [ select id, GU_Net_ID__c from Contact where GU_Net_ID__c IN :Netid ];
    Map<string,Contact> netidToContactMap= new Map <string,Contact>();
    
    for (Contact c : ContactList){
        
      netidToContactMap.put(c.GU_Net_ID__c,c);  
        
    }
    for(Assets_Check_in_Checkout__c chk : Trigger.new ){
        if(chk.GU_Net_ID__c != null && netidToContactMap.containsKey(chk.GU_Net_ID__c) ){
            system.debug('Contact id:' + netidToContactMap.get(chk.GU_Net_ID__c).id);
            chk.Loaner__c = netidToContactMap.get(chk.GU_Net_ID__c).id;
        }
        else
        {
           chk.addError('Netid not exist in Salesforce');
 
        }
    }
    
}


2nd Code:

trigger UpdateStatus on Assets_Check_in_Checkout__c (after insert, after update) {
 // Create a list of assetids
    List<Id> assetIds = New List<ID>();
    set<id> assetName = new set<id>();
    Map<id,id> Assetid = new Map<id,id>();
    List<Asset> LAsset = new List<Asset>();
    
    if(Trigger.isInsert){
        for(Assets_Check_in_Checkout__c AssID : Trigger.new){
        if(!string.isBlank(assid.Asset_Name__c))
        {
            Assetid.put(assID.id, assid.Asset_Name__c);
        }
    }
    }
    
    if(Trigger.isUpdate)
    {
    for(Assets_Check_in_Checkout__c AssID : Trigger.new){
        
        if(!string.isblank(assid.Asset_Name__c) && Trigger.oldMap.get(AssID.id).Status__c != Trigger.newMap.get(AssID.id).Status__c)
        {
            Assetid.put(assID.id, assid.Asset_Name__c);
            assetName.add(assid.Asset_Name__c);
        }
    }
    }
    
    for(Asset a: [select id, status,(select name, status__c from Assets_Check_in_Checkout__r where id IN:Assetid.keySet()) from Asset where id IN: AssetId.values()]){
        
        
        for(Assets_Check_in_Checkout__c ACC : a.Assets_Check_in_Checkout__r)
        {
            Asset a1 = new Asset();
            if(acc.status__C == 'Check-in')
            {
                a1.Status = 'Available';
                a1.id = a.id;
                LAsset.add(a1);
            }
            else if(acc.status__c == 'Checkout')
            {
                a1.Status = 'Not Available';
                a1.id = a.id;
                LAsset.add(a1);
            }
        }
        
       
    }
    
    Update LAsset;
}
    
Hi,
I am new to apex code I would  appreciate any assistance on the following question:
I am trying  to write apex  code that auto sums up the total assets per product . I have a custom field in product object called "Initial stock", and in the asset object, I  have a custom picklist field  called "Status" with values like "check-in" and  "Checkout", and also a  product field  which is a lookup field to the standard product object.
My business idea is  thus:  Every new or existing asset should have  "Check-in" as the asset status and  a trigger should auto sum up all the checked-in assets under each product and put the number in the "Initial stock" field in the product table.
And when the asset status changes to "Checkout", a custom field called "Consumed stock" should be auto updated with the number of assets checked out per product.
Any help will be highly appreciated!