• Sabah Farhana
  • NEWBIE
  • 50 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 7
    Replies
Hi All,

Since rollup summary(count) is available only for master detail how do i get this count of child objects for a lookup relationship.

Trigger is a solution but how would that update existing records?

Is it possible to update existing records with trigger.Please help with sample code if this is possible

Please advise.
I need to get the count for a report
Hi All ,

I have a custom object Opportunity_Attachment__c which has master-detail with Opportunity(master).

I have a button on the  opportunity attachment related list on the opportunity page,which creates an attachment .

Now i tried to modfy the code where i want to redirect after creating the attachment to another page where i add one more attachment

The redirect is working ,but the insert on the second page is giving an error as Iam not able to pass all the id's in the first page.
Please help me modify the code to pass the id's

when iam at the first page

browser has:
https://c.cs18.visual.force.com/apex/OpportunityAttachment1?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

At the second page its just:
https://c.cs18.visual.force.com/apex/OpportunityAttachment2

So how do i pass all the parameters from first page to the second so that second page has:

https://c.cs18.visual.force.com/apex/OpportunityAttachment2?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

Please find the controller codes below.

On the second page iam getting an insert error as id's are not passed.

Please help.
************************

public with sharing class OpportunityAttachmentController1{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController1(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        //return new PageReference('/'+objOppAtt.Opportunity__c); 
    PageReference congratsPage = Page.OpportunityAttachment2;


//Opportunityattachment2 controller is OpportunityAttachmentController2,its just exactle the same page to add one more attachment
//how to pass all id's here to OpportunityAttachmentController2

  congratsPage.setRedirect(true);

  return congratsPage;






    }
}

*************************************

public with sharing class OpportunityAttachmentController2{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController2(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        return new PageReference('/'+objOppAtt.Opportunity__c); 
  




    }
}


******************************
Hi ,

I have the following controller and Vf page that adds one attachment

controller:

public with sharing class OpportunityAttachmentController{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
 
    public OpportunityAttachmentController(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
     
    }

    public Attachment attach {get;set;}
    public Attachment attach1 {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert

    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
     
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
       
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
      
        return new PageReference('/'+objOppAtt.Opportunity__c); 
    }
  
  
  
  
  
}


VF page:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
              
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" /></apex:pageBlockSectionItem>
              
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


I want to add one more attachment  so i modify the vf as follows:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
             
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />
            <apex:pageBlockSectionItem >Upload Attributes<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />

</apex:pageBlockSectionItem>
             
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


do i have to write a seperate controller,or i can modify the same?If modify the same ,how?
Hi ,

I have the following controller and Vf page that adds one attachment

controller:

public with sharing class OpportunityAttachmentController{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
 
    public OpportunityAttachmentController(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
     
    }

    public Attachment attach {get;set;}
    public Attachment attach1 {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert

    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
     
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
       
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
      
        return new PageReference('/'+objOppAtt.Opportunity__c); 
    }
  
  
  
  
  
}


VF page:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
              
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" /></apex:pageBlockSectionItem>
              
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


I want to add one more attachment  so i modify the vf as follows:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
             
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />
            <apex:pageBlockSectionItem >Upload Attributes<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />


</apex:pageBlockSectionItem>
             
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


do i have to write a seperate controller,or i can modify the same?If modify the same ,how?

Please help.

Hi ,

I have the following controller and Vf page that adds one attachment

controller:

public with sharing class OpportunityAttachmentController{
   
    public Opportunity_Attachment__c objOppAtt {get; set;}
  
    public OpportunityAttachmentController(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
      
    }

    public Attachment attach {get;set;}
    public Attachment attach1 {get;set;}
  
    //When user clicks upload button on Visualforce Page, perform upload/insert

    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
       
        attach.ParentId = objOppAtt.id;
        insert attach;
       
        
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
       
        return new PageReference('/'+objOppAtt.Opportunity__c);  
    }
   
   
   
   
   
}


VF page:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
               
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" /></apex:pageBlockSectionItem>
               
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


I want to add one more attachment  so i modify the vf as follows:
<apex:page standardController="Opportunity_Attachment__c" extensions="OpportunityAttachmentController">
    <apex:sectionHeader title="New Opportunity Attachment" subtitle="Opportunity Attachment Edit"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
              
                <apex:outputField value="{!Opportunity_Attachment__c.Opportunity__c}"/>
                <apex:pageBlockSectionItem >Upload Contract<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />
            <apex:pageBlockSectionItem >Upload Attributes<apex:inputFile required="true" value="{!attach.body}" filename="{!attach.name}" />

</apex:pageBlockSectionItem>
              
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


do i have to write a seperate controller,or i can modify the same?If modify the same ,how?

Please help.
 

Hi ,

I have an apex trigger as follows

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
   
      if(c.groupid__c!= null){
       c.group_Account__c = [select id from account where Group_ID__c=:c.groupid__c].ID;
                
         
         
    }
  }
}

The line marked in bold is not getting tested


I have written a test class for it as below.Please help.

@isTest


    private class testUpdateLookup {
  
public  static testMethod void testocc(){
   

    
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCity='Dubai',
             BillingCountry='United Arab Emirates'
            
       );
         insert acc;
       
        Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];
    
     Contact con=new Contact(
        LastName='blah',
        AccountId=accCreated.Id,
        level__c='Other',
        Role__c='Customer Support',
        LeadSource='Consumer Referral'
   
    );
        insert con;
    
           
           
           
            // TEST ADDING A NEW case                       
      Case cas = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email',
                groupid__c=acc.group_id__c
       );
         insert cas;

//groupid null
       Case cass = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email'
               
       );
         insert cass;   

       
       
     }
}
Hi all,

Iam getting an error in the line highlighted below.Is this the correct way to write it

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
  
      if(c.groupid__c!= null){
     
               
        
          c.group_Account__c = [select id from account where Group_ID__c =c.groupid__c];
    }
  }

Hi all,

Iam getting an error in the line highlighted below.Is this the correct way to write it

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
   
      if(c.groupid__c!= null){
      
                
         
          c.group_Account__c = [select id from account where Group_ID__c =c.groupid__c];
    }
  }
}
Hi,

I have an apex trigger that updates the event custom field with opportunity Stage Value.The trigger is working fine.Now i want to move it to production.But having issues with the test class.

When i run the test class i get the error First Exception on row 0 insufficient access to cross reference entity Id.
Please help! Iam very bad at test classes.


The trigger:
trigger trigeventOpp on Event (before insert, before update) {

   
    Set<Id> opIds = new Set<Id>();
    for(Event t : trigger.new){
        String wId = t.WhatId;
                if(wId!=null && wId.startsWith('006') && !opIds.contains(t.WhatId))
                { opIds.add(t.WhatId);
                } }
    // Pull in opp ids and related field to populate Event record
    List<Opportunity> taskOps = [Select Id, StageName from Opportunity where Id in :opIds];
    Map<Id, Opportunity> opMap = new Map<Id, Opportunity>();
    for(Opportunity o : taskOps)
    { opMap.put(o.Id,o);  } // Update custom task field with  opp field
    for(Event t : trigger.new)
    { String wId = t.WhatId;
     if(wId!=null && wId.startswith('006'))
     { Opportunity thisOp = opMap.get(t.WhatId);
      if(thisOp!=null){t.Opportunity_stage__c = thisOp.StageName;}
                                                 } }
}

The test class

@isTest
private class TheTestClassEvent {
  
public static testMethod void testocc(){
   
     // GET USER FROM USER TABLE...
     User u = [select name, Id  FROM user LIMIT 1];
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCity='Dubai',
             BillingCountry='United Arab Emirates'
            
       );
         insert acc;
       
        Account accCreated = [Select Name From Account Where Id =: acc.Id LIMIT 1];
     // RUN AS USER...
     system.runAs(u)
     {
            Opportunity od = new Opportunity(
       Name = 'Distributor',
                AccountId=accCreated.id,
       Type='New Business',
                StageName='Pending',
                Stage_Notes__c='test opp',
                Edition__c='2014',
                LeadSource='Distributor',
                CloseDate=System.Today()
       );
         insert od;
           
           
           
            // TEST ADDING A NEW Event...                       
      Event eve = new Event(
       subject = 'Meeting test',
                whatId=od.Id
      
       );
         insert eve;
        

        
     }
     system.debug('----->>> RUNNING AS USER: ' + u.name);

   }       
}
Hi All,

Since rollup summary(count) is available only for master detail how do i get this count of child objects for a lookup relationship.

Trigger is a solution but how would that update existing records?

Is it possible to update existing records with trigger.Please help with sample code if this is possible

Please advise.
I need to get the count for a report
Hi All ,

I have a custom object Opportunity_Attachment__c which has master-detail with Opportunity(master).

I have a button on the  opportunity attachment related list on the opportunity page,which creates an attachment .

Now i tried to modfy the code where i want to redirect after creating the attachment to another page where i add one more attachment

The redirect is working ,but the insert on the second page is giving an error as Iam not able to pass all the id's in the first page.
Please help me modify the code to pass the id's

when iam at the first page

browser has:
https://c.cs18.visual.force.com/apex/OpportunityAttachment1?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

At the second page its just:
https://c.cs18.visual.force.com/apex/OpportunityAttachment2

So how do i pass all the parameters from first page to the second so that second page has:

https://c.cs18.visual.force.com/apex/OpportunityAttachment2?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

Please find the controller codes below.

On the second page iam getting an insert error as id's are not passed.

Please help.
************************

public with sharing class OpportunityAttachmentController1{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController1(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        //return new PageReference('/'+objOppAtt.Opportunity__c); 
    PageReference congratsPage = Page.OpportunityAttachment2;


//Opportunityattachment2 controller is OpportunityAttachmentController2,its just exactle the same page to add one more attachment
//how to pass all id's here to OpportunityAttachmentController2

  congratsPage.setRedirect(true);

  return congratsPage;






    }
}

*************************************

public with sharing class OpportunityAttachmentController2{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController2(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        return new PageReference('/'+objOppAtt.Opportunity__c); 
  




    }
}


******************************
Hi ,

I have an apex trigger as follows

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
   
      if(c.groupid__c!= null){
       c.group_Account__c = [select id from account where Group_ID__c=:c.groupid__c].ID;
                
         
         
    }
  }
}

The line marked in bold is not getting tested


I have written a test class for it as below.Please help.

@isTest


    private class testUpdateLookup {
  
public  static testMethod void testocc(){
   

    
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCity='Dubai',
             BillingCountry='United Arab Emirates'
            
       );
         insert acc;
       
        Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];
    
     Contact con=new Contact(
        LastName='blah',
        AccountId=accCreated.Id,
        level__c='Other',
        Role__c='Customer Support',
        LeadSource='Consumer Referral'
   
    );
        insert con;
    
           
           
           
            // TEST ADDING A NEW case                       
      Case cas = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email',
                groupid__c=acc.group_id__c
       );
         insert cas;

//groupid null
       Case cass = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email'
               
       );
         insert cass;   

       
       
     }
}

Hi all,

Iam getting an error in the line highlighted below.Is this the correct way to write it

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
   
      if(c.groupid__c!= null){
      
                
         
          c.group_Account__c = [select id from account where Group_ID__c =c.groupid__c];
    }
  }
}