I get an error Condition expression must be of type Boolean.
for (Account acc: trigger.new)
{
if(acc.RecordTypeId = [SELECT Id from RecordType WHERE sObjectType = 'Account' AND IsActive = True AND Name = 'Health Care' LIMIT 1].Id)
{
//some code
}
if(pos.RecordTypeId == [SELECT Id from RecordType WHERE sObjectType = 'Account' AND IsActive = True AND Name = 'Health Care' LIMIT 1].Id)
Use double == instead as above.
Related
Why I am getting 'TOO MANY DML STATEMENTS ERROR IN THIS LINE?'
Note: Class is auraenabled(cacheable =true)
List<ABC> obj1 = new List<ABC>();
for (ABC A1 : [SELECT ID,field1, field2, field3, (SELECT Id, Name,ParentId,CreatedDate FROM Attachments WHERE Name LIKE 'something' ORDER BY CreatedDate DESC Limit 1) FROM ABC WHERE field5ID = 'field5ID' ORDER BY field1]) {
if(!A1.Attachments.isEmpty()) {
for (Attachment Att : A1.Attachments) {
MAP1.put(A1.field1,A1.Id);
if(A1.field2 = true){
A1.field2 = false;
Obj1.add(A1) ;
}
}
}
}
if(Obj1.size() > 0){
update Obj1; //GETTING ERROR ON THIS LINE
}
I want to build a trigger on contact, that accept check box. And In Account object it take 2 contacts that are active. when user assign 3rd active contact to the same account he/she will get error messege. how can it possible? i completed all codes but these limit of 2 contacts are pending. can anyone try to guide me how to do this?
trigger ContactTrigger on Contact(after insert, after update, after delete, after undelete) {
switch on Trigger.operationType {
when AFTER_INSERT {
Set<Id> accountIds = new Set<Id>();
for (Contact con : Trigger.new) {
if (String.isNotBlank(con.AccountId)) {
//write automation logic here
accountIds.add(con.AccountId);
}
}
// get aggregate result for all accounts
List<AggregateResult> results = [
SELECT AccountId, COUNT(Id) totalContacts
FROM Contact
WHERE Active__c = TRUE AND AccountId IN :accountIds
GROUP BY AccountId
];
integer var = results.size();
// build final list of accounts to update
List<Account> accountsToUpdate = new List<Account>();
for (AggregateResult result : results) {
// get account id and number of active contacts
String accId = String.valueOf(result.get('AccountId'));
Integer totalContacts = Integer.valueOf(result.get('totalContacts'));
// make sure you use Id feild in your account to update it
Account acc = new Account(Id = accId, Active_Contacts__c = totalContacts);
accountsToUpdate.add(acc);
}
// update the final list of account
update accountsToUpdate;
}
when AFTER_UPDATE {
Set<Id> accountIds = new Set<Id>();
for (Contact con : Trigger.new) {
// capture the account id only if active checkbox value is flipped
if (String.isNotBlank(con.AccountId) && Trigger.oldMap.get(con.Id).Active__c != con.Active__c) {
// write automation logic here
accountIds.add(con.AccountId);
} else if (Trigger.oldMap.get(con.Id).AccountId != con.AccountId) {
accountIds.add(con.AccountId);
accountIds.add(Trigger.oldMap.get(con.Id).AccountId);
}
}
// get aggregate result for all accounts
List<AggregateResult> results = [
SELECT AccountId, COUNT(Id) totalContacts
FROM Contact
WHERE Active__c = TRUE AND AccountId IN :accountIds
GROUP BY AccountId
];
// build final list of accounts to update
List<Account> accountsToUpdate = new List<Account>();
for (AggregateResult result : results) {
// get account id and number of active contacts
String accId = String.valueOf(result.get('AccountId'));
Integer totalContacts = Integer.valueOf(result.get('totalContacts'));
// make sure you use Id feild in your account to update it
Account acc = new Account(Id = accId, Active_Contacts__c = totalContacts);
accountsToUpdate.add(acc);
}
// update the final list of account
update accountsToUpdate;
}
}
}
Here is snippet I am using in my anonymous window . Purpose is to retrieve Opportunities of a contact. Even after adding opportunity contact role , contact.Opportunities.size is resulting in zero (last debug line). Am I missing something ? you may use the below code directly.
Update: able to get size now but same logic doesn't work for code coverage in test class . details listed below:
Only 'if' part of controller is covered and 'else' part is never covered even though size of contact.opportunities is more than 0.
Controller method :
public PageReference sendingEmail() {
//contact1 has query records
sizeVar = contact1.Opportunities.size();
if(contact1.npo02__OppAmountLastYear__c>0 ) {
if(sizeVar==0) {
// if size is 0 then navigate to a particular vf page
PageReference pr = Page.NoDonationOrNoEmail;
pr.getParameters().put('id',(String)contact1.id);
pr.setRedirect(true);
return pr;
}
else
{ //when contact.opportunities size is more than 0 then navigate to
other vf page.
PageReference pr1 = Page.NoPrint;
pr1.getParameters().put('id',(String)contact1.id);
pr1.setRedirect(true);
return pr1;
}
} return null;
}
Test Class:
//creating account
Account a = new Account();
a.Name = 'Test Co.';
a.BillingStreet = '298 S. Ringo Street';
a.BillingCity = 'Little Rock';
insert a;
//Creating contact
Contact contact1 = new Contact();
contact1.FirstName = 'Paul';
contact1.LastName = 'Test';
contact1.AccountId = a.id;
contact1.npo02__OppAmountLastYear__c=100;
insert contact1;
//creating opportunity
Opportunity o = new Opportunity();
o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType
WHERE Name = 'Membership' LIMIT 1];
o.Name = 'New Record';
o.StageName = 'Posted';
o.AccountId = contact1.AccountId;
o.CloseDate = Date.today();
o.Description = 'Test Record';
insert o;
//creating opportunity contact role
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.ContactId = contact1.Id;
ocr.OpportunityId = o.Id;
ocr.IsPrimary = TRUE;
ocr.Role = 'Decision Maker';
insert ocr;
System.debug('created opportunity contact role for primary');
Update o;
contact1 = [SELECT Id, Name,(SELECT id FROM opportunities) FROM
Contact WHERE Id=:contact1.Id];
PageReference pr = Page.NoPrint;
pr.getParameters().put('id', String.valueOf(contact1.id));
Test.setCurrentPage(pr);
ApexPages.StandardController cont5 = new
ApexPages.StandardController(contact1);
BulkEmailController testAccPlan = new
BulkEmailController(cont5);
testAccPlan.sendingEmail();
When you create and insert a record, you don't get formula fields nor you can navigate lookup. The same goes with child relationship.
You have to query the fields you need.
Change Update contact1; to contact1 = [SELECT Id, (SELECT Id FROM Opportunities) FROM Contact WHERE Id = :contact1.Id]; and the last debug line will print 1.
i have this APEX trigger and I'm getting this error but not sure why "Error: Compile Error: expecting a left angle bracket, found 'oppcntctrle' at line 13 column 10"
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
Boolean isPrimary;
Integer iCount;
Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map
for (Integer i = 0; i < Trigger.new.size(); i++)
{
oppty_con.put(Trigger.new[i].id,
Trigger.new[i]);
}
isPrimary = False;
for (List oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
{
if (oppcntctrle .Size() >0)
{
isPrimary = True;
}
}
iCount = 0;
for (List oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
{
if (oppcntctrle2 .Size()>0)
{
iCount= oppcntctrle2 .Size();
}
}
for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
{
Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
Oppty.Primary_Contact_Assigned__c =isPrimary;
}
}
Instead of
for (List oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
should be
List<OpportunityContactRole> oppcntctrle = [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())];
The same for
for (List oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
should be changed to
List<OpportunityContactRole> oppcntctrle2 = [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())];//Query for Contact Roles
i am trying to update a ObjectPermissions Object in slaesforce such that a profile will get the permissions to access a object similar to other profile.
i write a code
my code segment is
PermissionSet set1 = [SELECT Id From PermissionSet
WHERE profileId = : SourceProfileId LIMIT 1] ;
PermissionSet set2 = [SELECT Id FROM PermissionSet
WHERE profileId = : TargetProfileId LIMIT 1];
List<ObjectPermissions> oo = [SELECT Id,
SObjectType,
ParentId,
PermissionsCreate,
PermissionsDelete,
PermissionsEdit,
PermissionsModifyAllRecords,
PermissionsRead,
PermissionsViewAllRecords
FROM ObjectPermissions
WHERE ParentId = : set1.id];
List<ObjectPermissions> oo1 = [SELECT ParentId,
Id,
SObjectType,
PermissionsCreate,
PermissionsDelete,
PermissionsEdit,
PermissionsModifyAllRecords,
PermissionsRead,
PermissionsViewAllRecords
FROM ObjectPermissions
WHERE ParentId = : set2.Id];
Map<String , ObjectPermissions> source_obj = new Map<String, ObjectPermissions>();
Map<String , ObjectPermissions> target_obj = new Map<String, ObjectPermissions>();
for (ObjectPermissions o : oo) {
source_obj.put(o.SObjectType, o);
}
for (ObjectPermissions o : oo1) {
target_obj.put(o.SObjectType, o);
}
ObjectPermissions target, source;
for (String s : source_obj.keySet() ) {
if (target_obj.containsKey(s)) {
target = target_obj.get(s);
source = source_obj.get(s);
System.debug('Source is:' + source);
System.debug('Target is : ' + target);
target.PermissionsCreate = source.PermissionsCreate;
target.PermissionsDelete = source.PermissionsDelete;
target.PermissionsEdit = source.PermissionsEdit;
target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
target.PermissionsRead = source.PermissionsRead;
target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
update target;
} else {
target = new ObjectPermissions(SObjectType = s );
source = source_obj.get(s);
target.PermissionsCreate = source.PermissionsCreate;
target.PermissionsDelete = source.PermissionsDelete;
target.PermissionsEdit = source.PermissionsEdit;
target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
target.PermissionsRead = source.PermissionsRead;
target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
insert target;
}
}
when the control come to this line
update target;
it is giving error
Update failed. First exception on row 0 with id 110i0000007KNEvAAO; first error: INVALID_CROSS_REFERENCE_KEY, You can't create, edit, or delete records for this permission set parent because it's associated with a profile. Parent ID: 0PSi00000009BE9: []
i am unable to figure it out why i am facing this error please some one help to resolve this error
the persmission set you are trying to update is already associated with some of your profile with id=0PSi00000009BE9
go to
https://ap1.salesforce.com/0PSi00000009BE9 and check there.