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
affuaffu 

Trigger to update status

Hi All ,

 

I have Three Objects 

 

Opportunity 

Parking Detail  - junction object to Opportunity and Car Park

Car Park 

 

Opportunity fields - Stage: Picklist(site visit, booking. blocking)

Parking Detail fields - Opportunity: lookup , Carpark: lookup

Car Park fields - Status: Picklist(Available,booked,blocked)

 

Here the should fire when oportunity status=blocking Parking details should check all the records which related to that opportunity and should update the carpark status= blocked

 

Please help me

 

Thank u 

 

Niket SFNiket SF

hello affu,

     you need to write a TRIGGER on the after opportunuty Insert or Update.

 1. In after update you will receive all the Opportunity Id's from trigger.newMap fire a loop on the Trigger.New

   Set<Id> SetOppId = new Set<Id>();

 

   for(Opportunity objOpp : Trigger.New)

{

   if(objOpp.stage == 'blocking')

    SetOppId.add(objOpp);

}

  Fire SOQL on the junction object with the help of SetOppId id collect all the junstion object whose is having Opportunity refereance in it.

 

List<ParkingDetails> lstPark = [select Opportunity,Carpark where  Opportunity In : SetOppId];

List<CarPark> lstCarPark = new List<CarPark>();

 

 

for(ParkingDetails objpark : lstPark)

{

   lstCarPark.add(new CarPark(Id = objpark .CarPark, Stage ='blocked'));

}

 

if(lstCarPark.IsEmpty())

    Database.update(lstCarPark);

 

 

If it helps you please marked as solved.

 

 

ShaTShaT

Hi ,

 

You need to write a trigger on Opportunity after update . 

Get the junction object realted to that opportunity .

When you get the junction object get the car park object in that junction object .

update the field in the car park object.

 

 

 

 

Thanks

Shailu

 

 

 

affuaffu

 

Thanks for replying nik

 

I m getting an error as Compile Error: unexpected token: where at line 11 column 76

 

List<Parking_Detail__c> lstPark = [select Id,Opportunity__c,Car_park__c where  Opportunity__c IN : SetOppId];

affuaffu

Thanks for replying Shailu, but im not getting how to relate the junction object to opportunity and add carpark object to junction object

 

 

 

 

ShaTShaT

Hi ,

 

The solution nik has given is good . That i was trying to say .

You just need to modify the query

 

try this-:

List<Parking_Detail__c> lstPark = [select Id,Opportunity__c,Car_park__c from Parking_Detail__c  where  Opportunity__c IN : SetOppId];

 

 

Thanks

Shailu

affuaffu

Yes i did it before 

 

still im getting an error 

 

Compile Error: Incompatible element type SOBJECT:Opportunity for collection of Id at line 7 column 9

 

trigger ePeople_CarPark_Update on Opportunity (after insert, after update) {
Set<Id> SetOppId = new Set<Id>();

for(Opportunity Opp : Trigger.New)
{
if(Opp.stage == 'Blocking')
SetOppId.add(Opp);
}
List<Parking_Detail__c> lstPark = [select Id,Opportunity__c,Car_park__c from Parking_Detail__c where Opportunity__c IN : SetOppId];
List<Car_Park__c> lstCarPark = new List<Car_Park__c>();


for(Parking_Detail__c objpark : lstPark)
{
lstCarPark.add(new Car_Park__c(Id = objpark.Car_Park__c, Status__c ='Blocked'));
}

if(lstCarPark.IsEmpty())
Database.update(lstCarPark);

}

ShaTShaT

Hi,

 

You just need to select all the fileds from your junction object which i Parking_Detail__c may be the api name is diffrent from the given one.

 

Thanks

Shailu

nagalakshminagalakshmi

Hi,

 

use the statement SetOppId.add(Opp.id ); instead of SetOppId.add(Opp);

 

Thanks