• Adnan P
  • NEWBIE
  • 30 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 3
    Replies
Hello,

We have some custom code that allows our users to add Attachments to a newly created Case. We are creating Cases via a customer controller and VF page. In Lightning Experience, Attachments are being replaced by Files.

Below is a snippet of code that we use to insert Attachments. Since Files is replacing Attachments does anyknow what I can use instead? In the future I would like to insert Files instead of Attachments.I can't find a File class in the SOAP API Developer Guide. Any feedback would be greatly appreciated!
 
public void insertAttachment() {
		try {
			insert new Attachment(ParentId = newCase.Id, OwnerId = UserInfo.getUserId(), Body = attachmentBody, Name = attachmentFilename);
			attachmentNameList.add(attachmentFilename);
			attachmentSuccess = true;
		} catch (DmlException e) {
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error inserting attachment ' + attachmentFilename + ' to Case #' + caseNumber + '.\nError: ' + e.getMessage()));
			attachmentSuccess = false;
		}
	}

 
Hello,

I'm hoping to get a little help with a unit test for the code below. Basicially, we have a custom clone button and I'm trying to check whether a user is trying to clone a Case with an inactive Record Type. If so, I'd like to display a warning message that let's the user know that a Case with an inactive Record Type cannot be cloned. The cloneCase() method from my controller is shown below.
 
public PageReference cloneCase() {
    	List<String> fieldsList = new List<String> {'RecordType.isActive'};
        Case parentCase = (Case)Database.query(SelectStarService.createSelectStarSoql('Case', fieldsList) + ' WHERE Id = :parentCaseId');
        if (parentCase.RecordType.isActive) {
	        clonedCase = parentCase.clone(false, true, false, false);
	        clonedCase = validatePicklistValues();
	        clonedCase.OwnerId = UserInfo.getUserId();
	        clonedCase.Cloned__c = true;
	        clonedCase.Origin = 'Cloned';
	        clonedCase.Incident_Date__c = System.now();
	        insert clonedCase;
	        PageReference pr = new PageReference('/' + clonedCase.Id + '/e?retURL=%2F' + clonedCase.Id);
	        return pr;
        } else {
        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING,
        	'You cannot clone a Case with an inactive Record Type.');
        	ApexPages.addMessage(msg);
        	return null;
        }
    }

I've tested and confirmed that the code above works in my sandbox. The issue I'm having is writing the unit test to trigger the else statement above. I can't figure out how to update the Record Type in my unit test so that the testCase has an inactive Record Type when a new Case is cloned from the testCase. I assumed I could insert testCase with an active Record Type and the perform an update to assign an inactive Record Type to testCase but it doesn't look like the system allows me to do that.

I get a "Attempt to de-reference a null object" when running the test. Obviously, this makes sense but I'm not sure how to fix it. I'm fairly new to coding so I'm not sure if there's something obvious that I'm mising. Any feedback would be greatly appreciated.
 
@isTest
	public static void testCloneCaseInactiveRT(){
		Test.startTest();
		Case testCase = (Case)SmartFactory.createsObject('Case');
		RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType = 'Case' AND IsActive = false LIMIT 1];
		testCase.RecordType.Id = rt.Id;
		insert testCase;
		ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
		CaseCloneController controller = new CaseCloneController(sc);
		PageReference pr = controller.cloneCase();
		System.assertEquals(null, controller.clonedCase.Id);
		Test.stopTest();
	}
Thank you!


 
Hello,

I'm attempting to write a test method to test a piece of code where a user is attempting to create a custom object record that has an Opportunity lookup on the page layout.  What I'm trying to test is to display an error if the user creating the custom object record does not have access to the related Opportunity.  The error should be displayed when the user clicks 'Save' to save the custom object record.  

What I'm having difficulty with is being able to write the test method and tell it to insert the custom object record but also to make sure that the user does not have access to the Opportunity in the lookup field.  Also, this test class has isTest(SeeAllData=true).

Can someone please point me in the right direction on how this may be accomplished?  Any responses would be greatly appreciated!

sniper of code being tested:
for (Internal_Request__c ir : newInternalRequestList) {
            Opportunity myOpp = oppMap.get(ir.Opportunity__c);
            if (myOpp == null) {
                    ir.addError('You do not have access to this Opportunity or the Opportunity does not exist');
            } else if (invalidStageNameSet.contains(oppMap.get(ir.Opportunity__c).StageName)) {
                    ir.addError('Unable to associate an Opportunity with a "Closed Lost" or "Closed:  No Bid" with an Internal Request.');
                }
            }
  • September 26, 2016
  • Like
  • 0
Hello,

I'm trying to write a method that will allow me to clone Attachments (Type = File) when a Campaign is auto-created from a custom object.  I have a trigger that auto-creates a Campaign when Status = Approved on a record for the custom object called Internal_Request__c.  I would like to copy all Attachments forom the parent record and attach them to the newly created Campaign.

I was able to get the method to work when Type = Attachment for the Attachment on the parent record.  But when I try to clone and insert Attachments where Type = File I get the error "DML operation INSERT not allowed on ContentDocument".  Does anyone know if this is possible any other way?  Any feedback would be greatly appreciated.  I'm a beginner developer so I apologize if any of the below is not clear.

// ***This method works without any issues***
private void copyAttachmentsFromIr(List<Campaign> newList) {
    Map<Id, Id> idMap = new Map<Id, Id>();
    for(Campaign c : newList) {
      idMap.put(c.Internal_Request__c, c.Id);
    }
    Set<Id> irIdSet = idMap.keySet();
    List<Attachment> iRAttachmentList = (List<Attachment>)Database.query(SelectStarService.createSelectStarSoql('Attachment') + ' WHERE ParentId = :irIdSet');
    List<Attachment> campaignAttachmentList = new List<Attachment>();
    for(Attachment a : iRAttachmentList) {
      Attachment clonedAttachment = a.clone(false, true, false, false);
      clonedAttachment.ParentId = idMap.get(a.ParentId);
      campaignAttachmentList.add(clonedAttachment);
    }
    if (!campaignAttachmentList.isEmpty()) {
      insert campaignAttachmentList;
    }
  }


// ***I get the "DML operation INSERT not allowed on ContentDocument" when trying to insert***
  private void copyFilesIr(List<Campaign> newList) {
    Map<Id, Id> idMap = new Map<Id, Id>();
    for(Campaign c : newList) {
      idMap.put(c.Internal_Request__c, c.Id);
    }
    Set<Id> irIdSet = idMap.keySet();
    List<ContentDocument> iRFileList = (List<ContentDocument>)Database.query(SelectStarService.createSelectStarSoql('ContentDocument') + ' WHERE ParentId = :irIdSet');
    List<ContentDocument> campaignFileList = new List<ContentDocument>();
    for(ContentDocument a : iRFileList) {
      ContentDocument clonedFile = a.clone(false, true, false, false);
      clonedFile.ParentId = idMap.get(a.ParentId);
      campaignFileList.add(clonedFile);
    }
    if (!campaignFileList.isEmpty()) {
      insert campaignFileList;
    }
  }
Hello,

I was hoping someone could provide some insight with an issue that I'm having.  I'm attempting to create a custom Clone button that will validate whether any picklist values being copied over are inactive.  If the picklist value is no longer active it would be cleared and the user would be forced to select a new value on the edit page before saving.  

The issue that I'm having is that I don't know how the take the user to the edit page of the cloned Case without inserting the Case first because I need the ID of the cloned Case for the PageReference.  The problem is that once the Case is inserted the user could click "Cancel" on the edit page assuming that the cloned Case will not be created but it has already been inserted.  Obviously, this may create junk data.

My understanding of Apex/Visualforce is limited.  Does anyone know if there's a way to land on the edit page for the cloned Case without having to insert the Case first?  I don't want to insert until the User clicks "Save" on the edit page.

Thanks in advance!!
Hello,

Does anyone know if it's possible to submit a record for approval using a custom button?  I would like to create three different approval processes where each approval process would have it's own custom submission button.

Each approval process would have the same entry criteria. Depending on which approval button the users clicks that record would be submitted for approval using the approval process tied to that particular custom approval button.

So instead of having just a single "Submit for Approval" button there would be three different buttons that the users could click to kickoff an approval process.

Any feedback would be greatly appreciated.

Thank you!
  • September 03, 2014
  • Like
  • 1

Can anyone please help with creating a validation rule that prevents a certain Profile Name from being able to change the "Assigned To' field.  All other profile names should be able to change the 'Assigned To' unless Profile = 'C User'

Hello,

Does anyone know if it's possible to submit a record for approval using a custom button?  I would like to create three different approval processes where each approval process would have it's own custom submission button.

Each approval process would have the same entry criteria. Depending on which approval button the users clicks that record would be submitted for approval using the approval process tied to that particular custom approval button.

So instead of having just a single "Submit for Approval" button there would be three different buttons that the users could click to kickoff an approval process.

Any feedback would be greatly appreciated.

Thank you!
  • September 03, 2014
  • Like
  • 1
Hello,

I'm attempting to write a test method to test a piece of code where a user is attempting to create a custom object record that has an Opportunity lookup on the page layout.  What I'm trying to test is to display an error if the user creating the custom object record does not have access to the related Opportunity.  The error should be displayed when the user clicks 'Save' to save the custom object record.  

What I'm having difficulty with is being able to write the test method and tell it to insert the custom object record but also to make sure that the user does not have access to the Opportunity in the lookup field.  Also, this test class has isTest(SeeAllData=true).

Can someone please point me in the right direction on how this may be accomplished?  Any responses would be greatly appreciated!

sniper of code being tested:
for (Internal_Request__c ir : newInternalRequestList) {
            Opportunity myOpp = oppMap.get(ir.Opportunity__c);
            if (myOpp == null) {
                    ir.addError('You do not have access to this Opportunity or the Opportunity does not exist');
            } else if (invalidStageNameSet.contains(oppMap.get(ir.Opportunity__c).StageName)) {
                    ir.addError('Unable to associate an Opportunity with a "Closed Lost" or "Closed:  No Bid" with an Internal Request.');
                }
            }
  • September 26, 2016
  • Like
  • 0
Hello,

Does anyone know if it's possible to submit a record for approval using a custom button?  I would like to create three different approval processes where each approval process would have it's own custom submission button.

Each approval process would have the same entry criteria. Depending on which approval button the users clicks that record would be submitted for approval using the approval process tied to that particular custom approval button.

So instead of having just a single "Submit for Approval" button there would be three different buttons that the users could click to kickoff an approval process.

Any feedback would be greatly appreciated.

Thank you!
  • September 03, 2014
  • Like
  • 1

Can anyone please help with creating a validation rule that prevents a certain Profile Name from being able to change the "Assigned To' field.  All other profile names should be able to change the 'Assigned To' unless Profile = 'C User'