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
WikWik 

Trigger On Service Agreement Object

Hi,

Need to write a trigger on the following requirement:

 

When the ‘Active’ status on the attached Service Agreement object goes to ‘Inactive’, the associated Equipment Object's field is marked on the ‘Contract Status’ field with ‘Under Expired Contract’.

 

The service agreement object is in lookup relationship with the equipment object.

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

 

Oh yes, I think you'll only need that to be after Update.

 

The reason I added this line: if(serv.Status__c == 'Inactive' && ( trigger.isInsert || serv.Status__c != trigger.oldMap.get(serv.id).Status__c))  is because I kept trigger on isInsert, but after update should get you what you need.

 

And yes, the code that you wrote just now should serve the purpose.

All Answers

magicforce9magicforce9

Hi,

 

Here is the sample trigger that you can start off with...Change the API names of obects and fields that I'm using

trigger serviceAgreementInactive on ServiceAgreements__c (before insert, before update){

	set<id> equipmentIds = new Set<id>();
	for(ServiceAgreements__c serv : trigger.new){
         //There is a look'up relationship between the two objects, i'm assuming Equipments__c is the reference field.
		if(serv.Status__c == 'Inactive' && ( trigger.isInsert || serv.Status__c != trigger.oldMap.get(serv.id).Status__c))
			equipmentIds.add(serv.Equipments__c);

	}
	List<Equipments__c> equipmentsToMakeExpire = [Select id, Contract_Status__c from Equipments__c where id IN :equipmentIds];

	for(Equipments__c e : equipmentsToMakeExpire)
		e.Contract_Status__c = ‘Under Expired Contract’;

	if(!equipmentsToMakeExpire.isEmpty()) update equipmentsToMakeExpire;
}

 

 

 

WikWik

Hi,

 

I think this would be a after update trigger.

 

May you explain the following code:

 

if(serv.Status__c == 'Inactive' && ( trigger.isInsert || serv.Status__c != trigger.oldMap.get(serv.id).Status__c))
equipmentIds.add(serv.Equipments__c);

WikWik

Hi,

Will the following serve the purpose

 

trigger serviceAgreementInactive on Service_Agreement__c(after update){

Map<ID, Service_Agreement__c > oldMap = new Map<ID,Service_Agreement__c >(Trigger.old);
List<Id> equipmentIds = new List<Id>();
for(Service_Agreement__c serv : trigger.new){

if(serv.status__c == 'Inactive' && oldMap.get(serv.Id).status__c !='Inactive')
equipmentIds.add(serv.Id);

    }
    List<Equipment__c> equipmentsToMakeExpire = [Select id, Contract_Status__c from Equipment__c where id IN :equipmentIds];

    for(Equipment__c e : equipmentsToMakeExpire)
        e.Contract_Status__c = 'Under Expired Contract';

    if(!equipmentsToMakeExpire.isEmpty()) update equipmentsToMakeExpire;
}

magicforce9magicforce9

Hi,

 

 

Oh yes, I think you'll only need that to be after Update.

 

The reason I added this line: if(serv.Status__c == 'Inactive' && ( trigger.isInsert || serv.Status__c != trigger.oldMap.get(serv.id).Status__c))  is because I kept trigger on isInsert, but after update should get you what you need.

 

And yes, the code that you wrote just now should serve the purpose.

This was selected as the best answer
WikWik

Thanx a lot.

WikWik

Hey it's actually not performing the desired action.

It's actually doing nothing.