• SKT
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 6
    Replies
I have a requirement to display Accounts-->child Accounts-->Tasks-->Child Tasks in the JSON.

Here is my Apex code:
 
List<Account> accountRecordsList = new list<Account>(); 
List<Custom_Tasks__c> taskRecordsList = new list<Custom_Tasks__c>(); 

accountRecordsList = [SELECT Id,Name,parent_account__c, (SELECT Id, Name FROM Accounts1__r) FROM Account WHERE Id = '<some record id>'];
string strJSON = JSON.serialize(accountRecordsList); //Account and Child Accounts

 Set<Id> accountIds = new Set<Id>();
        for(Account pr : accountRecordsList){
            for(Account cpr : pr.Accounts1__r){
            if(cpr.id != null){
                accountIds.add(cpr.id);
            }
            }
        }
taskRecordsList = [SELECT id, name,account__c, (SELECT id, name,pse__Start_Date__c, pse__End_Date__c from Sub_Tasks1__r ) from Custom_Tasks__c where account__c in :accountIds];
string ptJSON = JSON.serialize(taskRecordsList); //Tasks and Child Tasks related to above obtained child accounts

From the above code,
  • in strJSON, I am getting Accounts and its child accounts data.
  • in ptJSON, I am getting Tasks and Child Tasks related to above Child accounts
Now, is there anyway, I can append ptJSON under its related child Accounts in strJSON. Can anyone please suggest solution on how to get this done.

Thanks! 
  • August 03, 2021
  • Like
  • 0
I have a requirement to fetch Parent, Child, and Grandchild and Store them as JSON in the same hierarchy.

Here is my logic:
 
public List<Parent_Project__c> ProjectRecordsList {get; set;}
    ProjectRecordsList = [SELECT Id, Name,Parent_Project__c, (SELECT Id, Name, (select name from Orders__r) FROM Child_Projects__r) FROM Parent_Project__c];
    string strJSON = JSON.serialize(ProjectRecordsList);

Now the issue here is that, when I run this query, I am getting the error as
SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object.

I could able to fetch Parent Project and its child Project successfully whereas when I include Orders (Grandchild), I am seeing this error. I know that I can write queries 5 levels up if I start with Child but in this case, I need to fetch the records starting from Parent to Grandchild.

Can anyone please let me know how to add Orders of Child Projects in the JSON?
  • July 04, 2021
  • Like
  • 0
I have a requirement to display Account and its child Accounts using Gantt Chart. Here is my Logic:

Controller:
public with Sharing class Account_Gantt {
    public Account ParentRecord{get; set;}
    public Account ChildRecord{get; set;}
    public Account_Gantt() {
        ParentRecord = [SELECT Id, Name,start_date__c, end_date__c FROM Account  WHERE name = 'Test Parent Account'];
    }
    public Account_Gantt(Account_Gantt Controller){
         ChildRecord = [SELECT ID, Name, start_date__c, end_date__c from Account where parent_account__r.name = 'Test Parent Account'];
    }
}

VF Page:
<apex:page controller="Account_Gantt"  extensions="Account_Gantt" standardStylesheets="false" showHeader="false">
    <head>
       <apex:stylesheet value="{!URLFOR($Resource.Timeline, 'codebase/dhtmlxgantt.css')}"/>
    <apex:includeScript value="{!URLFOR($Resource.Timeline, 'codebase/dhtmlxgantt.js')}"/>
        <style type="text/css" media="screen">
            html, body { margin:0px; padding:0px; height:100%; overflow:hidden; }
        </style>
    </head>
    <body>
        <div id="gantt_here" style='width:100%; height:100%;'></div>
        <script type="text/javascript">
            let ganttDataArr = [];
            for(let newParntObj=1; newParntObj <=1; newParntObj++ ) {
                let ganttDataObj = {};
                ganttDataObj["id"] = newParntObj;
                ganttDataObj["text"] = "{!ParentRecord.Name}";
                ganttDataObj["start_date"] = new Date("{!ParentRecord.start_date__c}");
                ganttDataObj["duration"] = 18;
                //ganttDataObj["order"] = "10";
                ganttDataObj["progress"] = "0.6";
                ganttDataObj["open"] = true;
                ganttDataArr.push(ganttDataObj);
            }
        
            for(let newObj = ganttDataArr.length+1; newObj <= 15; ++newObj ) {
                let ganttDataObj = {};
                ganttDataObj["id"] = newObj;
                ganttDataObj["text"] = "{!ChildRecord.Name}";
                ganttDataObj["start_date"] = new Date("{!ParentRecord.start_date__c}");
                ganttDataObj["duration"] = newObj*2;
                ganttDataObj["order"] = '10';
                ganttDataObj["progress"] = newObj%2;
                ganttDataObj["parent"] = '1';
                ganttDataArr.push(ganttDataObj);
            }

            let tasks =  {
                data: ganttDataArr,
                links:[
                    { id:1, source:1, target:2, type:"1"},
                    { id:2, source:2, target:3, type:"0"},
                    { id:3, source:3, target:4, type:"0"},
                    { id:4, source:2, target:5, type:"2"},
                ]
            };
                    
            gantt.init("gantt_here");
            gantt.parse(tasks);
                    
        </script>        
    </body>
</apex:page>


Under the Parent Account, I have 15 child Accounts. The issue here is that I could able to see Parent Account Name in the chart but not Child Accounts. I am seeing only the blank values instead of proper account names. The screenshot is as follows:

Gantt


Can anyone please let me know how to display all the 15 child account names under the parent account in the chart?
  • July 01, 2021
  • Like
  • 0
I have a requirement to fetch Contact Region from Contact and update the same on User.

Here is my logic:
 
list<user> user = [SELECT name,Region__c from User where isactive = true];
  Set<string> str = new Set<string>();
        for(user u1 : user){
            str.add(u1.name);
        }

        List <Contact> Contact = [
 select id,Salesforce_User__c,Contact_Region__r.name  from contact where Contact_Region__c != null];
                                      
                                      
        Map<id, list<String>> usertcMap = new Map<id, list<String>> ();
        for (Contact Contacts : Contact) {
            if(!usertcMap.containsKey(Contacts.Salesforce_User__c))
                usertcMap.put(Contacts.Salesforce_User__c, new list<string>{Contacts.Contact_Region__r.name});
        else
            usertcMap.get(Contacts.Salesforce_User__c).add(Contacts.Contact_Region__r.name);
        }
        list<User> userupdates = new list<user>();
        for(User us : user){ 
            if(usertcMap.containsKey(us.id))
            {
                 
                list<string> allProsList =usertcMap.get(us.id);
                us.Region__c = allProsList;
                userupdates.add(us);
            }
        }

        if(!userupdates.IsEmpty())
            update userupdates;


now when i am trying to run the above logic, i am seeing this error:
Illegal assignment from List<String> to String

Can anyone please let me know how to overcome this error?
Thanks!
  • June 18, 2021
  • Like
  • 0
I have a requirement to get all the Project Ids of Resource under User Detail page.

Below is my Object Schema:
Parent: Project (Field: ProjectId)
Child: Timecard (Field: Project__c, Resource_Name__c)


Now, if the Resource Name under timecard matches with User Name, the Project Ids from that timecard should be stored on User detail page separated by a semicolon.
Below is my logic that I have achieved so far:
 
list<user> user = [SELECT  name from User where isactive = true]; 
list<string> str = new list<string>();
for(user u1 : user){
    str.add(u1.name);
}

List <Timecard__c> timecard = [
  SELECT ID,Project__c,resource_name__c  
  FROM Timecard__c 
  Where Resource_name__c = :str  and Project__c != null and Resource_Name__c != null];
            
    set<string> allProsset = new set<string>();
    for (Timecard__c timecards : timecard) {
      allProsset.add(timecards.Project__c);
    }
    list<string>allProsList = new list<string>(allProsset);
    list<User> userupdates = new list<user>();
    
    for(User us : user){
     
        us.Project_Ids__c = String.join(allProsList,';');
        userupdates.add(us);
        }
        
    update userupdates;

Now when i execute the above logic, Project Ids under User record is getting updated with some random Project IDs but not from the timecard where the User name is matching. Instead of pulling all the project Ids that the resource is working under, I am seeing some random project ids


Is there anyway that I can update user records with the exact project ids that matches resource name with user name in timecard under a project?


 
  • June 16, 2021
  • Like
  • 1
Below is my Object Schema:
  • Parent: User
  • Child: Contact
  • Grand-Child: Sponsor. Sponsor Holds a field called ProId. These as 16 digit Ids.
Now, my requirement is to get all the ProIds from the sponsor records populate on the User record as a multi-select picklist value. The field on User is named All_Pros__c
For eg: On User A, All_Pros__c field should hold values as 'id1';'id2';'id3'.

Can anyone please suggest any approach on ths regard.
  • June 09, 2021
  • Like
  • 0
I know this isnt possible, but is there anyway that we can use transformed lens to an Einstein Discovery Story.

We have some complex calculations that are built using formula in lens and we want the same in Einstein Discovery Story. Along with that we have also columns created for standard deviation which we want to use in our story.

Instead of uploading the calculated columns in csv and using them in Story, can anyone please let me know if there is any possibility lf using lens itself while building the story.
Thanks!
  • March 15, 2021
  • Like
  • 0
I have a requirement to get all the Project Ids of Resource under User Detail page.

Below is my Object Schema:
Parent: Project (Field: ProjectId)
Child: Timecard (Field: Project__c, Resource_Name__c)


Now, if the Resource Name under timecard matches with User Name, the Project Ids from that timecard should be stored on User detail page separated by a semicolon.
Below is my logic that I have achieved so far:
 
list<user> user = [SELECT  name from User where isactive = true]; 
list<string> str = new list<string>();
for(user u1 : user){
    str.add(u1.name);
}

List <Timecard__c> timecard = [
  SELECT ID,Project__c,resource_name__c  
  FROM Timecard__c 
  Where Resource_name__c = :str  and Project__c != null and Resource_Name__c != null];
            
    set<string> allProsset = new set<string>();
    for (Timecard__c timecards : timecard) {
      allProsset.add(timecards.Project__c);
    }
    list<string>allProsList = new list<string>(allProsset);
    list<User> userupdates = new list<user>();
    
    for(User us : user){
     
        us.Project_Ids__c = String.join(allProsList,';');
        userupdates.add(us);
        }
        
    update userupdates;

Now when i execute the above logic, Project Ids under User record is getting updated with some random Project IDs but not from the timecard where the User name is matching. Instead of pulling all the project Ids that the resource is working under, I am seeing some random project ids


Is there anyway that I can update user records with the exact project ids that matches resource name with user name in timecard under a project?


 
  • June 16, 2021
  • Like
  • 1
I have a requirement to display Accounts-->child Accounts-->Tasks-->Child Tasks in the JSON.

Here is my Apex code:
 
List<Account> accountRecordsList = new list<Account>(); 
List<Custom_Tasks__c> taskRecordsList = new list<Custom_Tasks__c>(); 

accountRecordsList = [SELECT Id,Name,parent_account__c, (SELECT Id, Name FROM Accounts1__r) FROM Account WHERE Id = '<some record id>'];
string strJSON = JSON.serialize(accountRecordsList); //Account and Child Accounts

 Set<Id> accountIds = new Set<Id>();
        for(Account pr : accountRecordsList){
            for(Account cpr : pr.Accounts1__r){
            if(cpr.id != null){
                accountIds.add(cpr.id);
            }
            }
        }
taskRecordsList = [SELECT id, name,account__c, (SELECT id, name,pse__Start_Date__c, pse__End_Date__c from Sub_Tasks1__r ) from Custom_Tasks__c where account__c in :accountIds];
string ptJSON = JSON.serialize(taskRecordsList); //Tasks and Child Tasks related to above obtained child accounts

From the above code,
  • in strJSON, I am getting Accounts and its child accounts data.
  • in ptJSON, I am getting Tasks and Child Tasks related to above Child accounts
Now, is there anyway, I can append ptJSON under its related child Accounts in strJSON. Can anyone please suggest solution on how to get this done.

Thanks! 
  • August 03, 2021
  • Like
  • 0
I have a requirement to fetch Parent, Child, and Grandchild and Store them as JSON in the same hierarchy.

Here is my logic:
 
public List<Parent_Project__c> ProjectRecordsList {get; set;}
    ProjectRecordsList = [SELECT Id, Name,Parent_Project__c, (SELECT Id, Name, (select name from Orders__r) FROM Child_Projects__r) FROM Parent_Project__c];
    string strJSON = JSON.serialize(ProjectRecordsList);

Now the issue here is that, when I run this query, I am getting the error as
SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object.

I could able to fetch Parent Project and its child Project successfully whereas when I include Orders (Grandchild), I am seeing this error. I know that I can write queries 5 levels up if I start with Child but in this case, I need to fetch the records starting from Parent to Grandchild.

Can anyone please let me know how to add Orders of Child Projects in the JSON?
  • July 04, 2021
  • Like
  • 0
I have a requirement to get all the Project Ids of Resource under User Detail page.

Below is my Object Schema:
Parent: Project (Field: ProjectId)
Child: Timecard (Field: Project__c, Resource_Name__c)


Now, if the Resource Name under timecard matches with User Name, the Project Ids from that timecard should be stored on User detail page separated by a semicolon.
Below is my logic that I have achieved so far:
 
list<user> user = [SELECT  name from User where isactive = true]; 
list<string> str = new list<string>();
for(user u1 : user){
    str.add(u1.name);
}

List <Timecard__c> timecard = [
  SELECT ID,Project__c,resource_name__c  
  FROM Timecard__c 
  Where Resource_name__c = :str  and Project__c != null and Resource_Name__c != null];
            
    set<string> allProsset = new set<string>();
    for (Timecard__c timecards : timecard) {
      allProsset.add(timecards.Project__c);
    }
    list<string>allProsList = new list<string>(allProsset);
    list<User> userupdates = new list<user>();
    
    for(User us : user){
     
        us.Project_Ids__c = String.join(allProsList,';');
        userupdates.add(us);
        }
        
    update userupdates;

Now when i execute the above logic, Project Ids under User record is getting updated with some random Project IDs but not from the timecard where the User name is matching. Instead of pulling all the project Ids that the resource is working under, I am seeing some random project ids


Is there anyway that I can update user records with the exact project ids that matches resource name with user name in timecard under a project?


 
  • June 16, 2021
  • Like
  • 1