You need to sign in to do that
Don't have an account?

Merging two different apex codes on similar custom object into one code
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;
}
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;
}
trigger SetContactOnAssetRecord on Assets_Check_in_Checkout__c (before update,before insert, after update, after insert) {
//using appropriate conditions
if(Trigger.isInsert & trigger.isafter)
{
// include the logic here
}
if(Trigger.isupdate & trigger.isafter)
{
// include the logic here
}
if(Trigger.isInsert & trigger.isbefore)
{
// include the logic here
}
if(Trigger.isupdate & trigger.isbefore)
{
// include the logic here
}
}
I appeciate!