+ Start a Discussion
NishanNishan 

Trigger to update another object

Hi Everyone,

 

                       I have 2 objects 'Gift__c' and 'Field_Executive__c'.    There is a lookup relation from the former to the latter. Whenever there is a name of a field executive in a newly created Gift record, an updation should occur in the record of that particular Field Executive.

                       There is a common decimal field called No_of_Gifts__c in both objects. This value should be extracted from the current Gift and it should be subtracted from the 'No_of_Gifts__c' field in the corresponding Field Executive. My code is not working. It is compiling but no updation occurs. Can someone point me the error ?

trigger Issue on Gift__c (after insert)
{

Decimal gifts=trigger.new[0].No_of_Gifts__c;
string fieldexecutive_name=trigger.new[0].Fieldexecutive__c;


List<Fieldexecutive__c> fieldexecutive=[Select Id from Fieldexecutive__c where Name=:fieldexecutive_name];

for(Fieldexecutive__c   fe:fieldexecutive)
{
    Decimal giftsfe=fe.Number_of_Gifts__c;
        if(giftsfe>=gifts)
        {
        fe.Number_of_Gifts__c=giftsfe-gifts;
        }
}
   
         update fieldexecutive;
                        
                 

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

try below

 

trigger Issue on Gift__c (after insert)
{
	Set<Id> setFieldexecutiveId = new Set<Id>();
	Map<Id, Decimal> mapGifts = new Map<Id, Decimal>();
	for(Gift__c g:Trigger.New){
		setFieldexecutiveId.add(g.Fieldexecutive__c);
		mapGifts.add(g.Fieldexecutive__c, g.No_of_Gifts__c);
	}
	List<Fieldexecutive__c> fieldexecutive = new List<Fieldexecutive__c>();
	if(setFieldexecutiveId.size()>0){
		fieldexecutive=[Select Id, Number_of_Gifts__c from Fieldexecutive__c where Id in:setFieldexecutiveId];
	}
	if(fieldexecutive.size()>0){
		List<Fieldexecutive__c> lstUpdate = new List<Fieldexecutive__c>();
		for(Fieldexecutive__c fe:fieldexecutive)
		{
			if(mapGifts.size()>0 && mapGifts.containsKey(fe.Id) && fe.Number_of_Gifts__c >= mapGifts.get(fe.Id)){
				fe.Number_of_Gifts__c=fe.Number_of_Gifts__c - mapGifts.get(fe.Id);
				lstUpdate.add(fe);
			}
		}
		if(lstUpdate.size()>0) update lstUpdate;
	}
}

 

All Answers

Dhaval PanchalDhaval Panchal

try this

trigger Issue on Gift__c (after insert)
{

Decimal gifts=trigger.new[0].No_of_Gifts__c;
string fieldexecutive_name=trigger.new[0].Fieldexecutive__c;


List<Fieldexecutive__c> fieldexecutive=[Select Id, Number_of_Gifts__c from Fieldexecutive__c where Name=:fieldexecutive_name];

for(Fieldexecutive__c   fe:fieldexecutive)
{
    Decimal giftsfe=fe.Number_of_Gifts__c;
        if(giftsfe>=gifts)
        {
              fe.Number_of_Gifts__c=giftsfe-gifts;
        }
}
   
         update fieldexecutive;
                        
                 

}

 

NishanNishan

Hi Dapanchal,

                          It is still not working. Also do I need to use a list here if I only have to deal with just 1 record everytime?

Dhaval PanchalDhaval Panchal
Can you please explain lookup relationship between these two objects?
NishanNishan

In the Gifts__c standard page there is a LOOKUP field called Fieldexecutive which points to the Name field of a single Fieldexecutive.

 

 

 

 

 

Dhaval PanchalDhaval Panchal

try below

 

trigger Issue on Gift__c (after insert)
{
	Set<Id> setFieldexecutiveId = new Set<Id>();
	Map<Id, Decimal> mapGifts = new Map<Id, Decimal>();
	for(Gift__c g:Trigger.New){
		setFieldexecutiveId.add(g.Fieldexecutive__c);
		mapGifts.add(g.Fieldexecutive__c, g.No_of_Gifts__c);
	}
	List<Fieldexecutive__c> fieldexecutive = new List<Fieldexecutive__c>();
	if(setFieldexecutiveId.size()>0){
		fieldexecutive=[Select Id, Number_of_Gifts__c from Fieldexecutive__c where Id in:setFieldexecutiveId];
	}
	if(fieldexecutive.size()>0){
		List<Fieldexecutive__c> lstUpdate = new List<Fieldexecutive__c>();
		for(Fieldexecutive__c fe:fieldexecutive)
		{
			if(mapGifts.size()>0 && mapGifts.containsKey(fe.Id) && fe.Number_of_Gifts__c >= mapGifts.get(fe.Id)){
				fe.Number_of_Gifts__c=fe.Number_of_Gifts__c - mapGifts.get(fe.Id);
				lstUpdate.add(fe);
			}
		}
		if(lstUpdate.size()>0) update lstUpdate;
	}
}

 

This was selected as the best answer
NishanNishan

It gives the following error

 

Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Decimal>].add(Id, Decimal) at line 7 column 9

 

 

   g.Field_Executive__c returns the Name field right? Can the Name field pass as Id?

Dhaval PanchalDhaval Panchal
Use

mapGifts.put(g.Fieldexecutive__c, g.No_of_Gifts__c);

instead of

mapGifts.add(g.Fieldexecutive__c, g.No_of_Gifts__c);
Dhaval PanchalDhaval Panchal
Its not a name field, its a lookup field and linked with id field not with name field.
NishanNishan

Hi Daval,

                It is working now. Thanks a lot for your help.