• 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,
I have an apex code that is working perfectly but I want the trigger to only fire when a user logs out/ timesout out his/her session, how can I make that work?

Thanks,
Mariam
Hi guys,
I have written the following test class to for the apex code below, but the code coverage is only 50%. Apparently the parent object record is not updated based on the code.
Below is the  apex code and the test class:
trigger SumTotalMonths on TargetX_SRMb__Family_Relationship__c (after insert,after update,after delete,after undelete) 
{
Map<ID,TargetX_SRMb__Application__c> applicationMap = new Map<ID,TargetX_SRMb__Application__c>();
    List<ID> appID = new List<ID>();
    
    //check for the DML operation, for Insert,update and undelete
    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){
        for(TargetX_SRMb__Family_Relationship__c Rel1 : Trigger.new) {
            
            //Add all new  AccountID in the list
            if(Rel1.TargetX_SRMb__Application__c != NULL && Rel1.Total_Months_in_this_Position__c != NULL)
                appID.add(Rel1.TargetX_SRMb__Application__c);
        }
    }
     // check for DML operation for update and delete
    if(Trigger.IsDelete) {
        for(TargetX_SRMb__Family_Relationship__c Rel2 : Trigger.old)
        {
            if(Rel2.TargetX_SRMb__Application__c != NULL && Rel2.Total_Months_in_this_Position__c != NULL)
              appID.add(Rel2.TargetX_SRMb__Application__c);
      }
    }  
   // check if application list is not empty

    if(appID != NULL && appID.size()>0){
        // Add all the applications in the map to map IDs with total months employed
        for(ID ApplicationID : appID) {
           applicationMap.put(ApplicationID, 
          new TargetX_SRMb__Application__c(ID=ApplicationID,Total_Months_Employed__c= 0) ) ;
      } }
   // Calculate the total months employed based on value in the total months in this position
    for(TargetX_SRMb__Family_Relationship__c Rel : [ SELECT ID,TargetX_SRMb__Application__c , Total_Months_in_this_Position__c
                                                   FROM  TargetX_SRMb__Family_Relationship__c WHERE TargetX_SRMb__Application__c  IN :appID ]) 
     
     {applicationMap.get(Rel.TargetX_SRMb__Application__c).Total_Months_Employed__c += Rel.Total_Months_in_this_Position__c;
                          }
    
    //commit to the database
    Database.update(applicationMap.values());
}

Test Class:
@isTest(seealldata=false)
public class GetTotalMonthsWorked {
    static testMethod void CalcMonths(){
        
     //create new Account
     Account acc = new Account(Name= 'Unassigned Contacts', Industry = 'Education'); 
     insert acc;
     
   // Create new contact
    Contact c = new Contact(FirstName= 'Sophie',LastName= 'Test', Accountid = acc.id, Gender__c  = 'F', Email= 'Testset@test.com');
        insert c;
   // Create new application
   TargetX_SRMb__Application__c app = new TargetX_SRMb__Application__c(TargetX_SRMb__Contact__c = c.id,TargetX_SRMb__Stage__c = 'In Progress',
                                                                      TargetX_SRMb__Status__c= 'Incomplete');
    insert app;
        
   // Create list of relationships
   List<TargetX_SRMb__Family_Relationship__c> RelList = new List <TargetX_SRMb__Family_Relationship__c>();
        
        for( integer i=0; i<200; i++)
        {
            
         RelList.add(new TargetX_SRMb__Family_Relationship__c (Name = 'TestRel' + i, TargetX_SRMb__Contact__c = c.id,
                 End_Date__c = date.ValueOf('2018-03-02'), Start_Date__c = date.ValueOf('2016-03-02'))); 
            
        }       
   insert RelList;
        //confirm if the total months worked is updated.
        List<TargetX_SRMb__Family_Relationship__c> RelMonths =new List<TargetX_SRMb__Family_Relationship__c>
            ([SELECT Total_Months_in_this_Position__c  FROM TargetX_SRMb__Family_Relationship__c
              WHERE Id= :app.id limit 1]);
        system.assertEquals(200, RelMonths[0].Total_Months_in_this_Position__c);
        
        //list all the application ids
        List<TargetX_SRMb__Application__c> IDs= new List<TargetX_SRMb__Application__c>
            ([SELECT Total_Months_Employed__c FROM TargetX_SRMb__Application__c 
             WHERE id = :app.id limit 1]);
        
    
    // update relationship record
    List<TargetX_SRMb__Family_Relationship__c> RelList2 = new List<TargetX_SRMb__Family_Relationship__c>();
        for ( integer i=0; i<10; i++)
       
        {
           TargetX_SRMb__Family_Relationship__c RelList3 = new TargetX_SRMb__Family_Relationship__c();
           RelList3.End_Date__c= date.ValueOf('2019-03-02');
           RelList3.Start_Date__c= date.ValueOf('2015-03-02');
           RelList3.id = RelList[i].id;
           RelList2.add(RelList3);
            
        }
        
        Update RelList2;
        
        //delete relationship from a list of relationships
       // TargetX_SRMb__Family_Relationship__c deletedRel = [ SELECT Name, IsDeleted FROM TargetX_SRMb__Family_Relationship__c 
                                                         // WHERE Name = :RelList ALL ROWS];
       // System.assertEquals(deletedTargetX_SRMb__Family_Relationship__c.IsDeleted,true);
    
   }

}

Thanks,
Mariam
 
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 there,
I am working on implementing a migration from cipher cloud to shield encryption in salesforce and I was wondering if anyone has done anything similar and what he/she thinks may be the implication of this kind of transition.
Any advice will be appreciated.
Thanks!
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!
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!