• Anoop Rb
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 6
    Replies
Hi please any one help me on this " I am trying to update Account related Opportunities ". Here I have a field on Account object named as "Account Status" and also i have field on Opportunity Object named as "Opportunity Status". if changed Account status as "Active " then the Account related Opportunities "Opportunity Status" also "Active", if the Account status is changed to "In Active " then the Account related Opportunities "Opportunity Status" also changed to " In Active". this is my requirement. below is my code. when i use this code am getting error like "Error:Apex trigger updateAccRelatedOpptys caused an unexpected exception, contact your administrator: updateAccRelatedOpptys: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0069000000cAZjEAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccCon: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0019000001Eu0chAAB Trigger.UpdateAccCon: line 16, column 1: []: Trigger.updateAccRelatedOpptys: line 15, column 1"

Below is my code. please let me explain how to avoid this error. when i use same code for account related contacts and cases update its working fine.


trigger updateAccRelatedOpptys on Account (After insert,After update) {
//set<>
list<Account> accRelConts=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities) from Account where id IN : trigger.newMap.keyset()];
list<Account> accrelOppty=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities ) from Account where Id IN : trigger.newMap.keyset()];

System.debug('++++++++++key set+++++++++++'+trigger.newMap.keyset());
list<Opportunity> opplst=new list<Opportunity>();
for(Account a:accRelConts){
for(Opportunity op:a.Opportunities){
op.Oppty_Status__c='hjg';
opplst.add(op);
}
}
if(opplst.size() > 0){
update opplst;
}
}
Can any one help me i have displayed records in visualforce pagehere i have delete each record without using param.
any body help me how to cover catch() block in this code..? i am just covering try block only. How to cover catch block please help me.
trigger chatter_answers_question_escalation_to_case_trigger on Question (after update) {
    for (Question q: Trigger.new) {
        try {
            if (q.Priority == 'high' && (q.Cases == null || q.Cases.size() == 0) && Trigger.oldMap.get(q.id).Priority != 'high') {
                q = [select Id, Title, Body, CommunityId, createdById, createdBy.AccountId, createdBy.ContactId from Question where Id = :q.Id];
                Case newCase = new Case(Origin='Chatter Answers', OwnerId=q.CreatedById, QuestionId=q.Id, CommunityId=q.CommunityId, Subject=q.Title, Description = (q.Body == null? null: q.Body.stripHtmlTags()), AccountId=q.CreatedBy.AccountId, ContactId=q.CreatedBy.ContactId);
                insert newCase;
            }
        } catch (Exception e) {
            String subjectText = 'Case Escalation exception in site ' + Site.getName();
            String bodyText = 'Case Escalation on Question having ID: ' + q.Id + ' has failed with the following message: ' + e.getMessage() +
                '\n\nStacktrace: ' + e.getStacktraceString();

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] { Site.getAdminEmail() };

            mail.setReplyTo('no-reply@salesforce.com');
            mail.setSenderDisplayName('Salesforce Chatter Answers User');

            // The default sender is the portal user causing this trigger to run, to change this, set an organization-wide address for
            // the portal user profile, and set the ID in the following line.
            // mail.setOrgWideEmailAddressId(orgWideEmailAddressId);
            mail.setToAddresses(toAddresses);
            mail.setSubject(subjectText);
            mail.setPlainTextBody(bodyText);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}
Hi please any one help me on this " I am trying to update Account related Opportunities ". Here I have a field on Account object named as "Account Status" and also i have field on Opportunity Object named as "Opportunity Status". if changed Account status as "Active " then the Account related Opportunities "Opportunity Status" also "Active", if the Account status is changed to "In Active " then the Account related Opportunities "Opportunity Status" also changed to " In Active". this is my requirement. below is my code. when i use this code am getting error like "Error:Apex trigger updateAccRelatedOpptys caused an unexpected exception, contact your administrator: updateAccRelatedOpptys: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0069000000cAZjEAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccCon: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0019000001Eu0chAAB Trigger.UpdateAccCon: line 16, column 1: []: Trigger.updateAccRelatedOpptys: line 15, column 1"

Below is my code. please let me explain how to avoid this error. when i use same code for account related contacts and cases update its working fine.


trigger updateAccRelatedOpptys on Account (After insert,After update) {
//set<>
list<Account> accRelConts=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities) from Account where id IN : trigger.newMap.keyset()];
list<Account> accrelOppty=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities ) from Account where Id IN : trigger.newMap.keyset()];

System.debug('++++++++++key set+++++++++++'+trigger.newMap.keyset());
list<Opportunity> opplst=new list<Opportunity>();
for(Account a:accRelConts){
for(Opportunity op:a.Opportunities){
op.Oppty_Status__c='hjg';
opplst.add(op);
}
}
if(opplst.size() > 0){
update opplst;
}
}
Can any one help me i have displayed records in visualforce pagehere i have delete each record without using param.
any body help me how to cover catch() block in this code..? i am just covering try block only. How to cover catch block please help me.
trigger chatter_answers_question_escalation_to_case_trigger on Question (after update) {
    for (Question q: Trigger.new) {
        try {
            if (q.Priority == 'high' && (q.Cases == null || q.Cases.size() == 0) && Trigger.oldMap.get(q.id).Priority != 'high') {
                q = [select Id, Title, Body, CommunityId, createdById, createdBy.AccountId, createdBy.ContactId from Question where Id = :q.Id];
                Case newCase = new Case(Origin='Chatter Answers', OwnerId=q.CreatedById, QuestionId=q.Id, CommunityId=q.CommunityId, Subject=q.Title, Description = (q.Body == null? null: q.Body.stripHtmlTags()), AccountId=q.CreatedBy.AccountId, ContactId=q.CreatedBy.ContactId);
                insert newCase;
            }
        } catch (Exception e) {
            String subjectText = 'Case Escalation exception in site ' + Site.getName();
            String bodyText = 'Case Escalation on Question having ID: ' + q.Id + ' has failed with the following message: ' + e.getMessage() +
                '\n\nStacktrace: ' + e.getStacktraceString();

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] { Site.getAdminEmail() };

            mail.setReplyTo('no-reply@salesforce.com');
            mail.setSenderDisplayName('Salesforce Chatter Answers User');

            // The default sender is the portal user causing this trigger to run, to change this, set an organization-wide address for
            // the portal user profile, and set the ID in the following line.
            // mail.setOrgWideEmailAddressId(orgWideEmailAddressId);
            mail.setToAddresses(toAddresses);
            mail.setSubject(subjectText);
            mail.setPlainTextBody(bodyText);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}