Not able to insert AccountPartner through test class in salesforce - salesforce

I was not able to insert mock data for AccountPartner through test class. I tried by inserting 2 accounts and one Partner object. Actually same code is working in case of running from Apex class or executing from developer console. Please see the below code and output and let me know solution..
#isTest
private class TestAccountPartner {
private static testmethod void unittest(){
test.startTest();
Account sourceAccount = new Account();
Account targetAccount = new Account();
sourceAccount.AccountNumber='Number1';
sourceAccount.Name='name1';
targetAccount.AccountNumber='Number2';
targetAccount.Name='name2';
insert sourceAccount;
insert targetAccount;
Partner p = new Partner(AccountFromId=sourceAccount.ID, AccountToId=targetAccount.ID);
insert p;
Account sa = [select ID from Account where Name='name1'];
Account ta = [select ID from Account where Name='name2'];
System.debug('Source Account is: '+sa);
System.debug('Target Account is: '+ta);
List<Partner> partners = [select AccountFromId from partner];
System.debug('Partner objects are: '+partners);
List<AccountPartner> accountPartners = [select AccountFromId from AccountPartner];
System.debug('account partners are: '+accountPartners);
test.stopTest();
}
}
Output is:
07:24:48:174 USER_DEBUG [26]|DEBUG|Source Account is: Account:{Id=00128000007YZk7AAG}
07:24:48:174 USER_DEBUG [27]|DEBUG|Target Account is: Account:{Id=00128000007YZk8AAG}
07:24:48:176 USER_DEBUG [30]|DEBUG|Partner objects are: (Partner:{AccountFromId=00128000007YZk7AAG, Id=00I28000000ZeBTEA0})
07:24:48:177 USER_DEBUG [33]|DEBUG|account partners are: ()

It's been a while but I think this is because account partner is one of the special Role type relationships. Have you tried accessing the Target account's AccountPartner collection and using the add() method to add the Source account.
This isn't identical to your situation, but the solution has a similar example that is adding partners:
https://salesforce.stackexchange.com/questions/3805/how-to-update-account-object-when-related-accountpartner-is-updated-inserted-del

Related

Microsoft graph api returning NULL in Department field

I have given all required permissions for retrieving user info from Azure active directory like User.Read.All and User.Read but still getting Department and EmployeeId field as NULL
code used:
public Microsoft.Graph.User GetUser(ref GraphServiceClient graphServiceClient, string UserId)
{
return graphServiceClient.Users[UserId].Request().GetAsync().Result;
}
According to the documentation both department and employeeId are returned only on $select.
Use Select method and specify those two properties. If you need to return more properties, they need to be specified in Select. Without using Select, Graph API return only default set of properties.
public Microsoft.Graph.User GetUser(ref GraphServiceClient graphServiceClient, string UserId)
{
return graphServiceClient.Users[UserId]
.Request()
.Select("department,employeeId")
.GetAsync()
.Result;
}
Resources:
Select parameter

Salesforce Apex Test Class for an auto convert apex class. REceiving errors moving to production

I have an apex class that works how I want it in the sandbox. Trying to move it to production and need to write a test class. Used an example provided but I continue to get errors when running test that the lead was already converted. Need Help!
#isTest
public class TestAutoConvertLeads{
static testMethod void createnewlead() {
User userToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
Test.startTest();
Lead leadToCreate =new Lead();
List<id> Ids= New List<Id>();
leadToCreate.ownerid= userToCreate.id;
leadToCreate.LastName ='Gupta';
leadToCreate.LeadSource='Partner Referral';
leadToCreate.Rating='';
leadToCreate.Status='';
insert leadToCreate;
Ids.add(leadToCreate.id);
AutoConvertLeads.LeadAssign(Ids);
Test.stopTest();
}
}
Also adding original apex class:

How to avoid ORA-00001 in multi target pickList for a table with combined Unique key

I have a table UserRoles with unique_key(fk_User_Id,fk_role_Id) on ORACLE DB
ID | fk_USER_ID | fk_ROLE_ID
1 | john | 1
and for frontend, I have PrimeFaces pickList that has a source(this.allRoles) and target(roleTarget).
<p:pickList id="rolePickList"
value="#{userBean.rolesDualModel}"
var="_role"
styleClass="width100"
effect="slide"
itemValue="#{_role}"
itemLabel="#{_role.name}"
showSourceFilter= "true"
showTargetFilter="true"
filterMatchMode="contains"
effectSpeed="fast">
<o:converter converterId="omnifaces.ListConverter" list="#{userBean.allRoles}" />
<f:facet name="sourceCaption">Role Available</f:facet>
<f:facet name="targetCaption">Role Selected</f:facet>
</p:pickList>
When I select a user from User datatable, rowSelectListener() brings the User with his current roles.
All roles take place in source this.allRoles(except the User's current roles) and the User's own(current) roles take place in the roleTarget.
The below method getting all roles from Role Table
private void initializeDualRoles()
{
this.allRoles = this.getService().findAll(Role.class);
this.rolesDualModel = new DualListModel<>(this.allRoles, roleTarget);
}
The below method getting the User's current roles to the target
private void adjustDualRoles()
{
List<Role> rolesSource = this.getService().findAll(Role.class);
if (CollectionUtils.isNotEmpty(rolesSource))
{
rolesSource.removeAll(this.getModel().getRoles());
}
rolesDualModel = new DualListModel<>(rolesSource, this.getModel().getRoles());
}
when I try to set a new Role to a User with Hibernate SaveOrUpdate() method;
public void save()
{
for (Role userRoles : rolesDualModel.getTarget())
{
this.getUserRolesBean().getModel().setUserIsn(this.getModel());
this.getUserRolesBean().getModel().setRoleCode(userRoles);
this.getUserRolesBean().getService().saveOrUpdate(this.getUserRolesBean().getModel());
}
I got
ORA-0001
Error due to a combined unique key.
It is due to saving the same Model several times in for loop.
If I remove the Combined Unique Key Rule, the rows are duplicated.
Any Idea, How to avoid the error?

Attaching a Completed DocuSign Document to a Salesforce Standard Object (Not Opportunities)

I'm trying to attach a completed DocuSign document to the Contact or Account record that initiated the document (within Notes or Notes & Attachments).
I've found instructions for attaching documents to an Opportunity as well as attaching to custom objects. I've walked through these steps and tried to tailor the instructions for a Contact and Account record but with no success.
Please let me know the steps I can take to accomplish this. Much appreciated!
If you are communicating with docusign via a REST API then you should be receiving a base64 response. You would pass the response body into the 'execute' method below and it should insert to the specified contact and related account. The logic leading up to this would need to query the specific contact and account, but the code here is an example of pulling an existing salesforce ContentVersion and create a new content document from it. Also, you would need to either parse the incoming base64 data to determine the file information (extension, title) or match the extension within the response body. Your go to links would be as follows:
Salesforce Reference- ContentVersion
Salesforce Reference- EncodingUtil
Salesforce Reference- ContentDocumentLink
DocuSign API Docs- Envelopes
Hope this helps!
public with sharing class uploadDocument {
//Build this to accept whatever you need to relate the document to
public Contact ourContact {get; set;}
//Init
public uploadDocument() {
ourContact = [SELECT Id, Name, AccountId FROM Contact LIMIT 1];
}
public static void execute(String base64EncodedDocument){
uploadDocument classObject = new uploadDocument();
String extension;
//Conditional logic will need to be added if you want to handle other file types
if(base64EncodedDocument.contains('pdf')){
extension = 'pdf';
}
//Prep the raw document for insert via the ContentVersion class
Blob base64DecodedDocument = EncodingUtil.base64Decode(base64EncodedDocument);
ContentVersion sfDocumentObject = new ContentVersion();
sfDocumentObject.VersionData = base64DecodedDocument;
sfDocumentObject.Title = 'Our Test Document';
sfDocumentObject.FirstPublishLocationId = classObject.ourContact.Id;
sfDocumentObject.PathOnClient = '//' + sfDocumentObject.Title + '.' + extension;
insert sfDocumentObject;
//Add additional references for the document
List<additionalIdLink> additionalLinks = new List<additionalIdLink>();
additionalIdLink accountLink = new additionalIdLink();
accountLink.linkedId = classObject.ourContact.AccountId;
accountLink.documentId = sfDocumentObject.Id;
additionalLinks.add(accountLink);
linkToAdditionalRecords(JSON.serialize(additionalLinks));
}
//Ignore, just for testing purposes
//I uploaded a document just to test it and everything worked just fine, preview includedS
private static String setupExample(){
ContentVersion ourTestDocument = [SELECT Id, FileExtension, FileType, Title, VersionData, VersionNumber FROM ContentVersion WHERE Title =: 'Getting Started With Salesforce' ORDER BY VersionNumber DESC][0];
String result = EncodingUtil.base64Encode(ourTestDocument.VersionData);
return result;
}
//Ignore, just for testing purposes
public static void executeExample(){
String base64DocumentReceived = setupExample();
execute(base64DocumentReceived);
}
//Quick Access class to pass the previous values to the Future method
public class additionalIdLink{
public String linkedId {get; set;}
public String documentId {get; set;}
}
//Allows you to link to as many additional records as you need. I haven't run into any use cases where you will be adding over 200 links at a time, but it will throw a query exception if you attempt that
#future
public static void linkToAdditionalRecords(String jsonLinks){
List<additionalIdLink> links = (List<additionalIdLink>)JSON.deserialize(jsonLinks, List<additionalIdLink>.class);
List<ContentDocumentLink> result = new List<ContentDocumentLink>();
for(additionalIdLink link : links){
ContentDocumentLink accountLink = new ContentDocumentLink();
String documentId = [SELECT Id, VersionNumber, ContentDocumentId FROM ContentVersion WHERE Id =: link.documentId LIMIT 1].ContentDocumentId;
accountLink.ContentDocumentId = documentId;
accountLink.LinkedEntityId = link.linkedId;
accountLink.ShareType = 'V';
accountLink.Visibility = 'AllUsers';
result.add(accountLink);
}
if(result.size() > 0){
insert result;
}
}
}

I to assign a default account to a custom object using Visualforce page

I am new to salesforce development. I am trying to create a visualforce page which helps to insert a new record into the custom object. Custom object has master-detail relationship with account. I am having issue when assiging a default value to the Account. The account already exists in the accounts table. Here is the Apex class I am trying.
public class RelatedAccount{
public Account parent {get; set;}
public RelatedAccount(ApexPages.StandardController controller){
Transaction__c child = (Transaction__c)controller.getRecord();
if (child.Account__c != null) { parent = [Select ID,Name FROM Account WHERE Account.Name = :'1Company Inc.,' LIMIT 1];
child.Account__c = parent;
}
}}
I am getting the error : "Illegal assignment from Account to Id"
Thanks In advance.
This should work:
child.Account__c = parent.Id
In your case you try to put the "whole" account object object into the Lookup field. But this just needs the Id of the parent account.

Resources