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
pfeppfep 

help on trigger

i need a trigger which can have following behavior

 

1. Allow engagements with a record type of "interest form" and status of "Prospect" to be deleted.
2. Prevent engagements with a record type othe than "Interest form" and status of "Prospect" from being deleted.

intrest form and prospect is record type.

engagement is custom object( engagement_c)

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

basic debugging

 

trigger checkDelete on Engagement__c(before Delete){

 

 

RecordType[] RT = [Select Name From RecordType Where sObjectType = 'engagement__c' and Name = 'Interest Form' and isActive = true LIMIT 1];

 

for(Engagement__c e : trigger.new){

   if(!rt.isEmpty()){

      if(e.RecordTypeID == RT[0].id && e.Status__c == 'Prospect')

           e.addError('You cannot delete this record');

    }else{

           e.addError('Contact System Admin, there was an error finding the correct RecordType for this validation');

    }

 

}

 

}

All Answers

Starz26Starz26

trigger checkDelete on Engagement__c(before Delete){

 

 

ID RT = [Select Name From RecordType Where sObjectType = 'engagement__c' and Name = 'Interest Form' and isActive = true LIMIT 1];

 

for(Engagement__c e : trigger.new){

 

   if(e.RecordTypeID = RT && e.Status__c = 'Prospect')

           e.addError('You cannot delete this record');

 

}

 

}

pfeppfep

This is  what i am facing:

Description:

Duplicate engagement records are generated in Salesforce when applicants submit more than one interest form. Using permission sets, we can allow specific users to delete engagements but We want to prevent those users from mistakenly deleting engagements of current students, alumni or students who were referred out.

Behavior:

1. Allow engagements with a record type of "interest form" and status of "Prospect" to be deleted.
2. Prevent engagements with a record type other than "Interest form" and status of "Prospect" from being deleted.

 

So  this trigger does work on this requirement as mentioned? will this also prevent duplicating engagement records also? or do i need to do something else? or this is perfectly fine.

plz suggest me. 

 

thank you

 

 

Starz26Starz26

It will work for requirement. It does not prevent duplicates

pfeppfep

when i put this trigger in engagement it shows error.

 

 

Starz26Starz26

What error does it show?

pfeppfep
Error

Error: Compile Error: Illegal assignment from LIST<RecordType> to Id at line 7 column 1

Starz26Starz26

Opps...

 

trigger checkDelete on Engagement__c(before Delete){

 

 

RecordType[] RT = [Select Name From RecordType Where sObjectType = 'engagement__c' and Name = 'Interest Form' and isActive = true LIMIT 1];

 

for(Engagement__c e : trigger.new){

   if(!rt.isEmpty()){

      if(e.RecordTypeID = RT[0].id && e.Status__c = 'Prospect')

           e.addError('You cannot delete this record');

    }else{

           e.addError('Contact System Admin, there was an error finding the correct RecordType for this validation');

    }

 

}

 

}

pfeppfep
ErrorError: Compile Error: AND operator can only be applied to Boolean expressions at line 15 column 39
pfeppfep

still it is not working.(

Starz26Starz26

basic debugging

 

trigger checkDelete on Engagement__c(before Delete){

 

 

RecordType[] RT = [Select Name From RecordType Where sObjectType = 'engagement__c' and Name = 'Interest Form' and isActive = true LIMIT 1];

 

for(Engagement__c e : trigger.new){

   if(!rt.isEmpty()){

      if(e.RecordTypeID == RT[0].id && e.Status__c == 'Prospect')

           e.addError('You cannot delete this record');

    }else{

           e.addError('Contact System Admin, there was an error finding the correct RecordType for this validation');

    }

 

}

 

}

This was selected as the best answer
pfeppfep

thank you